您好,登錄后才能下訂單哦!
這篇“vue中怎么實現可編輯table及其中加入下拉選項”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“vue中怎么實現可編輯table及其中加入下拉選項”文章吧。
<template> <div> <el-table :data="tabledatas" border> <el-table-column label="姓名"> <template slot-scope="scope"> <el-input placeholder="請輸入內容" v-show="scope.row.show" v-model="scope.row.name"></el-input> <span v-show="!scope.row.show">{{scope.row.name}}</span> </template> </el-table-column> <el-table-column label="年齡"> <template slot-scope="scope"> <el-input placeholder="請輸入內容" v-show="scope.row.show" v-model="scope.row.age"></el-input> <span v-show="!scope.row.show">{{scope.row.age}}</span> </template> </el-table-column> <el-table-column label="地址"> <template slot-scope="scope"> <el-input placeholder="請輸入內容" v-show="scope.row.show" v-model="scope.row.address"></el-input> <span v-show="!scope.row.show">{{scope.row.address}}</span> </template> </el-table-column> <el-table-column label="學籍"> <template slot-scope="scope"> <span v-show="!scope.row.show">{{scope.row.stu}}</span> <el-select v-model="scope.row.stu" placeholder="請選擇" v-show="scope.row.show" > <el-option v-for="item in options" :key="item.stu" :label="item.stu" :value="item.stu"> </el-option> </el-select> </template> </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button @click="scope.row.show =true" >編輯</el-button> <el-button @click="scope.row.show =false">保存</el-button> </template> </el-table-column> </el-table> </div> </template>
<script> export default { data(){ return { options: [{ value: '選項1', stu: '初中' }, { value: '選項2', stu: '高中' }, { value: '選項3', stu: '大專' }, { value: '選項4', stu: '本科' }, { value: '選項5', stu: '博士' }], value: '', tabledatas: [ { name: '李一', age: '19',address:"寧波",stu:"本科",show:false}, { name: '郭明', age: '23',address:"四川",stu:"本科",show:false}, { name: '天天', age: '12',address:"海南",stu:"初中",show:false}, { name: '隆', age: '40',address:"上海",stu:"博士",show:false}, ], } } </script>
可以通過設置js里的show:true讓該行處于默認編輯狀態
出來效果圖
使用:
<template slot="header" slot-scope="scope"> <el-dropdown trigger="click" @command = "handleCommand"> <span>類型</span> <el-dropdown-menu slot="dropdown"> <el-radio-group v-model="sx">//這里,會出現一個bug,下文有解決辦法 <el-dropdown-item command="屬性0"><el-radio label="0">屬性0</el-radio> </el-dropdown-item> <el-dropdown-item command="屬性1"><el-radio label="1">屬性1</el-radio> </el-dropdown-item> <el-dropdown-item command="屬性2"><el-radio label="2">屬性2</el-radio> </el-dropdown-item> <el-dropdown-item command="屬性3"><el-radio label="3">屬性3</el-radio> </el-dropdown-item> <el-dropdown-item command="屬性4"><el-radio label="4">屬性4</el-radio> </el-dropdown-item> <el-dropdown-item command="屬性5"><el-radio label="5">屬性5</el-radio> </el-dropdown-item> <el-dropdown-item command="屬性6"><el-radio label="6">屬性6</el-radio> </el-dropdown-item> </el-radio-group> </el-dropdown-menu> </el-dropdown> </template> <template slot-scope="scope">(表中元素)</template>
(當時沒使用handleCommand方法做緩沖,在刷新時,第一次刷新不會賦值,第二次刷新會得到上次刷新的值。)
handleCommand(command) { if(command == '屬性0' ){ this.sx= '0' } else if (command === '屬性1') { this.sx = '1' } else if( command === '屬性2') { this.sx = '2' } else if (command === '屬性3') { this.sx = '3' } else if (command === '屬性4') { this.sx = '4' } else if( command === '屬性5') { this.sx = '5' } else if (command === '屬性6') { this.sx = '6' } this.刷新方法; },
但是在使用過程中,點擊下拉框中數據時,會出現執行兩次handleCommand()方法的情況。通過一天的詢問與查找,得到解決辦法。
問題出現在<el-radio>標簽上,當點擊框中數據時,數據響應一次handleCommand()方法,<el-radio>也會響應一次handleCommand()方法。
所以,應該去掉<el-radio>標簽與<el-radio-group>標簽。
<template slot="header" slot-scope="scope"> <el-dropdown trigger="click" @command = "handleCommand"> <span>類型</span> <el-dropdown-menu slot="dropdown"> <el-dropdown-item command="屬性0">屬性0</el-dropdown-item> <el-dropdown-item command="屬性1">屬性1</el-dropdown-item> <el-dropdown-item command="屬性2">屬性2</el-dropdown-item> <el-dropdown-item command="屬性3">屬性3</el-dropdown-item> <el-dropdown-item command="屬性4">屬性4</el-dropdown-item> <el-dropdown-item command="屬性5">屬性5</el-dropdown-item> <el-dropdown-item command="屬性6">屬性6</el-dropdown-item> </el-dropdown-menu> </el-dropdown> </template> <template slot-scope="scope">(表中元素)</template>
以上就是關于“vue中怎么實現可編輯table及其中加入下拉選項”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。