el-tree:this.$refs.tree.setCheckedKeys([])清空失败

时间: 2024-04-25 12:21:08 浏览: 157
问题解决的方法可以根据引用\[1\]、引用\[2\]和引用\[3\]中提供的代码片段来进行。首先,你可以尝试使用`this.$refs.tree.setCheckedKeys(\[\])`来清空目标树已选择的节点,但是如果这个方法不生效,你可以尝试以下两种方法。 第一种方法是根据引用\[2\]中的代码片段重新获取整个树组件的node,然后将每个选项的checked属性设置为空。你可以使用`this.$refs.tree.getNode(this.data\[0\].id).parent`来获取整个树的node,然后使用`node.childNodes.map(val => { val.checked = false; if (val.childNodes && val.childNodes.length > 0) { val.childNodes.map(value => { value.checked = false }) } })`来遍历树的每个节点,并将其checked属性设置为false。 第二种方法是根据引用\[3\]中的代码片段,在checkAll方法中使用`this.$refs.tree.setCheckedNodes(this.data)`来将所有节点设置为选中状态,然后根据需要将其checked属性设置为false。具体来说,如果`this.checked`为true,你可以使用`this.$refs.tree.setCheckedNodes(this.data)`将所有节点设置为选中状态。如果`this.checked`为false,你可以使用以下代码来遍历树的每个节点,并将其checked属性设置为false:`node.childNodes.map(val => { val.checked = false; if (val.childNodes && val.childNodes.length > 0) { val.childNodes.map(value => { value.checked = false if (value.childNodes && value.childNodes.length > 0) { value.childNodes.map(value_ba => { value_ba.checked = false }) } }) } })`。 希望这些方法能够帮助你解决问题。 #### 引用[.reference_title] - *1* [el-tree使用setCheckedKeys清空树节点不生效](https://blog.csdn.net/qq_40896145/article/details/120738232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Vue+element tree使用,当弹窗关闭时,this.$refs.tree.setCheckedKeys([])不能清空选中](https://blog.csdn.net/qq_43485006/article/details/113359407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [el-tree:this.$refs.tree.setCheckedKeys([])清空失败](https://blog.csdn.net/Aoutlaw/article/details/123379741)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
阅读全文

相关推荐

为啥报这个错:Uncaught TypeError: Cannot read properties of undefined (reading 'picUrl';<template> <el-button type="primary" icon="el-icon-edit" class="btn" @click="categoryAdd">添加</el-button> <el-table ref="singleTable" border :data="tableData" highlight-current-row row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" @current-change="handleCurrentChange" style="width: 100%"> <el-table-column property="id" label="类目ID"> </el-table-column> <el-table-column property="name" label="类目名"> </el-table-column> <el-table-column property="iconUrl" label="类目图标"> <template v-slot="scope"> </template> </el-table-column> <el-table-column property="picUrl" label="类目图片"> <template v-slot="scope"> </template> </el-table-column> <el-table-column property="keywords" label="关键字"> </el-table-column> <el-table-column property="desc" label="简介" width="180"> </el-table-column> <el-table-column property="level" label="级别"> <template v-slot="scope"> <el-tag v-if="scope.row.level == 'L1'">一级类目</el-tag> <el-tag type="info" v-if="scope.row.level == 'L2'">二级类目</el-tag> </template> </el-table-column> <el-table-column property="" label="操作" width="190"> </el-table-column> </el-table> <el-dialog title="创建" :visible.sync="dialogVisible" width="50%"> <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"> <el-form-item label="类目名称" prop="name"> <el-input v-model="ruleForm.name"></el-input> </el-form-item> <el-form-item label="关键字"> <el-input v-model="ruleForm.keywords"></el-input> </el-form-item> <el-form-item label="级别"> <el-select v-model="ruleForm.level"> <el-option label="一级类目" value="L1"></el-option> <el-option label="二级类目" value="L2"></el-option> </el-select> </el-form-item> <el-form-item label="父目录" v-if="ruleForm.level == 'L2'"> <el-select v-model="ruleForm.pid" placeholder="0"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option> </el-select> </el-form-item> <el-form-item label="类目图标"> <el-upload class="avatar-uploader" action="http://admin.bsmall.byesame.com/admin/storage/create" :headers="headers" :show-file-list="false" :on-success="handleAvatarSuccess" v-model="ruleForm.iconUrl"> </el-upload> </el-form-item> <el-form-item label="类目图片"> <el-upload class="avatar-uploader" action="http://admin.bsmall.byesame.com/admin/storage/create" :headers="headers" :show-file-list="false" :on-success="handleAvatarSuccess2" v-model="ruleForm.picUrl"> </el-upload> </el-form-item> <el-form-item label="简介"> <el-input v-model="ruleForm.desc"></el-input> </el-form-item> </el-form> <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button> <el-button @click="resetForm('ruleForm')">重置</el-button> </el-dialog> </template> <script> import { listCategory,listCatL1, createCategory, } from '@/api/category' export default { data() { return { headers: {}, imageUrl: '', imageUrl2: '', tableData: [], options: [], currentRow: null, dialogVisible: false, ruleForm: { name: '', keywords: '', level: 'L2', pid: '', iconUrl: '', picUrl: '', desc: '' }, rules: { name: [{ required: true, message: '请输入类目名称', trigger: 'blur' } ] }, } }, created() { this.getList() this.getLevel1() }, methods: { // 添加: categoryAdd() { this.dialogVisible = true // 在dom加载完成之后在加载 this.$nextTick(() => { this.$refs.ruleForm.resetFields(); }) console.log(this.ruleForm); }, // 父目录 getLevel1() { listCatL1().then(res => { // console.log(res.data); this.options = res.data.data.list }) }, // 上传图片 handleAvatarSuccess(res, file) { this.ruleForm.iconUrl = res.data.iconUrl this.imageUrl = URL.createObjectURL(file.raw); }, handleAvatarSuccess2(res, file) { this.imageUrl2 = URL.createObjectURL(file.raw); this.ruleForm.iconUrl = res.data.picUrl }, // 创建 submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { createCategory(this.ruleForm).then(res => { // console.log(res); if(res.code == 200) { this.$message({ message: "新增成功", type: "success" }) // 关闭模态框 this.dialogVisible = false // 刷新列表 this.getList() // 重置表单信息及校验 this.$refs[formName].resetFields(); } }) .catch(err => { // console.log(err); this.$message({ message: err.data.errmsg, type: "error" }) }) } else { console.log('error submit!!'); return false; } }); }, // 重置 resetForm(formName) { this.$refs[formName].resetFields(); }, // 表格渲染数据 getList() { listCategory(this.query).then(res => { // console.log(res.data,'88'); this.tableData = res.data.data.list }) }, handleCurrentChange(val) { this.currentRow = val; } } } </script> <style lang="scss" scoped> .main { padding: 20px; .btn { margin: 0 0 20px 100px; } ::v-deep .el-table .cell { text-align: center !important; /* 水平居中 */ vertical-align: middle; /* 垂直居中 */ } .cell img { height: 40px !important; } } .avatar-uploader .el-upload { border: 1px dashed #d9d9d9; border-radius: 6px; cursor: pointer; position: relative; overflow: hidden; } .avatar-uploader .el-upload:hover { border-color: #409EFF; } .avatar-uploader-icon { font-size: 28px; color: #8c939d; width: 120px; height: 120px; line-height: 120px; text-align: center; } .avatar { width: 120px; height: 120px; display: block; } </style>