Are you an LLM? You can read better optimized documentation at /front/component/ma-table/selection.md for this page in Markdown format
Multi-Select Table β
Demonstrates the multi-select functionality of tables, including single selection, select all, batch operations, etc.
Multi-Select Table Demo β
Features β
Selection Control β
- Single-row selection: Click checkbox to select single row data
- Select all: Header checkbox to select/deselect all
- Selection constraints: Configure certain rows as non-selectable
- Selection persistence: Supports maintaining selection state across pages
Batch Operations β
- Batch processing: Perform operations based on selected rows
- Selection statistics: Real-time display of selected record count and status
- Operation feedback: Confirmation and result feedback for batch operations
Configuration Examples β
Basic Multi-Select Configuration β
javascript
const columns = [
{
type: 'selection', // Multi-select column
width: 50,
selectable: (row, index) => {
// Set selection constraints
return row.status !== 'disabled'
}
},
{ label: 'Name', prop: 'name' },
{ label: 'Status', prop: 'status' }
]
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Listening to Selection Events β
javascript
const options = {
on: {
onSelect: (selection, row) => {
console.log('Single selection:', row, selection.includes(row) ? 'selected' : 'deselected')
},
onSelectAll: (selection) => {
console.log('Select all:', selection.length > 0 ? 'select all' : 'deselect all')
},
onSelectionChange: (selection) => {
console.log('Selection changed:', selection)
// Update selected state
selectedRows.value = selection
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Programmatic Selection Control β
vue
<template>
<div>
<div class="selection-controls">
<el-button @click="selectAll">Select All</el-button>
<el-button @click="clearSelection">Clear Selection</el-button>
<el-button @click="toggleSelection">Toggle Selection</el-button>
<el-button @click="selectActive">Select Active Status</el-button>
</div>
<ma-table
ref="tableRef"
:columns="columns"
:data="data"
:options="options"
/>
</div>
</template>
<script setup>
const tableRef = ref()
const selectedRows = ref([])
// Select all
const selectAll = () => {
const selectableRows = data.value.filter(row => row.status !== 'disabled')
selectableRows.forEach(row => {
tableRef.value?.getElTableRef()?.toggleRowSelection(row, true)
})
}
// Clear selection
const clearSelection = () => {
tableRef.value?.getElTableRef()?.clearSelection()
}
// Toggle selection
const toggleSelection = () => {
data.value.forEach(row => {
if (row.status !== 'disabled') {
const isSelected = selectedRows.value.includes(row)
tableRef.value?.getElTableRef()?.toggleRowSelection(row, !isSelected)
}
})
}
// Select rows meeting specific conditions
const selectActive = () => {
const activeRows = data.value.filter(row => row.status === 'active')
activeRows.forEach(row => {
tableRef.value?.getElTableRef()?.toggleRowSelection(row, true)
})
}
</script>
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
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
Batch Operation Example β
vue
<template>
<div>
<!-- Batch operation toolbar -->
<div v-if="selectedRows.length > 0" class="batch-toolbar">
<span>Selected {{ selectedRows.length }} records</span>
<el-button type="success" @click="batchActivate">
Batch Activate
</el-button>
<el-button type="warning" @click="batchDeactivate">
Batch Deactivate
</el-button>
<el-button type="danger" @click="batchDelete">
Batch Delete
</el-button>
</div>
<ma-table
:columns="columns"
:data="data"
:options="options"
/>
</div>
</template>
<script setup>
// Batch activate
const batchActivate = async () => {
try {
await ElMessageBox.confirm(
`Confirm activating ${selectedRows.value.length} selected users?`,
'Batch Activate',
{ type: 'warning' }
)
// Execute batch operation
const ids = selectedRows.value.map(row => row.id)
await api.batchActivateUsers(ids)
// Update local data
selectedRows.value.forEach(row => {
row.status = 'active'
})
ElMessage.success('Batch activation successful')
clearSelection()
} catch (error) {
if (error !== 'cancel') {
ElMessage.error('Batch activation failed')
}
}
}
// Batch delete
const batchDelete = async () => {
try {
await ElMessageBox.confirm(
`Confirm deleting ${selectedRows.value.length} selected records? This cannot be undone!`,
'Batch Delete',
{ type: 'error' }
)
const ids = selectedRows.value.map(row => row.id)
await api.batchDeleteUsers(ids)
// Remove from local data
data.value = data.value.filter(row => !ids.includes(row.id))
ElMessage.success('Batch deletion successful')
selectedRows.value = []
} catch (error) {
if (error !== 'cancel') {
ElMessage.error('Batch deletion failed')
}
}
}
</script>
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Multi-Select Parameters β
Selection Column Configuration β
Parameter | Description | Type | Default |
---|---|---|---|
type | Column type, set to 'selection' for multi-select column | string | - |
selectable | Whether the row's checkbox can be checked | Function(row, index) | - |
reserveSelection | Whether to retain selection after data updates | boolean | false |
Selection Events β
Event | Description | Parameters |
---|---|---|
select | Triggered when manually checking a row's Checkbox | (selection, row) |
select-all | Triggered when manually checking the select-all Checkbox | (selection) |
selection-change | Triggered when selection changes | (selection) |
Table Methods β
Access the following methods via tableRef.value?.getElTableRef()
:
Method | Description | Parameters |
---|---|---|
clearSelection | Clear all selections | - |
getSelectionRows | Returns currently selected rows | - |
toggleRowSelection | Toggle a row's selection state | (row, selected) |
toggleAllSelection | Toggle select-all state | - |
Usage Scenarios β
1. User Management β
javascript
// User data structure
const userData = [
{
id: 1,
name: 'Zhang San',
status: 'active', // Selectable in active status
role: 'admin'
},
{
id: 2,
name: 'Li Si',
status: 'disabled', // Non-selectable in disabled status
role: 'user'
}
]
// Selection constraints
const columns = [
{
type: 'selection',
selectable: (row) => row.status !== 'disabled'
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2. Order Management β
javascript
// Batch process orders
const batchProcessOrders = async (action) => {
const orderIds = selectedRows.value.map(row => row.orderId)
try {
switch (action) {
case 'ship':
await api.batchShipOrders(orderIds)
break
case 'cancel':
await api.batchCancelOrders(orderIds)
break
case 'export':
await api.exportOrders(orderIds)
break
}
ElMessage.success(`Batch ${action} operation successful`)
refreshData()
} catch (error) {
ElMessage.error(`Batch ${action} operation failed`)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
3. Data Export β
javascript
// Export selected data
const exportSelected = () => {
if (selectedRows.value.length === 0) {
ElMessage.warning('Please select data to export first')
return
}
// Construct export data
const exportData = selectedRows.value.map(row => ({
Name: row.name,
Email: row.email,
Department: row.department,
Position: row.position
}))
// Execute export
exportToExcel(exportData, 'UserData.xlsx')
ElMessage.success(`Exported ${selectedRows.value.length} records`)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Best Practices β
- Selection constraints: Properly configure
selectable
function to prevent selecting invalid data - State synchronization: Ensure selected state remains synchronized with actual data
- Batch confirmation: Implement secondary confirmation for batch operations, especially dangerous ones like deletion
- Operation feedback: Provide timely feedback on batch operation results
- Performance considerations: For large datasets, consider virtual scrolling or pagination
Notes β
- Selection state is not automatically persisted and will be lost on page refresh
- Cross-page selection requires special handling using
reserveSelection
property - Consider network exceptions and partial failures during batch operations
- Selection constraint functions are called during each render - avoid complex computations