1. 程式人生 > 實用技巧 >Element-ui中Tree的使用,以及全選反選獲取id

Element-ui中Tree的使用,以及全選反選獲取id

<el-checkbox v-model="checkAll" @change="handleCheckAllChange">全選</el-checkbox>
                <el-tree
                    :data="data"
                    show-checkbox
                    default-expand-all
                    node-key="module_id"
                    ref="tree"
                    highlight
-current :props="defaultProps" @check="nodeClick" ></el-tree>
data() {
        return {
            checkAll: false,
            select_box: [],
            data: [], //tree
            defaultProps: {
                children: 'child',
                label: 
'module_name' } }; },
 handleCheckAllChange(val) {
            if (this.checkAll) {
//全選
this.$nextTick(function () { this.$refs.tree.setCheckedNodes(this.data); }); } else {
//反選
this.$nextTick(function () {
this.$refs.tree.setCheckedKeys([]); }); } this.$nextTick(function () {
//獲取id let select_box
= []; this.$refs.tree.getCheckedNodes().forEach((item) => { select_box.push(item.module_id); }); this.select_box = select_box; // console.log(this.select_box, 'select_box888'); }); }, nodeClick(data, node, e) {
//點選的時候獲取到所有選中的
// console.log(this.$refs.tree.getCheckedNodes(), 'console.log(this.$refs.tree.getCheckedNodes());'); let select_box = []; this.$refs.tree.getCheckedNodes().forEach((item) => { select_box.push(item.module_id); }); this.select_box = select_box; console.log(this.select_box, 'select_box'); },