vue3左树的全选和反选

html 复制代码
 <el-input v-model="filterText" placeholder="" style="width: 48%"/>
                  <el-button type="primary" @click="handleSearch" class="ml-2">查找</el-button>
                  <el-radio-group v-model="form.choice" @change="handleCheckAll">
                    <el-radio label="all" class="ml-2">全选</el-radio>
                    <el-radio label="invert">反选</el-radio>
                  </el-radio-group>
                  <el-tree
                    ref="treeRef"
                    class="filter-tree"
                    :data="treeData"
                    :props="defaultProps"
                    show-checkbox
                    node-key="id"
                    :filter-node-method="filterNode"
                  >
                    <template #default="{ node, data }">
                      <div style="display: flex;">
                        <span style="margin-right: 8px">{{ node.label }}</span>
                        <span>
<!--            <a @click="append(data)"> Append </a>-->
                          <!--            <el-link  @click="remove(node, data)"><el-icon><Delete /></el-icon></el-link>-->
          </span>
                        <!--          <IconifyIconOffline :icon="Del" class="relink-btn-icon" />-->
                      </div>
                    </template>
                  </el-tree>
html 复制代码
const filterText = ref("");

const treeRef = ref<InstanceType<typeof ElTree>>();
interface Tree {
  id: number
  label: string
  children?: Tree[]
  checked?: boolean;
}

const defaultProps = {
  children: "children",
  label: "label"
};
const filterNode = (value: string, treeData: Tree) => {
  if (!value) return true;
  return treeData.label.includes(value);
};
const handleSearch = () => {
  treeRef.value!.filter(filterText.value);
}
const handleCheckAll = (value: string) => {
  if (value === 'all') {
    for (let i = 0; i < treeData.value.length; i++) {
      if (treeRef.value.getNode(treeData.value[i]).disabled == false) {
        treeRef.value.setChecked(treeData.value[i].id, true, true);
      }
    }
  } else {
    treeRef.value.setCheckedKeys([])
  }
}
相关推荐
FRing039 小时前
vue3干货总结3(defineOptions(Vue3.3)、defineModel(Vue3.4)、Pinia)
开发语言·前端·vue.js·vue3
inksci11 小时前
用飞帆做了个小网页
前端·javascript·css·html
zhensherlock12 小时前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js
多加点辣也没关系15 小时前
JavaScript|第40章:JavaScript 工程化实践
开发语言·javascript·ecmascript
彩虹暴龙兽17 小时前
JavaScript 预编译机制与执行上下文构建分析
javascript
tryCbest17 小时前
Vue3——第 22 篇:Pinia——新一代状态管理(上)
前端·javascript·vue.js
bai27317 小时前
"👨‍👩‍👧‍👦" 的 length 是 11:我把自己写的字数统计脚本拖出来审了一遍
前端·javascript
bai27318 小时前
同一个 1.005,toFixed 给 1.00、Math.round 给 1、Intl 给 1.01:999 个三分位小数的实测
javascript·后端
爱编程的1718 小时前
一篇讲透 JS 对象:数据类型、原型链与包装类
javascript
Highcharts.js1 天前
Highcharts漏斗图表使用配置指南
java·开发语言·javascript·信息可视化·highcharts·漏斗图