日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ElementUI中的el-table怎样实现每一列显示的是控件并能动态实现双向数据绑定

發布時間:2025/3/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ElementUI中的el-table怎样实现每一列显示的是控件并能动态实现双向数据绑定 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

要實現在ElementUI的表格中每一列展示的不是數據而是控件。效果如下

?

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

普通表格官方示例代碼

<template><el-table:data="tableData"stripestyle="width: 100%"><el-table-columnprop="date"label="日期"width="180"></el-table-column><el-table-columnprop="name"label="姓名"width="180"></el-table-column><el-table-columnprop="address"label="地址"></el-table-column></el-table> </template><script>export default {data() {return {tableData: [{date: '2016-05-02',name: '王小虎',address: '上海市普陀區金沙江路 1518 弄'}, {date: '2016-05-04',name: '王小虎',address: '上海市普陀區金沙江路 1517 弄'}, {date: '2016-05-01',name: '王小虎',address: '上海市普陀區金沙江路 1519 弄'}, {date: '2016-05-03',name: '王小虎',address: '上海市普陀區金沙江路 1516 弄'}]}}} </script>要實現每列中使用的是控件的話可以使用template來實現<el-table-column label="指定天數" align="center" prop="ts" width="150"><template slot-scope="scope"><el-selectclearable@change="changezdts(scope.row)"v-model="bcglXiangXiList[scope.row.xh-1].ts"><el-optionv-for="dict in zdtsOptions":key="dict.dictValue":label="dict.dictLabel":value="dict.dictValue"/></el-select></template></el-table-column>

這里是在此列使用下拉框控件作為模板

這里要添加slot-scope屬性。添加這個屬性可以在后面獲取到某一行。

完整示例代碼

??????? <el-tablev-loading="loading":data="bcglXiangXiList":row-class-name="rowClassName"@selection-change="handleDetailSelectionChange"ref="tb"><el-table-column type="selection" width="30" align="center" /><el-table-column label="序號" align="center" prop="xh"? width="50"></el-table-column><el-table-column label="開始時間/最早時間-結束時間/最晚時間" width="250" prop="sjfw"><template slot-scope="scope"><el-time-pickeris-rangeformat="HH:mm"value-format="HH:mm":style="{width: '100%'}"start-placeholder="開始時間"end-placeholder="結束時間"range-separator="至"clearable@change="changesjfw(scope.row)"v-model="bcglXiangXiList[scope.row.xh-1].sjfw"></el-time-picker></template></el-table-column><el-table-column label="指定天數" align="center" prop="ts" width="150"><template slot-scope="scope"><el-selectclearable@change="changezdts(scope.row)"v-model="bcglXiangXiList[scope.row.xh-1].ts"><el-optionv-for="dict in zdtsOptions":key="dict.dictValue":label="dict.dictLabel":value="dict.dictValue"/></el-select></template></el-table-column><el-table-column label="打卡地點" align="center" prop="dkdd" width="150"><template slot-scope="scope"><el-selectclearable@change="changedkdd(scope.row)"v-model="bcglXiangXiList[scope.row.xh-1].dkdd"><el-optionv-for="dict in dkddOptions":key="dict.dictValue":label="dict.dictLabel":value="dict.dictValue"/></el-select></template></el-table-column><el-table-column label="最小井下累計時間-最大井下累計時間" width="250" prop="jxsjfw"><template slot-scope="scope"><el-time-pickeris-rangeformat="HH:mm"value-format="HH:mm":style="{width: '100%'}"start-placeholder="開始時間"end-placeholder="結束時間"range-separator="至"clearable@change="changejxsjfw(scope.row)"v-model="bcglXiangXiList[scope.row.xh-1].jxsjfw"></el-time-picker></template></el-table-column></el-table>

在使用可控件之后再使用v-model進行雙向數據綁定時就是動態數據綁定了。

這里首先設置el-table的數據源為 一個對象數組

????????? :data="bcglXiangXiList"

然后添加一個單選框

<el-table-column type="selection" width="30" align="center" />

通過@selection-change="handleDetailSelectionChange"

設置其勾選事件

??? //單選框選中數據handleDetailSelectionChange(selection) {if (selection.length > 1) {this.$refs.tb.clearSelection();this.$refs.tb.toggleRowSelection(selection.pop());} else {this.checkedDetail = selection;}},

實現單選并保存選中項。

前提要設置el-table的ref="tb"并且this.checkedDetail 需要提前聲明

? data() {return {//選中的從表數據checkedDetail: [],

然后添加了一列序號用來保存行的索引號,通過設置el-table的

?:row-class-name= "rowClassName"

來實現

??? rowClassName({ row, rowIndex }) {row.xh = rowIndex + 1;},

然后怎樣對每個控件進行v-model數據綁定,通過

? v-model="bcglXiangXiList[scope.row.xh-1].ts"

使用scope.row可以獲取到當前行對象。

通過scope.row.xh可以獲取到當前行的xh字段屬性,因為之前設置了xh為行的索引+1

而數組的索引從0開始,所以這里是xh-1

??????????? <template slot-scope="scope"><el-selectclearable@change="changezdts(scope.row)"v-model="bcglXiangXiList[scope.row.xh-1].ts"><el-optionv-for="dict in zdtsOptions":key="dict.dictValue":label="dict.dictLabel":value="dict.dictValue"/></el-select></template>

?

總結

以上是生活随笔為你收集整理的ElementUI中的el-table怎样实现每一列显示的是控件并能动态实现双向数据绑定的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。