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


    1. <script setup>
    2. import { ref, reactive, watch, nextTick } from 'vue';
    3. import { ElTree, ElCheckbox, ElInput } from 'element-plus';
    4. const searchInput = ref('');
    5. const checkedKeys = ref([]);
    6. const treeData = reactive([
    7. {
    8. key: 1,
    9. label: '选项1',
    10. children: [
    11. { key: 11, label: '子选项11' },
    12. { key: 12, label: '子选项12' },
    13. { key: 13, label: '子选项13' },
    14. ],
    15. },
    16. {
    17. key: 2,
    18. label: '选项2',
    19. children: [
    20. { key: 21, label: '子选项21' },
    21. { key: 22, label: '子选项22' },
    22. { key: 23, label: '子选项23' },
    23. ],
    24. },
    25. ]);
    26. const selectAll = ref(false);
    27. const treeRef = ref(null);
    28. watch(searchInput, (val) => {
    29. treeRef.value.filter(val);
    30. });
    31. const filterNode = (value, data) => {
    32. if (!value) return true;
    33. return data.label.includes(value);
    34. };
    35. const clearSearch = () => {
    36. searchInput.value = '';
    37. };
    38. const handleSelectAll = (checked) => {
    39. if (checked) {
    40. checkedKeys.value = getAllNodeKeys();
    41. } else {
    42. checkedKeys.value = [];
    43. treeRef.value.setCheckedKeys([]);
    44. }
    45. };
    46. const getAllNodeKeys = () => {
    47. const keys = [];
    48. const traverse = (nodes) => {
    49. for (const node of nodes) {
    50. keys.push(node.key);
    51. if (node.children && node.children.length > 0) {
    52. traverse(node.children);
    53. }
    54. }
    55. };
    56. traverse(treeData);
    57. return keys;
    58. };
    59. const handleCheckChange = (data) => {
    60. checkedKeys.value = data.checkedKeys;
    61. // 获取树节点选中的id
    62. console.log(treeRef.value.getCheckedKeys())
    63. nextTick(() => {
    64. if (treeRef.value) {
    65. const nodes = treeRef.value.root.childNodes;
    66. const allChecked = nodes.every((node) => node.checked);
    67. selectAll.value = allChecked;
    68. }
    69. });
    70. };
    71. script>
    72. <style scoped>
    73. .half-transfer {
    74. margin-top: 20px;
    75. margin-left: 20px;
    76. width: 335px;
    77. height: 260px;
    78. background: #fff;
    79. padding: 20px;
    80. border: 1px solid #dcdfe6;
    81. border-radius: 4px;
    82. }
    83. .el-transfer-panel {
    84. display: flex;
    85. flex-direction: column;
    86. height: 100%;
    87. }
    88. .el-transfer__list {
    89. overflow-y: auto;
    90. border-radius: 4px;
    91. margin-top: 8px;
    92. }
    93. .el-transfer__list .el-checkbox-group {
    94. padding: 10px;
    95. }
    96. .el-transfer__list .el-checkbox {
    97. display: block;
    98. margin-bottom: 5px;
    99. line-height: 24px;
    100. }
    101. .el-transfer__list .el-checkbox:last-child {
    102. margin-bottom: 0;
    103. }
    104. .el-transfer__list .el-scrollbar {
    105. background-color: #f5f7fa;
    106. }
    107. style>

  • 相关阅读:
    CREAL访谈:将推出光场验光方案,AR眼镜仍是长期目标
    「项目进度管理」如何编制有效的进度计划?
    猿创征文|【深度学习前沿应用】文本审核
    2022年09月 C/C++(八级)真题解析#中国电子学会#全国青少年软件编程等级考试
    MySQL中的SHOW FULL PROCESSLIST命令
    信息安全实验三 :PGP邮件加密软件的使用
    Docker搭建私有镜像仓库及推送、拉取私服镜像
    Golang数据结构和算法
    Neo4j 宣布下一代图数据平台 Neo4j 5 上线
    达梦数据库常见日志错误与解决方法
  • 原文地址:https://blog.csdn.net/yf18040578780/article/details/132584989