vxe-table如何使用vxe-grid實(shí)現(xiàn)動(dòng)態(tài)配置表格
更新時(shí)間:2025年04月24日 10:45:00 作者:小泡泡c
這篇文章主要介紹了vxe-table如何使用vxe-grid實(shí)現(xiàn)動(dòng)態(tài)配置表格問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
一、圖1:界面效果

說(shuō)明:
- 1)刪除:觸發(fā)函數(shù)
deleteRow刪除當(dāng)前行。 - 2)編輯:觸發(fā)函數(shù)
editRow將當(dāng)前行激活,界面發(fā)生變化。見(jiàn)圖2。
二、圖2:編輯效果

說(shuō)明:
- 3)保存:觸發(fā)函數(shù)
saveRow保存當(dāng)前行數(shù)據(jù)。 - 4)取消:觸發(fā)函數(shù)
cancelRow還原當(dāng)前行數(shù)據(jù)。
三、表格配置
// 表格配置
export const colConfig = [
{
type:'seq', // 列的類(lèi)型:seq、checkbox、radio、expand
title:'序號(hào)',
align:'center'
},
{
title:'姓名', // 列標(biāo)題(支持開(kāi)啟國(guó)際化)
field:'name', // 列字段名(注:屬性層級(jí)越深,渲染性能就越差,例如:aa.bb.cc.dd.ee)
align:'center', // 對(duì)齊方式
editRender:{ // 可編輯渲染器配置項(xiàng)
name:'input' // 渲染器名稱:input, textarea, select, $input, $select, $switch
}
},
{
title:'年齡',
field:'age',
align:'center',
editRender:{
name:'input'
}
},
{
title:'性別',
field:'sex',
align:'center',
editRender:{
name:'input'
}
}
]
// 表格數(shù)據(jù)
export const tableData = [
{
name:'小紅',
age:15,
sex:'女',
id:1
},
{
name:'小白',
age:25,
sex:'男',
id:2
},
{
name:'小藍(lán)',
age:21,
sex:'女',
id:3
}
]四、代碼實(shí)現(xiàn)
<template>
<div style="padding:100px">
<vxe-grid
ref="xTable"
:max-height="450"
v-bind="gridOptions"
show-overflow
keep-source
highlight-hover-row
highlight-current-row
resizable
auto-resize
:row-config="{ isCurrent: true, isHover: true,keyField:'id' }"
:edit-config="{trigger: 'manual', mode: 'row'}"
>
>
<template #operation="{ row }">
<vxe-button type="text" status="primary" @click="deleteRow(row)">刪除</vxe-button>
<span v-if="$refs.xTable.isActiveByRow(row)">
<vxe-button type="text" status="primary" @click="saveRow()">保存</vxe-button>
<vxe-button type="text" status="primary" @click="cancelRow(row)">取消</vxe-button>
</span>
<span v-else>
<vxe-button type="text" status="primary" @click="editRow(row)">編輯</vxe-button>
</span>
</template>
</vxe-grid>
</div>
</template>
<script>
import { colConfig, tableData } from './colConfig'
export default {
data() {
return {
gridOptions: {
columns: [],
data: [],
},
}
},
created() {
this.getTableData()
},
methods: {
getTableData() {
// 1.設(shè)置表格列
this.gridOptions.columns = colConfig
// 2.設(shè)置表格數(shù)據(jù)
this.gridOptions.data = tableData
// 3.添加操作列
let operationCol = {
title: '操作',
slots: { default: 'operation' },
align: 'center'
}
this.gridOptions.columns.push(operationCol)
},
deleteRow(row) {
let index = this.gridOptions.data.findIndex(item => item.id === row.id)
this.gridOptions.data.splice(index, 1)
},
editRow(row) {
const $table = this.$refs.xTable
$table.setActiveRow(row)
},
saveRow() {
const $table = this.$refs.xTable
$table.clearActived().then(() => {
this.loading = true
setTimeout(() => {
this.loading = false
}, 300)
})
},
cancelRow(row) {
const $table = this.$refs.xTable
$table.clearActived().then(() => {
// 還原行數(shù)據(jù)
$table.revertData(row)
})
}
}
}
</script>官方文檔:vxe-table API
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決echarts數(shù)據(jù)二次渲染不成功的問(wèn)題
這篇文章主要介紹了解決echarts數(shù)據(jù)二次渲染不成功的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue?狀態(tài)存儲(chǔ)和會(huì)話存儲(chǔ)同步清空問(wèn)題解決方案(最新推薦)
文章介紹了在使用Pinia定義的useHeaderTabStore中,tab狀態(tài)通過(guò)會(huì)話存儲(chǔ)初始化但未在退出登錄時(shí)同步清空的問(wèn)題,通過(guò)在PiniaStore中定義清空tab的函數(shù),并在退出登錄時(shí)調(diào)用該函數(shù),可以確保狀態(tài)和會(huì)話存儲(chǔ)同步清空,避免內(nèi)存中殘留舊數(shù)據(jù),感興趣的朋友一起看看吧2024-12-12
Vuex中actions優(yōu)雅處理接口請(qǐng)求的方法
在項(xiàng)目開(kāi)發(fā)中,如果使用到了 vuex,通常我會(huì)將所有的接口請(qǐng)求單獨(dú)用一個(gè)文件管理,這篇文章主要介紹了Vuex中actions如何優(yōu)雅處理接口請(qǐng)求,業(yè)務(wù)邏輯寫(xiě)在 actions 中,本文給大家分享完整流程需要的朋友可以參考下2022-11-11
Vue2和Vue3在v-for遍歷時(shí)ref獲取dom節(jié)點(diǎn)的區(qū)別及說(shuō)明
這篇文章主要介紹了Vue2和Vue3在v-for遍歷時(shí)ref獲取dom節(jié)點(diǎn)的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

