device-cat-select-jqcontrol.js 5.32 KB
(function ($) {
    $.extend({
        //初始化配置
        initcat: function (e, p) {
            p = $.extend({
                urlOrData: null,                            // 地址获取设备类型数据
                deviceParentCat: '',                       // 一级分类控件ID
                deviceChildCat: '',                         //二级分类控件ID
                animate: { showClass: '', hideClass: '' },  // 显示/隐藏动画效果
                onChoice: false                             // 选择后执行的事件
            }, p);
            var deviceParentCat = p.deviceParentCat ? p.deviceParentCat : 'deviceParentCat';
            var deviceChildCat = p.deviceChildCat ? p.deviceChildCat : 'deviceChildCat';
            var option = {
                _LoadParentCat:function (control,getDeviceCatListURL) {
                    $.ajax({
                        type: "post",
                        url: getDeviceCatListURL,
                        dataType:"json",
                        data: $.csrf({"parentId":0}),
                        success:function(msg){
                            if(msg['status'] == 1){
                                var data = eval(msg['modelList']);
                                var html = "<option value=''>--请选择--</option>";
                                for(var s in data){
                                    html = html +"<option value='"+data[s]['id']+"'>"+data[s]['name']+"</option>";
                                }
                                $(control).html(html);
                                //触发二级选择
                                //$("#deviceParentCat").trigger("change");
                            }else{
                                //提示确认失败
                                alert("加载失败");
                            }
                        },
                        error:function(msg){
                            //提示确认失败
                            alert("加载异常");
                        }
                    });
                },
                _Change:function(pid,getDeviceCatListURL,childselect){
                    if(pid == ""){
                        $("#"+childselect).html("<option value=''>--请选择二级分类--</option>");
                        return false;
                    }
                    $.ajax({
                        type: "post",
                        url:getDeviceCatListURL,
                        dataType:"json",
                        data: $.csrf({"parentId":pid}),
                        success:function(msg){
                            if(msg['status'] == 1){
                                var data = eval(msg['modelList']);
                                var html = "<option value=''>--请选择二级分类--</option>";
                                var cat_id = $("#hid_cat_id").val();
                                for(var s in data){
                                    if(parseInt(cat_id) == parseInt(data[s]['id'])){
                                        html = html +"<option value='"+data[s]['id']+"' selected=\"selected\">"+data[s]['name']+"</option>";
                                    }else{
                                        html = html +"<option value='"+data[s]['id']+"'>"+data[s]['name']+"</option>";
                                    }
                                }
                                $("#"+childselect).html(html);
                            }else{
                                alert("加载失败");
                            }
                        },
                        error:function(msg){
                            //提示确认失败
                            alert("加载异常");
                        }
                    });
                },
                //加载数据
                loadData: function () {
                    if (typeof p.urlOrData == 'string') {
                        option._LoadParentCat($("#"+deviceParentCat),p.urlOrData);
                        var pid = $("#"+deviceParentCat).val();
                        option._Change(pid,p.urlOrData,deviceChildCat);

                    }
                }
            }

            option.loadData();
            $(e).bind("change",function(){
                var pid = $("#"+deviceParentCat).val();
                option._Change(pid,p.urlOrData,deviceChildCat);
            });
        }
    });

    $.fn.extend({
        initcat: function (p) {
            return this.each(function () {
                $.initcat(this, p);
            });
        }
    });
})(jQuery);
/*
    使用方法:
    <script src="<?php echo Url::toRoute('/exts-src/base/1.0.0/ui/device-cat-select/device-cat-select-jqcontrol.js'); ?>" type="text/javascript" ></script>
       $(function () {
       $('#deviceParentCat').initcat({
            urlOrData: '<?=Url::toRoute("/device/model/get-device-cat-list")?>',                            // 地址获取设备类型数据
            deviceParentCat: 'deviceParentCat',                       // 一级分类控件ID
            deviceChildCat: 'deviceChildCat',                         //二级分类控件ID
            animate: { showClass: 'animated flipInX', hideClass: 'animated flipOutX' },
            onChoice: function (data) {
                console.log(data);
            }
        });
    });
 */