XX分类联动js通用写法

var CatUtils = {
    initOpiton: '<option value="">---请选择---</option>',
    catDataArr: [],//分类集合
    init: function (params) {
        var arrObj = params['objArr']
        switch (arrObj.length) {
            case 1://1级联动
                this.setLiandong(arrObj[0])
                break
            case 2://2级联动
                this.setLiandong(arrObj[0], arrObj[1])
                break
            case 3://3级联动
                this.setLiandong(arrObj[0], arrObj[1], arrObj[2])
                break
        }

        this.setData(0, arrObj[0])

    }, setData: function (pid, obj, selectVal) {
        pid = parseInt(pid)
        if (!$.isEmptyObject(this.catDataArr)) {
            $(obj).html(CatUtils.getHtmlstr(this.catDataArr, pid, CatUtils.initOpiton))
            if (selectVal)
                $(obj).find("option[value = '" + selectVal + "']").attr("selected", "selected");
        } else {
            $.ajax({
                type: 'GET',
                url: '/user/crop/categoryPid?pid=' + pid,
                async: false,
                success: function (result) {
                    CatUtils.catDataArr = result.data
                    $(obj).html(CatUtils.getHtmlstr(CatUtils.catDataArr, pid, CatUtils.initOpiton))
                    if (selectVal)
                        $(obj).find("option[value = '" + selectVal + "']").attr("selected", "selected");
                }, complete: function () {
                    //layer.closeAll("loading")
                }
            });
        }
    },
    getHtmlstr: function (data, pid, str) {
        for (var index in data) {
            var item = data[index]
            if (isNaN(pid) || pid == 0) {
                /** 一级分类 **/
                str += '<option value="' + item.id + '">' + item.name + '</option>'
            } else {
                if (item.id == pid && !$.isEmptyObject(item.cropCategoryVos)) {
                    $.each(item.cropCategoryVos, function (index, item) {
                        str += '<option value="' + item.id + '">' + item.name + '</option>'
                    })
                    return str
                } else {
                    if (item.cropCategoryVos) {
                        CatUtils.getHtmlstr(item.cropCategoryVos, pid, str)
                    }
                }
            }
        }
        if (isNaN(pid) || pid == 0)
            return str
    },
    setLiandong: function (a, b, c) {
        if (a) {
            a.change(function () {
                var val = $(this).find("option:selected").val()
                if (!$.isEmptyObject(val) && b != undefined) {
                    /** B里填充数据 **/
                    CatUtils.setData(val, b)
                    /** 清空c的数据 **/
                    if (c)
                        c.html(CatUtils.initOpiton)
                }
            })
        }
        if (b) {
            b.change(function () {
                var val = $(this).find("option:selected").val()
                if (!$.isEmptyObject(val) && c != undefined) {
                    CatUtils.setData(val, c)
                }
            })
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42051240/article/details/81057135