最近刚好要做个有联动的分类选择,就写了下面这个东西,令人郁闷的javascript
cat = {
data : function (code,name,price,discount){
this.code = code;
this.name = name;
this.price = price;
this.discount = discount;
},
get : function(code){
if (!code || code.length % 3 != 0 || code.length < 3)
{
return false;
}
if(cat.getlevel(code.length / 3)[code]){
return cat.getlevel(code.length / 3)[code];
} else {
return false;
}
},
add : function (code,name,price,discount){
if(!cat["lv_" + code.length / 3 ]){
cat["lv_" + code.length / 3 ] = new Object();
cat["lv_" + code.length / 3 ].len = 0;
}
cat["lv_" + code.length / 3 ][code] = new cat.data(code,name,price,discount );
cat["lv_" + code.length / 3 ].len++;
},
getlevel : function (level){
if(cat["lv_" + level]){
return cat["lv_" + level]
} else {
return false;
}
},
getsub : function( code ){
code = new String(code);
datas = new Object();
level = code.length / 3 + 1;
sub = cat.getlevel(level);
count = 0;
for(var item in sub){
if(sub[item].code){
if(sub[item].code.substr(0,code.length) == code ){
datas[sub[item].code] = sub[item];
count++;
}
}
}
datas.len = count;
if(count == 0){return false;}
return datas;
}
}
附部份测试数据
cat.add(‘001′,’魔兽世界’);
cat.add(‘001001′,’一区’);
cat.add(‘001001001′,’伊瑟拉’);
cat.add(‘001001001001′,’联盟’,’0.2617′,’1.00′);
cat.add(‘001001001002′,’部落’,’0.6229′,’1.00′);
cat.add(‘001001002′,’卡德罗斯’);
cat.add(‘001001002001′,’联盟’,’0.3297′,’1.00′);
cat.add(‘001001002002′,’部落’,’0.7794′,’1.00′);
cat.add(‘001001003′,’卡扎克’);
cat.add(‘001001003001′,’联盟’,’0.9082′,’1.00′);
cat.add(‘001001003002′,’部落’,’0.2026′,’1.00′);
cat.add(‘001001004′,’回音山’);
cat.add(‘001001004001′,’联盟’,’0.2884′,’1.00′);
cat.add(‘001001004002′,’部落’,’0.8341′,’1.00′);
cat.add(‘001001005′,’国王之谷’);
还是感觉好麻烦 做联动的时候还是很郁闷啊····没有什么好办法
我太笨了
Read: 820