vue穿梭框?qū)崿F(xiàn)上下移動(dòng)
本文實(shí)例為大家分享了vue穿梭框?qū)崿F(xiàn)上下移動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
使用elementUI的樹(shù)形組件 tree組件

功能需求:
1、左側(cè)的子節(jié)點(diǎn)移動(dòng)到右側(cè)的表格中
2、右側(cè)選中的內(nèi)容移動(dòng)到左側(cè)樹(shù)中,單一移動(dòng)和全部移動(dòng)
3、點(diǎn)擊右側(cè)節(jié)點(diǎn)實(shí)現(xiàn)上下移動(dòng)
首先會(huì)遇到的問(wèn)題可能是如何實(shí)現(xiàn)左側(cè)只讓子節(jié)點(diǎn)顯示checkbox,我這邊是根據(jù)后端返回了一個(gè)參數(shù)來(lái)判斷是否是父節(jié)點(diǎn)(其實(shí)只要后端給父節(jié)點(diǎn)加一個(gè)nocheck=true就可以)
// setLeftAgency :封裝好的請(qǐng)求接口名稱(chēng)
setLeftAgency(params).then((res) => { // 當(dāng)返回的code==0時(shí)就意味著成功
if (res.data.code == 0) {
let { data } = res.data;
data.forEach((item) => { //遍歷返回的數(shù)據(jù),如果當(dāng)這個(gè)參數(shù)是Item時(shí)候就給當(dāng)前這條數(shù)據(jù)加上nocheck=true,這樣就不會(huì)顯示checkbox
if(item.Type!=='Item'){
item.nocheck=true
}
// delete item.children;
});
this.parentNodes = data; // 把修改好的數(shù)據(jù)放在數(shù)組中再渲染
}
左側(cè)樹(shù)結(jié)構(gòu),中間的按鈕和右側(cè)表格(左側(cè)樹(shù)結(jié)構(gòu)和表格是封裝好的,直接引入)
<div class="leftTree"> // 這里綁定的onCreated是左側(cè)樹(shù)的初始化函數(shù),parentNodes儲(chǔ)存了左側(cè)樹(shù)的所有數(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選中項(xiàng)的數(shù)組 id="personListSettingPage" :showPagination= 'false' :height-full-screen = 'false' @sort-change="sortChange" @checkbox-change="selectChange" // 右側(cè)選中的單選事件 @checkbox-all="selectAll" // 右側(cè)選中項(xiàng)的全選事件 @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í)儲(chǔ)存的數(shù)據(jù) moveUpId:"", //上移時(shí)遍歷右側(cè)表格數(shù)據(jù)儲(chǔ)存的數(shù)據(jù) selectGroups:[], // 用來(lái)存放右側(cè)選中的數(shù)據(jù) tableData:[], // 請(qǐng)求回來(lái)后會(huì)把左側(cè)的所有數(shù)據(jù)存放在此數(shù)組中 parentNodes:[], //左側(cè)樹(shù)的所有數(shù)據(jù) treeObj:"",
左側(cè)樹(shù)初始化和右側(cè)表格復(fù)選框選擇事件
// 初始化ztree
onCreated(treeObj){
this.treeObj = treeObj
let nodes = this.treeObj.getCheckedNodes(true);
},
//復(fù)選框事件
selectChange({ checked, records}) {
this.selectGroups = records // 把選擇的那條數(shù)據(jù)存儲(chǔ)到數(shù)組中
},
//復(fù)選框全選事件
selectAll({ checked, records }) {
this.selectGroups = records
},
上移
moveUp(index){
if(this.selectGroups.length>0){ // 判斷右側(cè)是否有選中的項(xiàng)
let goOrnot = true
this.selectGroups.find((seItem)=>{ //遍歷右側(cè)tab中選中的項(xiàng),找到對(duì)應(yīng)的id
if(seItem.id==this.moveUpId.id){
this.$message.warning(this.moveUpId.text+"此行沒(méi)有上移的空間了")
goOrnot = false
}
})
if(goOrnot){
this.tableData.forEach((item,index)=>{ // 遍歷右側(cè)表格所有數(shù)據(jù),
this.$set(item,'rowIndex',index) //由于受JavaScript的限制,vue.js不能監(jiān)聽(tīng)對(duì)象屬性的添加和刪除,所以要使用$set或者Object.assign(target, sources),這樣試圖才會(huì)更新
})
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ù)相對(duì)比,如果id相同,在判斷剛剛添加的rowIndex屬性值是多少
this.$message.warning(itm.text+"此行沒(méi)有上移的空間了")
this.moveUpId = itm // 把當(dāng)前這條數(shù)據(jù)存起來(lái)
flag = false
return
}else{
if(flag){ // 此時(shí)可以對(duì)多條數(shù)據(jù)進(jìn)行移動(dòng)了
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('請(qǐng)選擇要移動(dòng)的數(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+"此行沒(méi)有下移的空間了")
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+"此行沒(méi)有下移的空間了")
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加了一個(gè)ref=personListSettingPage
}
}
}
})
})
}
}else{
this.$message.warning('請(qǐng)選擇要移動(dòng)的數(shù)據(jù)')
}
}
表格移動(dòng)到樹(shù)
/* 移入tree */
moveTree(){
if(this.selectGroups.length>0){
this.selectGroups.forEach(item=>{
this.parentNodes.find(val=>{
if(val.id == item.pid){
/* 添加節(jié)點(diǎn) */
let node = this.treeObj.getNodeByParam("id", val.id, null);
this.treeObj.addNodes(node,item)
/* 取消新增節(jié)點(diǎn)的選中狀態(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('請(qǐng)選擇要移動(dòng)的數(shù)據(jù)')
}
},
樹(shù)移到表格
/* 移入表格 */
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ù),請(qǐng)勿重新添加")
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('請(qǐng)勾選數(shù)據(jù)')
}
},
封裝的過(guò)濾字段
/* 過(guò)濾對(duì)象多余字段 */
filterObj(obj, arr) {
const result = {}
Object.keys(obj).filter((key) => arr.includes(key)).forEach((key) => {
result[key] = obj[key]
})
return result
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
優(yōu)化Vue template中大量條件選擇v-if的方案分享
本文我們將給大家詳細(xì)的講解一下如何優(yōu)化Vue template 中的大量條件選擇v-if,文中通過(guò)代碼示例介紹的非常詳細(xì),有詳細(xì)的優(yōu)化方案,感興趣的朋友可以參考閱讀下2023-07-07
vue 使用Jade模板寫(xiě)html,stylus寫(xiě)css的方法
這篇文章主要介紹了vue 使用Jade模板寫(xiě)html,stylus寫(xiě)css的方法,文中還給大家提到了使用jade注意事項(xiàng),需要的朋友可以參考下2018-02-02
Vite版本更新檢查實(shí)現(xiàn)頁(yè)面自動(dòng)刷新的解決思路
這篇文章主要給大家介紹了關(guān)于Vite版本更新檢查實(shí)現(xiàn)頁(yè)面自動(dòng)刷新的解決思路,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02
vue給數(shù)組中對(duì)象排序 sort函數(shù)的用法
這篇文章主要介紹了vue給數(shù)組中對(duì)象排序 sort函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
淺談在不使用ssr的情況下解決Vue單頁(yè)面SEO問(wèn)題(2)
這篇文章主要介紹了淺談在不使用ssr的情況下解決Vue單頁(yè)面SEO問(wèn)題(2),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Vue學(xué)習(xí)-VueRouter路由基礎(chǔ)
這篇文章主要介紹了Vue學(xué)習(xí)-VueRouter路由基礎(chǔ),路由本質(zhì)上就是超鏈接,xiamian?文章圍繞VueRouter路由的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2021-12-12
Vue3中依賴(lài)注入provide、inject的使用
這篇文章主要介紹了Vue3中依賴(lài)注入provide、inject的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

