# Row Selection

# Table Row VTr

To enable row selection you need to use the VTr component. It only has one property: row

# Row object

You must provide the row property with the current Object for the row:

<VTable :data="users">
      <template #head>
        <th>Name</th>
        ...
      </template>
      <template #body="{ rows }">
        <VTr
          v-for="row in rows"
          :key="row.guid"
          :row="row"
        >
          <td>{{ row.name }}</td>
          ...
        </VTr>
      </template>
    </VTable>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# Selection Options

You can configure the Selection Mode and the Selected Class in the VTable component.

# Selection Mode 'single' | 'multiple'

By default, the selection mode is single, meaning only one row at a time can be selected. You can use the selectionMode property in the VTable component to set it to multiple, so multiple rows can be selected.

# Selected Class string

When a row is selected a class is added to the tr element, by default it is vt-selected but you can change it to something else with the selectedClass property in the VTable component.

# Get selected rows

VTable exposes the selected rows with the stateChanged event. Every time the selection changes the event is triggered. The stateChanged payload object includes a selectedRows property with our selected rows. You can listen to the event and store the selected rows locally for your use:

<template>
  <VTable
    :data="users"
    @stateChanged="selectedRows = $event.selectedRows"
  >
    ...
  </VTable>
</template>
1
2
3
4
5
6
7
8
<script>
  import users from './users.json'

  export default {
    data: () => ({
      users,
      selectedRows: []
    })
  }
</script>
1
2
3
4
5
6
7
8
9
10

You can learn more about the stateChanged event in the Table State section.

# Advanced Selection

Through scoped slots and one setting, you can have fine control over how the selection works:

# Select On Click

By default a VTr row will be selected when the user clicks on it. However, if you want to have more control over it, for instance to show a checkbox, you can set selectOnClick to false in the VTable component, this will make it so clicking a VTr will not select the row.

<template>
  <VTable
    :data="users"
    :selectOnClick="false"
  >
    ...
  </VTable>
</template>
1
2
3
4
5
6
7
8

# Head Scoped Slot

The head scoped slot in the VTable component provides the following properties:

PropertyDescriptionType Definition
selectAllFunction to select all the rows() => void
deselectAllFunction to deselect all the rows() => void
toggleAllRowsFunction to toggle all the rows selection. If all rows are selected it deselects them all, otherwise it will select them all.() => void
selectedRowsAn array with all the selected rowsany[]
allRowsSelectedBoolean indicating whether or not all rows are selectedboolean
<template>
  <VTable
    :data="users"
  >
    <template #head="{ allRowsSelected, selectAll, deselectAll, toggleAllRows, selectedRows }">
      ...
    </template>
  </VTable>
</template>
1
2
3
4
5
6
7
8
9

# Body Scoped Slot

The body scoped slot in the <VTable> provides the following properties:

PropertyDescriptionType Definition
selectRowFunction to select one row. The row parameter should be === to one row in the table.(row: any) => void
deselectRowFunction to deselect one row. The row parameter should be === to one row in the table.(row: any) => void
selectedRowsAn array with all the selected rowsany[]
<template>
  <VTable
    :data="users"
  >
    <template #body="{ selectRow, deselectRow, selectedRows }">
      ...
    </template>
  </VTable>
</template>
1
2
3
4
5
6
7
8
9

# VTr Default Slot

The VTr component on its default slot provides the following properties:

PropertyDescriptionType Definition
isSelectedBoolean indicating whether or not the row is selectedboolean
toggleFunction to toggle the selection state of the VTr() => void
<template>
  <VTable
    :data="users"
  >
    <template #body="{ rows }">
      <VTr 
        v-for="row in rows" 
        :key="row.id"
        v-slot="{ isSelected, toggle }"
      >
        ...
      </VTr>
    </template>
  </VTable>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Example

<template>
 <div>
   <VTable
     ref="usersTable"
     :data="users"
     selectionMode="multiple"
     :selectOnClick="false"
     @stateChanged="selectedRows = $event.selectedRows"
   >
     <template #head="{ allRowsSelected, toggleAllRows }">
       <th>
         <input
           type="checkbox"
           :checked="allRowsSelected"
           @change="toggleAllRows"
         />
       </th>
       <th>Name</th>
       <th>Age</th>
       <th>State</th>
       <th>Registered at</th>
     </template>
     <template #body="{ rows }">
       <VTr
         v-for="row in rows"
         :key="row.guid"
         :row="row"
         v-slot="{ isSelected, toggle}"
       >
         <td>
           <input
             type="checkbox"
             :checked="isSelected"
             @change="toggle"
           />
         </td>
         <td>{{ row.name }}</td>
         <td>{{ row.age }}</td>
         <td>{{ row.address.state }}</td>
         <td>{{ row.registered }}</td>
       </VTr>
     </template>
   </VTable>

   <strong>Selected:</strong>
   <div v-if="selectedRows.length === 0">No rows selected</div>
   <ul>
     <li v-for="selected in selectedRows">
       {{ selected.name }}
     </li>
   </ul>
 </div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<script>
   import users from './users.json'

   export default {
       name: 'Selection',
       data: () => ({
         users: users,
         selectedRows: []
       })
   }
</script> 
1
2
3
4
5
6
7
8
9
10
11
NameAgeStateRegistered at
Deana Lindsay25Pennsylvania2015-10-30
Wyatt Kline39Maine2014-06-17
Harmon Huber29American Samoa2016-01-25
Penny Maddox29Oregon2016-01-08
Morgan Gomez39Connecticut2014-04-10
Beck Mckay35Mississippi2016-05-06
Massey Carlson39Hawaii2014-10-22
Hill Hale33Colorado2016-04-18
Stokes Hurst26North Dakota2016-01-30
Cain Knapp20Michigan2016-01-04
Selected:
No rows selected

# Selection API

We also provide an API to perform some selection actions such as selecting all the rows. These are functions that are exposed in the VTable component. The first thing you need to do is to get a ref to the VTable component:

<template>
  <VTable
    :data="users",
    :ref="usersTable"
  >
    ...
  </VTable>
</template>
1
2
3
4
5
6
7
8

Once we have a ref, we can now call a couple of functions to manipulate the table selection:

# Select All

The selectAll function will select all the rows in the table.

If you have pagination enabled we will still select all the rows even the ones that are not currently visible.

<script>
  export default {
    methods: {
      selectAll () {
        this.$refs.usersTable.selectAll()
      }
    }
  }
</script>
1
2
3
4
5
6
7
8
9

# Deselect All

The deselectAll function will deselect all the rows in the table currently selected.

If you have pagination enabled we will still deselect all the rows even the ones that are not currently visible.

<script>
  export default {
    methods: {
      deselectAll () {
        this.$refs.usersTable.deselectAll()
      }
    }
  }
</script>
1
2
3
4
5
6
7
8
9

# Select Rows

Clicking on a row is not the only way to select rows, you can also select them programmatically. For instance, you may want your table to have a couple of rows selected by default.

For those instances you can use the selectRows function. The function only has one parameter rows which is an array of rows to select.

<script>
  import users from './users.json'

  export default {
    data: () => ({
      users: users
    }),
  mounted () {
    const toSelect = [users[0], users[1], users[2]]
    this.$refs.usersTable.selectRows(toSelect)
  }
</script>
1
2
3
4
5
6
7
8
9
10
11
12

WARNING

For the selectRows function to work properly, the rows parameter must contain objects that exist in the data property of the VTable components. In other words, the rows parameter must be a subset of the data property.

WARNING

If the selectionMode is single and you provide more than one object in the rows parameter, only the last object in the array will be selected.

# Example

<template>
  <div>
    <button @click="selectAll">Select All</button>
    <button @click="deselectAll">Deselect All</button>

    <VTable
      ref="usersTable"
      :data="users"
      selectionMode="multiple"
      selectedClass="selected-row"
      @stateChanged="selectedRows = $event.selectedRows"
    >
      <template #head>
        <th>Name</th>
        <th>Age</th>
        <th>State</th>
        <th>Registered at</th>
      </template>
      <template #body="{ rows }">
        <VTr
          v-for="row in rows"
          :key="row.guid"
          :row="row"
        >
          <td>{{ row.name }}</td>
          <td>{{ row.age }}</td>
          <td>{{ row.address.state }}</td>
          <td>{{ row.registered }}</td>
        </VTr>
      </template>
    </VTable>

    <strong>Selected:</strong>
    <div v-if="selectedRows.length === 0">
      No rows selected
    </div>
    <ul>
      <li v-for="selected in selectedRows">
        {{ selected.name }}
      </li>
    </ul>
  </div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<script>
import users from './users.json'

export default {
  data: () => ({
    users: users,
    selectedRows: []
  }),
  mounted () {
    const toSelect = [users[0], users[1], users[2]]
    this.$refs.usersTable.selectRows(toSelect)
  },
  methods: {
    selectAll () {
      this.$refs.usersTable.selectAll()
    },
    deselectAll () {
      this.$refs.usersTable.deselectAll()
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
NameAgeStateRegistered at
Deana Lindsay25Pennsylvania2015-10-30
Wyatt Kline39Maine2014-06-17
Harmon Huber29American Samoa2016-01-25
Penny Maddox29Oregon2016-01-08
Morgan Gomez39Connecticut2014-04-10
Beck Mckay35Mississippi2016-05-06
Massey Carlson39Hawaii2014-10-22
Hill Hale33Colorado2016-04-18
Stokes Hurst26North Dakota2016-01-30
Cain Knapp20Michigan2016-01-04
Selected:
No rows selected
Last Updated: 7/28/2021, 8:24:25 PM