国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue+element-ui實現表格編輯的三種實現方式

2024-05-06 15:28:19
字體:
來源:轉載
供稿:網友

1、表格內部顯示和編輯切換

這種方式就是利用兩個標簽顯示隱藏來實現,我們這里用input和span,正常用span將數據顯示,點擊編輯時,將span隱藏,顯示input進行編輯。選中當前行我們可以通過slot-scope中的index去實現,在控制顯示隱藏的屬性上綁定index就可以選中當前行了,如showEdit[index]。

頁面結構代碼:

<el-table :data="tableData" tooltip-effect="dark" style="width: 100%" header-align="center"> <el-table-column width="50" header-align="center">  <template slot-scope="{row,$index}">   <span>{{$index + 1}}</span>  </template> </el-table-column> <el-table-column label="名稱" prop="Name" width="300" header-align="center">  <template slot-scope="{row,$index}">   <input class="edit-cell" v-if="showEdit[$index]" v-model="row.Name">   <span v-if="!showEdit[$index]">{{row.Name}}</span>  </template> </el-table-column> <el-table-column  fixed="right"  label="操作"  width="100"  header-align="center">  <template slot-scope="{row,$index}">   <el-button type="text" size="small"  @click.native="handleUpdate($index, row)"  v-if="showBtn[$index]">更新</el-button>   <el-button type="text" size="small"  @click.native="handleCancel($index, row)"  v-if="showBtn[$index]">取消</el-button>   <el-button type="text" size="small"  @click.native="handleEdit($index, row)"  v-if="!showBtn[$index]">編輯</el-button>   <el-button type="text" size="small"  @click.native="handleDelete($index, row)"  v-if="!showBtn[$index]">刪除</el-button>  </template> </el-table-column></el-table>

初始化data:

data() { return {  showEdit: [], //顯示編輯框  showBtn: [],  showBtnOrdinary: true }}

實現方法:

//點擊編輯handleEdit(index, row) { this.showEdit[index] = true; this.showBtn[index] = true; this.$set(this.showEdit,row,true) this.$set(this.showBtn,row,true)},//取消編輯handelCancel(index, row) { this.getContentList(); this.showEdit[index] = false; this.showBtn[index] = false;   },//點擊更新handleUpdate(formName) {},//點擊刪除handleDelete(index, row) {},

初始化的時候最好給數組遍歷賦值

for(var i = 0; i < 100; i ++) { this.showEdit[i] = false; this.showBtn[i] = false; this.$set(vm.showEdit[i], false); this.$set(vm.showBtn[i], false);}

另外還可以給row自身添加一個屬性,通過row.showEdit來控制每一行的編輯。上面說的這些在我的開發環境實現一點問題都沒有,但是測試出來了很多bug(沒反應、點擊第一次第二次沒反應等),后來又查詢了一下vue的官方文檔“異步隊列更新”,可能需要加一個Vue.nextTick(callback)。項目比較緊換了另外一種實現方式。有興趣的小伙伴可以看看官方文檔:https://cn.vuejs.org/v2/guide/reactivity.html

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 项城市| 大丰市| 阜新| 博爱县| 衡水市| 铜川市| 新营市| 宜黄县| 勐海县| 安平县| 射阳县| 磐石市| 亳州市| 铜鼓县| 涟水县| 馆陶县| 新竹市| 浦县| 兴文县| 渝中区| 乌鲁木齐市| 年辖:市辖区| 晋州市| 平武县| 东光县| 铜陵市| 图木舒克市| 清流县| 湖北省| 临汾市| 修水县| 同心县| 大埔区| 武山县| 丘北县| 安西县| 平和县| 丹东市| 普兰店市| 锡林浩特市| 高雄市|