欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > el-tree树添加向下移动按钮,前端界面调整顺序

el-tree树添加向下移动按钮,前端界面调整顺序

2024/10/24 17:22:52 来源:https://blog.csdn.net/dy1717/article/details/140661814  浏览:    关键词:el-tree树添加向下移动按钮,前端界面调整顺序

需求:树上添加向下按钮,再不调用接口的情况下,调整树的顺序结构

遇到的问题:第一次点击更新的,数据和视图是调整好的,再次点击页面调整顺序,只有数据被调整了,视图没有发生改变。

     <el-treeref="tree":data="treeData"highlight-currentnode-key="id":props="props":default-expanded-keys="showList"@node-click="clicktree"><span slot-scope="{node, data }" class="custom-tree-node"><img v-if="data.seq_img" :src="hanleImgShow(data?.seq_img)" width="20" height="20"><span style="display:inline-block;padding-left:5px;">{{ data?.seq_name_chn }}</span><el-button v-if="data.seq_img" type="text" icon="el-icon-arrow-down" @click="moveDown(node,data,data?.seq_num)" /></span></el-tree>
    moveDown(node, data, num) {const seqNum = Number(num)const childrenLength = node.parent.data.children.lengthconst childrenList = node?.parent?.data?.children//     seqNum  这个值去判断if (childrenLength == seqNum) {return this.$message.warning('不可以下移')}const treeList = this.treeCreatedData // 存放数据的, 这个里面的children是存放的序数据const targetIndex = childrenList.findIndex(item => item.seq_num == num)if (targetIndex !== -1) {this.moveItemAndUpdateSeqNum(childrenList, targetIndex) // 将第三项移动到第一位}},moveItemAndUpdateSeqNum(list, index) {if (index < list.length - 1 && index >= 0) {const temp = list[index]list[index] = list[index + 1]list[index + 1 ] = tempthis.$nextTick(() => {for (let i = index; i <= index + 1; i++) {list[i].seq_num = (i + 1).toString()}console.log(list, 'list382')this.treeCreatedData[0].children[0].children = listthis.treeData = this.treeCreatedDatathis.$forceUpdate() })}},

 这样写,只会更新第一次的视图,后续点击 视图没有发生改变;最后找到的方法是使用ref找树的方法 去修改。

 moveItemAndUpdateSeqNum(list, index) {if (index < list.length - 1 && index >= 0) {const temp = list[index]list[index] = list[index + 1]list[index + 1 ] = tempthis.$nextTick(() => {for (let i = index; i <= index + 1; i++) {list[i].seq_num = (i + 1).toString()}console.log(list, 'list382')this.treeCreatedData[0].children[0].children = listthis.treeData = this.treeCreatedDatathis.$refs.tree.root.setData(this.treeData) //使用这个 去更新树节点的数据})}},

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com