vue穿梭框?qū)崿F(xiàn)上下移動
本文實例為大家分享了vue穿梭框?qū)崿F(xiàn)上下移動的具體代碼,供大家參考,具體內(nèi)容如下
使用elementUI的樹形組件 tree組件
功能需求:
1、左側(cè)的子節(jié)點移動到右側(cè)的表格中
2、右側(cè)選中的內(nèi)容移動到左側(cè)樹中,單一移動和全部移動
3、點擊右側(cè)節(jié)點實現(xiàn)上下移動
首先會遇到的問題可能是如何實現(xiàn)左側(cè)只讓子節(jié)點顯示checkbox,我這邊是根據(jù)后端返回了一個參數(shù)來判斷是否是父節(jié)點(其實只要后端給父節(jié)點加一個nocheck=true就可以)
// setLeftAgency :封裝好的請求接口名稱 setLeftAgency(params).then((res) => { // 當(dāng)返回的code==0時就意味著成功 if (res.data.code == 0) { let { data } = res.data; data.forEach((item) => { //遍歷返回的數(shù)據(jù),如果當(dāng)這個參數(shù)是Item時候就給當(dāng)前這條數(shù)據(jù)加上nocheck=true,這樣就不會顯示checkbox if(item.Type!=='Item'){ item.nocheck=true } // delete item.children; }); this.parentNodes = data; // 把修改好的數(shù)據(jù)放在數(shù)組中再渲染 }
左側(cè)樹結(jié)構(gòu),中間的按鈕和右側(cè)表格(左側(cè)樹結(jié)構(gòu)和表格是封裝好的,直接引入)
<div class="leftTree"> // 這里綁定的onCreated是左側(cè)樹的初始化函數(shù),parentNodes儲存了左側(cè)樹的所有數(shù)據(jù) <ztree :setting="setting" @onCreated = 'onCreated' :parentNodes="parentNodes"></ztree> </div> <div class="centerBtn"> <el-button type="danger" plain icon="el-icon-arrow-right" @click="moveTable"></el-button> <el-button type="danger" plain icon="el-icon-d-arrow-left" @click="moveTreeAll"></el-button> <el-button type="danger" plain icon="el-icon-arrow-left" @click="moveTree"></el-button> <el-button type="danger" plain @click="moveUp(index)">上移</el-button> <el-button type="danger" plain @click="moveDown(index)">下移</el-button> </div> <div class="rightTable"> <table :data.sync="tableData" // 表格接口返回的數(shù)據(jù) ref="personListSettingPage" :loading='vxeLoading' v-model="selectGroups" // 綁定右側(cè)table選中項的數(shù)組 id="personListSettingPage" :showPagination= 'false' :height-full-screen = 'false' @sort-change="sortChange" @checkbox-change="selectChange" // 右側(cè)選中的單選事件 @checkbox-all="selectAll" // 右側(cè)選中項的全選事件 @data-refresh="getTableData()"> // 獲取右側(cè)表格數(shù)據(jù)的函數(shù) <vxe-table-column type="checkbox" width="60" align="center"></vxe-table-column> <table-column field="text" show-overflow="title" title="已選指標(biāo)" filterType='' > </table-column> </table> </div>
用到的參數(shù)
moveDownId:"", //下移時儲存的數(shù)據(jù) moveUpId:"", //上移時遍歷右側(cè)表格數(shù)據(jù)儲存的數(shù)據(jù) selectGroups:[], // 用來存放右側(cè)選中的數(shù)據(jù) tableData:[], // 請求回來后會把左側(cè)的所有數(shù)據(jù)存放在此數(shù)組中 parentNodes:[], //左側(cè)樹的所有數(shù)據(jù) treeObj:"",
左側(cè)樹初始化和右側(cè)表格復(fù)選框選擇事件
// 初始化ztree onCreated(treeObj){ this.treeObj = treeObj let nodes = this.treeObj.getCheckedNodes(true); }, //復(fù)選框事件 selectChange({ checked, records}) { this.selectGroups = records // 把選擇的那條數(shù)據(jù)存儲到數(shù)組中 }, //復(fù)選框全選事件 selectAll({ checked, records }) { this.selectGroups = records },
上移
moveUp(index){ if(this.selectGroups.length>0){ // 判斷右側(cè)是否有選中的項 let goOrnot = true this.selectGroups.find((seItem)=>{ //遍歷右側(cè)tab中選中的項,找到對應(yīng)的id if(seItem.id==this.moveUpId.id){ this.$message.warning(this.moveUpId.text+"此行沒有上移的空間了") goOrnot = false } }) if(goOrnot){ this.tableData.forEach((item,index)=>{ // 遍歷右側(cè)表格所有數(shù)據(jù), this.$set(item,'rowIndex',index) //由于受JavaScript的限制,vue.js不能監(jiān)聽對象屬性的添加和刪除,所以要使用$set或者Object.assign(target, sources),這樣試圖才會更新 }) let flag = true this.selectGroups.forEach((val,index2)=>{ this.tableData.find((itm,ind)=>{ if(val.id==itm.id){ if(itm.rowIndex==0){ // 遍歷右側(cè)選中數(shù)據(jù)和所有數(shù)據(jù)相對比,如果id相同,在判斷剛剛添加的rowIndex屬性值是多少 this.$message.warning(itm.text+"此行沒有上移的空間了") this.moveUpId = itm // 把當(dāng)前這條數(shù)據(jù)存起來 flag = false return }else{ if(flag){ // 此時可以對多條數(shù)據(jù)進行移動了 let changeItem = JSON.parse(JSON.stringify(this.tableData[itm.rowIndex-1])) this.tableData.splice(itm.rowIndex-1,1); this.tableData.splice(itm.rowIndex,0,changeItem) } } } }) }) } }else{ this.$message.warning('請選擇要移動的數(shù)據(jù)') } },
下移
moveDown(index){ if(this.selectGroups.length>0){ let goOrnot = true this.selectGroups.find((seItem)=>{ if(seItem.id==this.moveDownId.id){ this.$message.warning(this.moveDownId.text+"此行沒有下移的空間了") goOrnot = false }else{ this.moveFlag = true } }) if(goOrnot){ this.tableData.forEach((item,index)=>{ this.$set(item,'rowIndex',index) }) let selectData = JSON.parse(JSON.stringify(this.tableData)) let a=[...this.selectGroups] a.reverse().forEach((val,index2)=>{ selectData.find((itm,ind)=>{ if(val.id==itm.id){ if(itm.rowIndex==selectData.length-1){ this.$message.warning(itm.text+"此行沒有下移的空間了") this.moveDownId = itm this.moveFlag = false }else{ if(this.moveFlag){ let changeItem = itm let delIndex=selectData.findIndex(i=>i.id == changeItem.id) this.tableData.splice(delIndex,1); this.tableData.splice(delIndex+1,0,changeItem) this.$refs.personListSettingPage.$refs.Table.setCheckboxRow(this.tableData[itm.rowIndex+1],true) // 給右側(cè)table加了一個ref=personListSettingPage } } } }) }) } }else{ this.$message.warning('請選擇要移動的數(shù)據(jù)') } }
表格移動到樹
/* 移入tree */ moveTree(){ if(this.selectGroups.length>0){ this.selectGroups.forEach(item=>{ this.parentNodes.find(val=>{ if(val.id == item.pid){ /* 添加節(jié)點 */ let node = this.treeObj.getNodeByParam("id", val.id, null); this.treeObj.addNodes(node,item) /* 取消新增節(jié)點的選中狀態(tài) */ let cancelNode = this.treeObj.getNodeByParam("id", item.id, null); this.treeObj.checkNode(cancelNode,false,true); } }) this.tableData.splice(this.tableData.findIndex(val => val.id === item.id), 1) }) }else{ this.$message.warning('請選擇要移動的數(shù)據(jù)') } },
樹移到表格
/* 移入表格 */ moveTable(){ let arr=[] let nodes = this.treeObj.getCheckedNodes(true); if(nodes.length>0){ nodes.forEach(item=>{ this.tableData.find(val=>{ arr.push(val.id) }) if(arr.indexOf(item.id)>-1) return this.$message.error("數(shù)據(jù)重復(fù),請勿重新添加") this.treeObj.removeNode(item) this.tableData.push(this.filterObj(item,["checked","codeSetId",'id','img','infoItemFlag','isMult','itemType','locked','mult','orgCode','pid','sort','syrs','text'])) // 調(diào)用下面的方法,去除多余字段 }) }else{ this.$message.warning('請勾選數(shù)據(jù)') } },
封裝的過濾字段
/* 過濾對象多余字段 */ filterObj(obj, arr) { const result = {} Object.keys(obj).filter((key) => arr.includes(key)).forEach((key) => { result[key] = obj[key] }) return result }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
優(yōu)化Vue template中大量條件選擇v-if的方案分享
本文我們將給大家詳細的講解一下如何優(yōu)化Vue template 中的大量條件選擇v-if,文中通過代碼示例介紹的非常詳細,有詳細的優(yōu)化方案,感興趣的朋友可以參考閱讀下2023-07-07vue 使用Jade模板寫html,stylus寫css的方法
這篇文章主要介紹了vue 使用Jade模板寫html,stylus寫css的方法,文中還給大家提到了使用jade注意事項,需要的朋友可以參考下2018-02-02vue給數(shù)組中對象排序 sort函數(shù)的用法
這篇文章主要介紹了vue給數(shù)組中對象排序 sort函數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Vue學(xué)習(xí)-VueRouter路由基礎(chǔ)
這篇文章主要介紹了Vue學(xué)習(xí)-VueRouter路由基礎(chǔ),路由本質(zhì)上就是超鏈接,xiamian?文章圍繞VueRouter路由的相關(guān)資料展開詳細內(nèi)容,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助2021-12-12