vue获取table一列数据_VUE table表格动态添加一列数据,新增的这些数据不可以编辑(v-model绑定的数据不能实时更新)...
时间: 2024-03-13 11:46:20 浏览: 140
你可以在VUE表格中动态添加一列数据,同时设置该列为只读,以保证新增的数据不可编辑。以下是一个示例代码,供你参考:
```
<template>
<div>
<el-table :data="tableData">
<el-table-column prop="name" label="Name"></el-table-column>
<el-table-column prop="age" label="Age"></el-table-column>
<el-table-column label="New Column">
<template slot-scope="scope">
{{ scope.row.newData }}
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: 'John', age: 25, newData: 'data1' },
{ name: 'Jane', age: 30, newData: 'data2' },
{ name: 'Bob', age: 20, newData: 'data3' }
]
}
},
mounted() {
// 设置新增列为只读
this.$nextTick(() => {
const newColumn = document.querySelectorAll('.el-table__body-wrapper .cell')[2]
newColumn.setAttribute('contenteditable', 'false')
})
}
}
</script>
```
在以上代码中,我们通过`el-table-column`组件来定义表格的列,其中`prop`属性用于指定列所对应的数据字段。我们定义了一个名为`New Column`的新列,并通过`slot-scope`来访问该列所对应的数据。我们还通过`mounted()`钩子函数来将新增的列设置为只读,以防止用户编辑该列数据。
当你往`tableData`数组中添加新的数据时,只需要在其中添加一个名为`newData`的属性即可,该属性的值可以是任意值。例如,你可以像下面这样来添加一条新的数据:
```
this.tableData.push({ name: 'Tom', age: 28, newData: 'data4' })
```
这样,当你再次渲染表格时,新增的数据就会显示在新的一列中,并且该列数据不可编辑。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)