| 
 | 1 | + | 
 | 2 | +:::demo 通过设置列对象中的 isEdit 属性,控制哪些列开启单元格编辑功能。通过传入回调函数 cell-edit-done,接收编辑结果。  | 
 | 3 | + | 
 | 4 | +```html  | 
 | 5 | +<template>  | 
 | 6 | +    <v-table  | 
 | 7 | +            :columns="tableConfig.columns"  | 
 | 8 | +            :table-data="tableConfig.tableData"  | 
 | 9 | +            row-hover-color="#eee"  | 
 | 10 | +            row-click-color="#edf7ff"  | 
 | 11 | +            :cell-edit-formatter="cellEditFormatter"  | 
 | 12 | +            :cell-edit-done="cellEditDone"  | 
 | 13 | +    ></v-table>  | 
 | 14 | +</template>  | 
 | 15 | + | 
 | 16 | +<style>  | 
 | 17 | +    .cell-edit-color{  | 
 | 18 | +        color:#2db7f5;  | 
 | 19 | +        font-weight: bold;  | 
 | 20 | +    }  | 
 | 21 | +</style>  | 
 | 22 | + | 
 | 23 | +<script>  | 
 | 24 | +
  | 
 | 25 | +    import mockData from '../../mock/tableData.js'  | 
 | 26 | +
  | 
 | 27 | +    export default{  | 
 | 28 | +        data() {  | 
 | 29 | +            return {  | 
 | 30 | +                tableConfig: {  | 
 | 31 | +                    tableData:[],  | 
 | 32 | +                    columns:  [  | 
 | 33 | +                             {field: 'name', title:'姓名', width: 100, titleAlign: 'center',columnAlign:'center',isEdit:true,  | 
 | 34 | +                              formatter: function (rowData,rowIndex,pagingIndex,field) {  | 
 | 35 | +
  | 
 | 36 | +                                   return `<span class='cell-edit-color'>${rowData[field]}</span>`;  | 
 | 37 | +                               }},  | 
 | 38 | +                             {field: 'tel', title: '手机号码', width: 260, titleAlign: 'center',columnAlign:'center',isEdit:true},  | 
 | 39 | +                             {field: 'hobby', title: '爱好', width: 380, titleAlign: 'center',columnAlign:'center',isEdit:true},  | 
 | 40 | +                             {field: 'address', title: '地址', width: 358, titleAlign: 'center',columnAlign:'left',isEdit:true}  | 
 | 41 | +                         ],  | 
 | 42 | +                    titleBgColor: "#e1e1e1"  | 
 | 43 | +                }  | 
 | 44 | +            }  | 
 | 45 | +        },  | 
 | 46 | +        methods:{  | 
 | 47 | +           // 单元格编辑格式化回调  | 
 | 48 | +           cellEditFormatter(newValue,oldValue,rowIndex,rowData,field){  | 
 | 49 | +
  | 
 | 50 | +                if (field === 'name'){  | 
 | 51 | +                    return `<span class="cell-edit-color">${newValue}</span>`  | 
 | 52 | +                }  | 
 | 53 | +           },  | 
 | 54 | +
  | 
 | 55 | +            // 单元格编辑回调  | 
 | 56 | +            cellEditDone(newValue,oldValue,rowIndex,rowData,field){  | 
 | 57 | +                 console.log(newValue)  | 
 | 58 | +                 console.log(oldValue)  | 
 | 59 | +                 console.log(rowIndex)  | 
 | 60 | +                 console.log(rowData)  | 
 | 61 | +                 console.log(field)  | 
 | 62 | +            }  | 
 | 63 | +        },  | 
 | 64 | +        created(){  | 
 | 65 | +
  | 
 | 66 | +            this.tableConfig.tableData = mockData;  | 
 | 67 | +        }  | 
 | 68 | +    }  | 
 | 69 | +</script>  | 
 | 70 | +```  | 
 | 71 | +:::  | 
 | 72 | + | 
0 commit comments