Skip to content

Commit

Permalink
feat(Table): support nested keys in columns (#503)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
  • Loading branch information
vladyslav-mikhieiev and benjamincanac committed Sep 7, 2023
1 parent 74f4903 commit 858886a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/content/4.data/1.table.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ Even though you can customize the sort button as mentioned in the [Sortable](#so

### `<column>-data`

Use the `#<column>-data` slot to customize the data cell of a column. You will have access to the `row` and `column` properties in the slot scope.
Use the `#<column>-data` slot to customize the data cell of a column. You will have access to the `row`, `column` and `getRowData` properties in the slot scope.

You can for example create an extra column for actions with a [Dropdown](/elements/dropdown) component inside or change the color of the rows based on a selection.

Expand Down
14 changes: 9 additions & 5 deletions src/runtime/components/data/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
</td>

<td v-for="(column, subIndex) in columns" :key="subIndex" :class="[ui.td.base, ui.td.padding, ui.td.color, ui.td.font, ui.td.size]">
<slot :name="`${column.key}-data`" :column="column" :row="row" :index="index">
{{ row[column.key] }}
<slot :name="`${column.key}-data`" :column="column" :row="row" :index="index" :get-row-data="(defaultValue: any) => getRowData(row, column.key, defaultValue)">
{{ getRowData(row, column.key) }}
</slot>
</td>
</tr>
Expand All @@ -69,9 +69,8 @@
<script lang="ts">
import { ref, computed, defineComponent, toRaw } from 'vue'
import type { PropType } from 'vue'
import { capitalize, orderBy } from 'lodash-es'
import { capitalize, orderBy, omit, get } from 'lodash-es'
import { defu } from 'defu'
import { omit } from 'lodash-es'
import type { Button } from '../../types/button'
import { useAppConfig } from '#imports'
// TODO: Remove
Expand Down Expand Up @@ -231,6 +230,10 @@ export default defineComponent({
}
}
function getRowData (row: Object, rowKey: string | string[], defaultValue: any = 'Failed to get cell value') {
return get(row, rowKey, defaultValue)
}
return {
// eslint-disable-next-line vue/no-dupe-keys
ui,
Expand All @@ -247,7 +250,8 @@ export default defineComponent({
isSelected,
onSort,
onSelect,
onChange
onChange,
getRowData
}
}
})
Expand Down

0 comments on commit 858886a

Please sign in to comment.