Are you an LLM? You can read better optimized documentation at /front/component/ma-table/dynamic-columns.md for this page in Markdown format
Dynamic Column Management β
Demonstrates how to dynamically control column visibility, add/remove columns, and use Expose methods for column management.
Dynamic Columns Demo β
Features β
Column Control Methods β
- Show/Hide: Control column visibility through reactive data
- Dynamic Addition: Add new columns at runtime
- Dynamic Removal: Remove unnecessary columns
- Column Configuration Retrieval: Get current column configuration
Expose Methods β
ma-table provides comprehensive column management APIs:
setColumns()
: Reset all columnsgetColumns()
: Get current column configurationappendColumn()
: Append new columnremoveColumn()
: Remove specified columngetColumnByProp()
: Get column configuration by property name
Usage Examples β
Column Visibility Control β
vue
<template>
<div>
<!-- Column toggle switches -->
<el-switch
v-for="(visible, key) in columnVisibility"
:key="key"
v-model="columnVisibility[key]"
:label="getColumnLabel(key)"
/>
<ma-table
:columns="visibleColumns"
:data="data"
:options="options"
/>
</div>
</template>
<script setup>
import { ref, computed } from 'vue'
// Column visibility state
const columnVisibility = ref({
name: true,
age: true,
email: true,
department: false
})
// Base column configuration
const baseColumns = [
{ label: 'Name', prop: 'name' },
{ label: 'Age', prop: 'age' },
{ label: 'Email', prop: 'email' },
{ label: 'Department', prop: 'department' }
]
// Compute visible columns
const visibleColumns = computed(() => {
return baseColumns.filter(col =>
columnVisibility.value[col.prop]
)
})
</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
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
Using Expose Methods for Dynamic Column Management β
vue
<template>
<ma-table
ref="tableRef"
:columns="columns"
:data="data"
:options="options"
/>
</template>
<script setup>
import { ref } from 'vue'
const tableRef = ref()
// Add new column
const addColumn = () => {
const newColumn = {
label: 'New Column',
prop: 'newField',
cellRender: ({ row }) => <el-tag>Dynamic Column</el-tag>
}
tableRef.value?.appendColumn(newColumn)
}
// Remove column
const removeColumn = (prop) => {
tableRef.value?.removeColumn(prop)
}
// Get column configuration
const getColumns = () => {
const columns = tableRef.value?.getColumns()
console.log('Current column configuration:', columns)
}
// Reset column configuration
const resetColumns = () => {
const newColumns = [
{ label: 'Name', prop: 'name' },
{ label: 'Age', prop: 'age' }
]
tableRef.value?.setColumns(newColumns)
}
</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
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
Dynamic Column Form β
vue
<template>
<div>
<el-dialog v-model="dialogVisible" title="Add Column">
<el-form :model="newColumn">
<el-form-item label="Column Title" required>
<el-input v-model="newColumn.label" />
</el-form-item>
<el-form-item label="Field Name" required>
<el-input v-model="newColumn.prop" />
</el-form-item>
<el-form-item label="Column Width">
<el-input v-model="newColumn.width" />
</el-form-item>
<el-form-item label="Alignment">
<el-select v-model="newColumn.align">
<el-option label="Left" value="left" />
<el-option label="Center" value="center" />
<el-option label="Right" value="right" />
</el-select>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">Cancel</el-button>
<el-button type="primary" @click="confirmAddColumn">Confirm</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup>
const dialogVisible = ref(false)
const newColumn = ref({
label: '',
prop: '',
width: '',
align: 'center'
})
const confirmAddColumn = () => {
// Validate form
if (!newColumn.value.label || !newColumn.value.prop) {
return
}
// Build column configuration
const column = {
label: newColumn.value.label,
prop: newColumn.value.prop,
align: newColumn.value.align,
width: newColumn.value.width ? parseInt(newColumn.value.width) : undefined,
cellRender: ({ row }) => row[newColumn.value.prop] || '--'
}
// Add to table
tableRef.value?.appendColumn(column)
// Reset form
newColumn.value = { label: '', prop: '', width: '', align: 'center' }
dialogVisible.value = false
}
</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
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
API Reference β
Expose Methods β
Method | Description | Parameters | Return Value |
---|---|---|---|
setColumns(columns) | Reset table columns | MaTableColumns[] | - |
getColumns() | Get current column configuration | - | MaTableColumns[] |
appendColumn(column) | Append new column to table end | MaTableColumns | - |
removeColumn(prop) | Remove column by property name | string | - |
getColumnByProp(prop) | Get column configuration by property name | string | MaTableColumns |
Dynamic Column Configuration β
typescript
interface MaTableColumns {
label: string // Column title
prop: string // Field name
width?: number // Column width
minWidth?: number // Minimum width
align?: 'left' | 'center' | 'right' // Alignment
hide?: boolean // Whether to hide
cellRender?: Function // Custom render
// ... Other Element Plus column properties
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Use Cases β
- Personalized Configuration: Users can show/hide specific columns as needed
- Permission Control: Dynamically display different columns based on user permissions
- Data Adaptation: Adjust column configuration according to different data sources
- Real-time Configuration: Add/remove columns at runtime based on business needs
- Column Preference Saving: Save user column configuration preferences
Best Practices β
- State Management: Use reactive data to manage column visibility
- Configuration Validation: Verify completeness and uniqueness when adding new columns
- User Experience: Provide intuitive column control interfaces like switches, checkboxes
- Data Synchronization: Ensure data contains corresponding fields when adding columns
- Performance Optimization: Avoid frequent column operations, consider batch updates
Notes β
- When dynamically removing columns, corresponding data won't be deleted, only hidden
- Dynamically added columns must have corresponding fields in the data, otherwise empty
- Using
setColumns
will completely replace existing column configuration - Column
prop
must be unique, no duplicates allowed