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


    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>

  • 相关阅读:
    Git命令总结-常用-后续使用频繁的再添加~
    前端入门到入土?
    通过tushare接口完成股票的实际交易的方法有哪些?
    电子学会 2023年3月 青少年软件编程Python编程等级考试三级真题解析(选择题+判断题+编程题)
    二叉树的重建问题
    SAP 标准表
    【面试刷题】——C++的特点简单说明
    mac tcp实现客户端与服务端进行图像传输及处理
    [Python进阶] 监听键鼠:Pynput
    LeetCode53. 最大子数组和
  • 原文地址:https://blog.csdn.net/yf18040578780/article/details/132584989