function filterCache(config){ var _default = [{ name: 'ssmc', isNeed: true, isNeedAll: false },{ name: 'zsnf', isNeed: true },{ name: 'klmc', isNeed: true, isNeedAll: false },{ name: 'sex', isNeed: true, isNeedAll: false },{ name: 'campus', isNeed: true, isNeedAll: false },{ name: 'zslx', isNeed: true, isNeedAll: false }]; this.cache = {}; this.data = null; config = config || []; this.config = $.extend([],_default,config); } /** *初始化数据 */ filterCache.prototype._initData = function(){ var table = {}; var config = this.config; if( this.data != null && this.data.list != null ){ this.cache.ssmcList = []; var $cache = this.cache; this.data.list.forEach(function(items,index){ for(var key in items){ var params = key.split('_'); var lastArr = items[key]; lastArr.forEach(function(lastParam){ var obj = {}; var i = 0; var firstParam = null; for(var key in params){ i = key; if(key == 0){ firstParam = params[key]; }else{ var item_config = config[key]; obj[item_config['name']] = params[key]; if(!item_config['isNeed']){ obj[item_config['name']] = ''; } } } var last_item_config = config[++i]; obj[last_item_config['name']] = lastParam; if(!last_item_config['isNeed']){ obj[last_item_config['name']] = ''; } if( table[firstParam] == null ){ $cache.ssmcList.push(firstParam); table[firstParam] = []; } table[firstParam].push(obj); }); } }); this.cache.table = table; console.log(this.cache); } } /** *添加数据 */ filterCache.prototype.add = function(data){ this.data = data; this._initData(); this.data = null; this._getSub(); } /** *修改配置文件 */ filterCache.prototype.changeConfig = function(config){ config = config||[]; this.config = $.extend([],this.config,config); } /** *根据菜单获取数据 */ filterCache.prototype._getSub = function(params){ if( params == null ){ return false; } var config = this.config; var table = this.cache.table; var needTable = []; if( params[0] == '_filter_all' ){ this.cache.ssmcList.forEach(function(ssmc){ var items = table[ssmc]||[]; items.forEach(function(item){ item.ssmc = ssmc; needTable.push(item); }); }); }else{ needTable = table[params[0]]||[]; } var list = needTable.filter(function(item){ for( var key = 0; key < params.length; key++ ){ var param = params[key]; if( key!=0 && param!="" && param!='_filter_all' ){ if( item[config[key]['name']] != param ){ return false; } } } return true; }); var name = config[params.length]['name']; var json = {}; list.forEach(function(item,key){ if(json[item[name]] == null){ json[item[name]] = key; } }); return json_to_array(json_key_val_reverse(json)); } /** *获取数据 */ filterCache.prototype.get = function(params){ var config = this.config; var ssmcList = this.cache.ssmcList var table = this.cache.table; if( params == null ){ params = ["","","","","",""]; if(config[0]['isNeedAll']){ params[0] = '_filter_all'; }else{ params[0] = this.cache.ssmcList[0]; }; } var subParams = []; var list = []; list[0] = ssmcList; var _this = this; for(var key = 0; key < config.length; key++ ){ var item = config[key]; var lastArr = list[list.length-1]; var param = lastArr[0]||''; if( item.isNeedAll ){ param = '_filter_all'; } for(var k=0 ; k < lastArr.length; k++){ if(lastArr[k] == params[key]){ param = params[key] break; } } subParams.push(param); if(config[key+1]!=null&&!config[key+1].isNeed){ list.push([]); }else{ if( key < config.length-1 ){ list.push(_this._getSub(subParams)); } } } return { list: list, params: subParams }; }; function json_key_val_reverse(json){ var n = {}; for(var key in json){ n[json[key]] = key; } return n; } function json_to_array(json){ var arr = []; for(var key in json){ arr.push(json[key]) } return arr; }