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

功能需求:
1、左側(cè)的子節(jié)點(diǎn)移動(dòng)到右側(cè)的表格中
2、右側(cè)選中的內(nèi)容移動(dòng)到左側(cè)樹中,單一移動(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)求接口名稱
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è)樹結(jié)構(gòu),中間的按鈕和右側(cè)表格(左側(cè)樹結(jié)構(gòu)和表格是封裝好的,直接引入)
<div class="leftTree"> // 這里綁定的onCreated是左側(cè)樹的初始化函數(shù),parentNodes儲(chǔ)存了左側(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選中項(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ù)據(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ù)存儲(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)到樹
/* 移入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ù)')
}
},
樹移到表格
/* 移入表格 */
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)文章
vue子組件設(shè)計(jì)provide和inject理解使用
這篇文章主要為大家介紹了vue子組件設(shè)計(jì)provide和inject理解及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
VUE2 前端實(shí)現(xiàn) 靜態(tài)二級(jí)省市聯(lián)動(dòng)選擇select的示例
下面小編就為大家分享一篇VUE2 前端實(shí)現(xiàn) 靜態(tài)二級(jí)省市聯(lián)動(dòng)選擇select的示例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
詳解Vue2 無(wú)限級(jí)分類(添加,刪除,修改)
本篇文章主要介紹了詳解Vue2 無(wú)限級(jí)分類(添加,刪除,修改) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Vue使用富文本編輯器Vue-Quill-Editor(含圖片自定義上傳服務(wù)、清除復(fù)制粘貼樣式等)
這篇文章主要介紹了Vue項(xiàng)目中使用富文本編輯器Vue-Quill-Editor(含圖片自定義上傳服務(wù)、清除復(fù)制粘貼樣式等)的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05

