Skip to content

Commit 5b1cb90

Browse files
lianeetmorehouse
authored andcommitted
feat(table): Selectable rows (fixes #1790) (#2260)
1 parent 1853954 commit 5b1cb90

File tree

4 files changed

+208
-16
lines changed

4 files changed

+208
-16
lines changed

src/components/table/README.md

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,14 @@ fields: [
334334
```html
335335
<template>
336336
<div>
337-
<b-form-checkbox v-model="striped">Striped</b-form-checkbox>
338-
<b-form-checkbox v-model="bordered">Bordered</b-form-checkbox>
339-
<b-form-checkbox v-model="outlined">Outlined</b-form-checkbox>
340-
<b-form-checkbox v-model="small">Small</b-form-checkbox>
341-
<b-form-checkbox v-model="hover">Hover</b-form-checkbox>
342-
<b-form-checkbox v-model="dark">Dark</b-form-checkbox>
343-
<b-form-checkbox v-model="fixed">Fixed</b-form-checkbox>
344-
<b-form-checkbox v-model="footClone">Foot Clone</b-form-checkbox>
337+
<b-form-checkbox inline v-model="striped">Striped</b-form-checkbox>
338+
<b-form-checkbox inline v-model="bordered">Bordered</b-form-checkbox>
339+
<b-form-checkbox inline v-model="outlined">Outlined</b-form-checkbox>
340+
<b-form-checkbox inline v-model="small">Small</b-form-checkbox>
341+
<b-form-checkbox inline v-model="hover">Hover</b-form-checkbox>
342+
<b-form-checkbox inline v-model="dark">Dark</b-form-checkbox>
343+
<b-form-checkbox inline v-model="fixed">Fixed</b-form-checkbox>
344+
<b-form-checkbox inline v-model="footClone">Foot Clone</b-form-checkbox>
345345

346346
<b-table :striped="striped"
347347
:bordered="bordered"
@@ -612,7 +612,7 @@ for details and usage of `<colgroup>`
612612
## Table busy state
613613
`<b-table>` provides a `busy` prop that will flag the table as busy, which you can
614614
set to `true` just before you update your items, and then set it to `false` once you have
615-
your items. When in hte busy state, the tabe will have the attribute `aria-busy="true"`.
615+
your items. When in the busy state, the table will have the attribute `aria-busy="true"`.
616616

617617
During the busy state, the table will be rendered in a "muted" look (`opacity: 0.6`), using the
618618
following custom CSS:
@@ -746,11 +746,12 @@ The slot's scope variable (`data` in the above sample) will have the following p
746746
| `unformatted` | Any | The raw value for this key in the item record (`null` or `undefined` if a virtual column), before being passed to the field's `formatter` function
747747
| `detailsShowing` | Boolean | Will be `true` if the row's `row-details` scoped slot is visible. See section [**Row details support**](#row-details-support) below for additional information
748748
| `toggleDetails` | Function | Can be called to toggle the visibility of the rows `row-details` scoped slot. See section [**Row details support**](#row-details-support) below for additional information
749+
| `rowSelected` | Boolean | Will be `true` if the row has been selected. See section [**Row select support**](#row-select-support) for additional information
749750

750751

751752
**Notes:**
752753
- _`index` will not always be the actual row's index number, as it is
753-
computed after pagination and filtering have been applied to the original
754+
computed after filtering, sorting and pagination have been applied to the original
754755
table data. The `index` value will refer to the **displayed row number**. This
755756
number will align with the indexes from the optional `v-model` bound variable._
756757

@@ -901,6 +902,64 @@ event will not be emitted when the input, select, textarea is clicked (unless th
901902
(even when disabled)
902903

903904

905+
## Row select support
906+
You can make rows selectable, by using the prop `selectable`.
907+
908+
Users can easily change the selecting mode by setting the `select-mode` prop.
909+
- `multi`: each click will select/deselect the row (default mode)
910+
- `single`: only a single row can be selected at one time
911+
- `range`: any row clicked is toggled, any other deselected. the SHIFT key selects a range of
912+
rows, and CTRL/CMD click will toggle the selected row.
913+
914+
When a table is `selectable` and the user clicks on a row, `<b-table>` will emit the `row-selected`
915+
event, passing a single argument which is the complete list of selected items.
916+
**Treat this argument as read-only.**
917+
918+
```html
919+
<template>
920+
<b-form-group
921+
label="Selection mode:"
922+
horizontal
923+
:label-cols="4"
924+
breakpoint="md"
925+
>
926+
<b-form-select v-model="selectMode" :options="modes" class="mb-3" />
927+
</b-form-group>
928+
<b-table selectable :select-mode="selectMode" selectedVariant="success" :items="items" @row-selected="rowSelected"></b-table>
929+
{{ selected }}
930+
</template>
931+
932+
<script>
933+
export default {
934+
data () {
935+
return {
936+
modes: ['multi', 'single', 'range'],
937+
items: [
938+
{ isActive: true, age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
939+
{ isActive: false, age: 21, first_name: 'Larsen', last_name: 'Shaw' },
940+
{ isActive: false, age: 89, first_name: 'Geneva', last_name: 'Wilson' },
941+
{ isActive: true, age: 38, first_name: 'Jami', last_name: 'Carney' }
942+
],
943+
selectMode: 'multi',
944+
selected: []
945+
}
946+
},
947+
methods: {
948+
rowSelected(items) {
949+
this.selected = items
950+
}
951+
}
952+
}
953+
</script>
954+
955+
<!-- table-selectable.vue -->
956+
```
957+
958+
**Notes:**
959+
- _Paging, filtering, or sorting will clear the selection. The `row-selected` event will be emitted with an empty array._
960+
- _Selected rows will have a class of `b-row-selected` added to them._
961+
- _When the table is in `selectable` mode, all data item `<tr>` elements will be in the document tab sequence (`tabindex="0"`) for accesibility reasons._
962+
904963
## Row details support
905964
If you would optionally like to display additional record information (such as
906965
columns not specified in the fields definition array), you can use the scoped slot
@@ -1404,6 +1463,22 @@ when fetching your data!
14041463
When `b-table` is mounted in the document, it will automatically trigger a provider update call.
14051464

14061465

1466+
## Table accessibility notes
1467+
When the table is in `selectable` mode, or if there is a `row-clicked` event listener registered, all
1468+
data item rows (`<tr>` elements) will be placed into the document tab sequence (via `tabindex="0"`) to
1469+
allow keyboard-only and screen reader users the ability to click the rows.
1470+
1471+
When a column (field) is sortable, the header (and footer) heading cells will also be placed into the
1472+
document tab sequence for accesibility.
1473+
1474+
Note the following row based events/actions are not considered accessible, and should only be used if the
1475+
functionality is non critical or can be provided via other means:
1476+
1477+
- `row-dblclicked`
1478+
- `row-contextmenu`
1479+
- `row-hovered`
1480+
- `row-unhovered`
1481+
14071482
## Complete Example
14081483

14091484
```html

src/components/table/_table.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,9 @@
155155
}
156156
}
157157
}
158+
159+
/* b-table: selectable rows */
160+
table.b-table.b-table-selectable > tbody > tr {
161+
cursor: pointer;
162+
user-select: none;
163+
}

src/components/table/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@
9595
}
9696
]
9797
},
98+
{
99+
"event": "row-selected",
100+
"description": "Emitted when a row or rows have been selected.",
101+
"args": [
102+
{
103+
"arg": "rows",
104+
"description": "Array of the row items that are selected."
105+
}
106+
]
107+
},
98108
{
99109
"event": "head-clicked",
100110
"description": "Emitted when a header or footer cell is clicked.",

0 commit comments

Comments
 (0)