JS+Vue实现三级全选单选

 更新时间:2022年07月15日 14:39:40   作者:webqingyou  
这篇文章主要为大家详细介绍了JS+Vue实现三级全选单选,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了JS+Vue实现三级全选单选的具体代码,供大家参考,具体内容如下

HTML

<div class="demand-class">
    <div class="demand-class-title">需求分类</div>
    <div class="demand-check">
        <input
        class="collect-top-checked"
        type="checkbox"
        v-model="demandChecked"
        @change="handledemandChecked"
        />全选
    </div>
    <div
        class="package-type package-type2"
        v-for="(itns, itds) in classiFications"
        :key="itns.id"
    >
        <div class="package-type-title upgrading-title">
        <input
            class="collect-top-checked"
            type="checkbox"
            v-model="ficationsCheck[itds]"
            @change="handleFicationsCheck(itds)"
        />{{ itns.name }}
        <div class="title-bor"></div>
        </div>
        <div class="package-type-content">
        <div
            v-for="cd in itns.children"
            :key="cd.id"
            class="package-type-list"
        >
            <input
            class="collect-top-checked"
            type="checkbox"
            :value="cd.id"
            @change="handlechildrenCheck(itds, cd.id)"
            v-model="childrenCheck"
            />
            <div>{{ cd.name }}</div>
        </div>
        </div>
    </div>
</div>

js

data () {
    classiFications: [], //需求分类 接口给的集合
    demandChecked: false, // 需求分类全选
    ficationsCheck: [], //一级分类的单个全选
    childrenCheck: [], //二级分类的全选
    demandCheckedShow: false, //二级全选不触发接口
}
 
methods: {
    // 需求分类全选
    handledemandChecked() {
        
        if (this.demandChecked) {
            this.classiFications.forEach((it, is) => {
            this.ficationsCheck[is] = true;
            this.handleFicationsCheck(is);
            });
        } else {
            this.classiFications.forEach((it, is) => {
            this.ficationsCheck[is] = false;
            this.handleFicationsCheck(is);
            });
        }
        },
        //一级分类所选
        async handleFicationsCheck(id) {
        
        this.demandCheckedShow = true;
        let tmp = this.classiFications[id].childrenIds; //当前选择的id子集合
        let tmpAdd = this.childrenCheck; //当前选择的id子集合
        if (this.ficationsCheck[id]) {
            tmp.forEach((item) => {
            for (let i = 0; i < tmp.length; i++) {
                if (tmpAdd.indexOf(item) === -1) {
                this.childrenCheck.push(item);
                }
            }
            });
        } else {
            tmp.forEach((item) => {
            for (let i = 0; i < tmp.length; i++) {
                if (tmpAdd.indexOf(item) !== -1) {
                this.childrenCheck.splice(this.childrenCheck.indexOf(item), 1);
                }
            }
            });
        }
        // this.handleType();
        this.currentPage = 0;
        await this.initSolutionAllPage();
        this.demandCheckedShow = false;
        },
        //二级分类所选
        handlechildrenCheck(ids, cd) {
      
        console.log(cd);
        let cont = 0;
        // let conts = 0;
        let tmp = this.classiFications[ids].childrenIds; //当前选择的id子集合
        let tmpAdd = this.childrenCheck; //当前选择的id子集合
        if (this.ficationsCheck[ids]) {
            tmp.forEach((item) => {
            for (let i = 0; i < tmp.length; i++) {
                if (tmpAdd.indexOf(item) === -1) {
                this.ficationsCheck[ids] = false;
                }
            }
            });
        } else {
            let tmpl = tmp.length === 1 ? 1 : tmp.length - 1;
            tmp.forEach((item) => {
            for (let i = 0; i < tmpl; i++) {
                if (tmpAdd.indexOf(item) !== -1) {
                // console.log(item);
                cont = cont + 1;
                }
            }
            });
            if (cont === this.classiFications[ids].childrenIds.length) {
            this.ficationsCheck[ids] = true;
            }
        }
        // this.handleType();
        if (!this.demandCheckedShow) {
            this.currentPage = 0;
            this.initSolutionAllPage();
        }
    },
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 区别JavaScript函数声明与变量声明

    区别JavaScript函数声明与变量声明

    这篇文章给大家分享了关于JavaScript中函数声明与变量声明之间的区别以及相关知识点,有兴趣的朋友参考下。
    2018-09-09
  • Bootstrap安装环境配置教程分享

    Bootstrap安装环境配置教程分享

    这篇文章主要为大家分享了Bootstrap安装环境配置教程,安装步骤非常简单,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • webpack自动化打包webpack-dev-server的实现

    webpack自动化打包webpack-dev-server的实现

    我们每次改完要打包的资源文件,和配置文件都是是输入npx webpack命令手动打包的,本文就来介绍一下webpack自动化打包webpack-dev-server的实现,具有一定的参考价值,感兴趣的可以了解一下
    2023-07-07
  • js 表格排序(编辑+拖拽+缩放)

    js 表格排序(编辑+拖拽+缩放)

    js 表格排序(编辑+拖拽+缩放)实现代码,需要的朋友可以参考下。
    2010-05-05
  • 单行 JS 实现移动端金钱格式的输入规则

    单行 JS 实现移动端金钱格式的输入规则

    这篇文章主要介绍了单行 JS 实现移动端金钱格式的输入规则,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-05-05
  • JS实现简单获取最近7天和最近3天日期的方法

    JS实现简单获取最近7天和最近3天日期的方法

    这篇文章主要介绍了JS实现简单获取最近7天和最近3天日期的方法,涉及javascript针对日期与时间的相关数值运算与转换操作技巧,需要的朋友可以参考下
    2018-04-04
  • JAVA四种基本排序方法实例总结

    JAVA四种基本排序方法实例总结

    这篇文章主要介绍了JAVA四种基本排序方法,较为详细的总结分析了插入法、冒泡法、选择法及Shell排序等四种常用的排序技巧,非常具有实用价值,需要的朋友可以参考下
    2015-07-07
  • javascript字符串循环匹配实例分析

    javascript字符串循环匹配实例分析

    这篇文章主要介绍了javascript字符串循环匹配,实例分析三种常用的字符串循环匹配的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • GoJs 图片绘图模板Picture使用示例详解

    GoJs 图片绘图模板Picture使用示例详解

    这篇文章主要为大家介绍了GoJs 图片绘图模板Picture使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • Javascript 原型和继承(Prototypes and Inheritance)

    Javascript 原型和继承(Prototypes and Inheritance)

    前面我们看到了如何使用 constructor 来初始化对象。如果这样做,那么每一个创建的新对象都会对那些相同的属性,方法建立一个独立的副本。而实际上有更加有效的方法来指定方法,常量,以及其他一些可被所有该类的对象共享的属性。
    2009-04-04

最新评论