树多选搜索查询,搜索后选中状态仍保留

javascript 复制代码
<template>
  <div class="half-transfer">
    <div class="el-transfer-panel">
      <div>
        <el-checkbox v-model="selectAll" @change="handleSelectAll">全部</el-checkbox>
      </div>
      <el-input v-model="searchInput" placeholder="请输入搜索内容" clearable @clear="clearSearch"></el-input>
      <div class="el-transfer__list">
        <el-tree
          ref="treeRef"
          :data="treeData"
          node-key="key"
          :default-expand-all="true"
          show-checkbox
          :default-checked-keys="checkedKeys"
          :filter-node-method="filterNode"
          @check="handleCheckChange"
        ></el-tree>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, watch, nextTick } from 'vue';
import { ElTree, ElCheckbox, ElInput } from 'element-plus';

const searchInput = ref('');
const checkedKeys = ref([]);
const treeData = reactive([
  {
    key: 1,
    label: '选项1',
    children: [
      { key: 11, label: '子选项11' },
      { key: 12, label: '子选项12' },
      { key: 13, label: '子选项13' },
    ],
  },
  {
    key: 2,
    label: '选项2',
    children: [
      { key: 21, label: '子选项21' },
      { key: 22, label: '子选项22' },
      { key: 23, label: '子选项23' },
    ],
  },
]);
const selectAll = ref(false);
const treeRef = ref(null);

watch(searchInput, (val) => {
  treeRef.value.filter(val);
});

const filterNode = (value, data) => {
  if (!value) return true;
  return data.label.includes(value);
};

const clearSearch = () => {
  searchInput.value = '';
};

const handleSelectAll = (checked) => {
  if (checked) {
    checkedKeys.value = getAllNodeKeys();
  } else {
    checkedKeys.value = [];
    treeRef.value.setCheckedKeys([]);
  }
};

const getAllNodeKeys = () => {
  const keys = [];
  const traverse = (nodes) => {
    for (const node of nodes) {
      keys.push(node.key);
      if (node.children && node.children.length > 0) {
        traverse(node.children);
      }
    }
  };
  traverse(treeData);
  return keys;
};

const handleCheckChange = (data) => {
  checkedKeys.value = data.checkedKeys;
  // 获取树节点选中的id
  console.log(treeRef.value.getCheckedKeys())
  nextTick(() => {
    if (treeRef.value) {
      const nodes = treeRef.value.root.childNodes;
      const allChecked = nodes.every((node) => node.checked);
      selectAll.value = allChecked;
    }
  });
};
</script>

<style scoped>
.half-transfer {
  margin-top: 20px;
  margin-left: 20px;
  width: 335px;
  height: 260px;
  background: #fff;
  padding: 20px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

.el-transfer-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.el-transfer__list {
  overflow-y: auto;
  border-radius: 4px;
  margin-top: 8px;
}

.el-transfer__list .el-checkbox-group {
  padding: 10px;
}

.el-transfer__list .el-checkbox {
  display: block;
  margin-bottom: 5px;
  line-height: 24px;
}

.el-transfer__list .el-checkbox:last-child {
  margin-bottom: 0;
}

.el-transfer__list .el-scrollbar {
  background-color: #f5f7fa;
}
</style>
相关推荐
CodeChampion19 小时前
60.基于SSM的个人网站的设计与实现(项目 + 论文)
java·vue.js·mysql·spring·elementui·node.js·mybatis
_不是惊风1 天前
el-table合并表头,表头第一个添加斜线
vue.js·elementui
毛毛三由1 天前
表单校验记录
前端·vue.js·elementui
_Feliz2 天前
vue2实现word在线预览
前端·javascript·vue.js·elementui·vue-office
Qlittleboy2 天前
vue的elementUI 给输入框绑定enter事件失效
前端·vue.js·elementui
Cachel wood3 天前
Vue.js前端框架教程14:Vue组件el-popover
前端·javascript·vue.js·python·elementui·django·前端框架
小阳生煎4 天前
el-date-picker筛选时间日期选择范围
vue.js·elementui
喜陈4 天前
elementui在任意页面点击消息,弹出消息对应页面处理弹窗
前端·javascript·elementui
肉肉心很软4 天前
本地项目显示正常,打包部署后ElementUI重点饿图标全部显示异常为小方框
前端·elementui
大得3694 天前
模仿elementui的Table,实现思路
前端·chrome·elementui