sort.js 5.47 KB
/**
 * 
 */
var QCategory = new Object();
var QCategory_ID = 'category';
var QCategory_MainName = 'max_sort_id';
var QCategory_MainID = 'max_sort_id';
var QCategory_MainClass = 'valid';
var QCategory_SubName = 'middle_sort_id';
var QCategory_SubID = 'middle_sort_id';
var QCategory_SubClass = 'valid';
var QCategory_CategoryName = 'small_sort_id';
var QCategory_CategoryID = 'small_sort_id';
var QCategory_CategoryClass = 'valid';
var QCategory_MainValue = 0;
var QCategory_SubValue  = 0;
var QCategory_CateValue = 0;
var QCategory_grade = 3;
var QCategory_call = '';

QCategory = {
		init:function(main,sub,cate,_call){
			QCategory_MainValue = main;
			QCategory_SubValue  = sub;
			QCategory_CateValue = cate;
			QCategory_call = _call;
			
			this.getData(null,QCategory.main);
			if(QCategory_MainValue != 0){
				this.getData(QCategory_MainValue,QCategory.sub);
			}
			if(QCategory_SubValue != 0){
				this.getData(QCategory_SubValue,QCategory.subCategory);
			}
		},
		main:function(data){
			var html =  '<select style="width:120px;" '+QCategory.grade(1)+' name="'+QCategory_MainName+'" id="'+QCategory_MainID+'" class="'+QCategory_MainClass+'">"';
			html += '<option value=" ">请选择分类</option>';	
			var i = 0;
			for(key in data){
				val = data[key];
				html += '<option value="'+val.id+'" '+QCategory.getSelect(val.id,QCategory_MainValue)+'>'+val.sort_name+'</option>';
			}
			html += '</select>&nbsp;&nbsp;';
			html += '<select '+QCategory.grade(2)+' name="'+QCategory_SubName+'" id="'+QCategory_SubID+'" class="'+QCategory_SubClass+'">"';
			html += '</select>&nbsp;&nbsp;';
			html += '<select '+QCategory.grade(3)+' name="'+QCategory_CategoryName+'" id="'+QCategory_CategoryID+'" class="'+QCategory_CategoryClass+'">"';
			html += '</select>';
			$("#"+QCategory_ID).html(html);
			$('#'+QCategory_SubName).attr('disabled','disabled');
			$('#'+QCategory_CategoryName).attr('disabled','disabled');
		},
		getData:function(id,callBack){
			var d = '';
			if(d == ' ')
			{
				alert(1);
				$("#" + QCategory_SubID).empty();
				$("#" + QCategory_CategoryID).empty();
				$('#'+QCategory_SubName).attr('disabled','disabled');
				$('#'+QCategory_CategoryName).attr('disabled','disabled');
				return false;
			}
			if(id){
				d += 'parent_id='+id;
			}
			$.ajax({
		        type: "GET",
		        url: "/product/getsort",
		        data: d,
		        dataType:'json',
		        success: function(jsonData)
		        {
		        	if(jsonData.code == 200){
		        		callBack( jsonData.data );
		        		return true;
		        	}
		        	alert('数据请求错误!');
		        	return false;
		        }
			});	
		},
		mainChange:function(obj){
			html = '<select style="width:120px;" '+QCategory.grade(2)+' name="'+QCategory_SubName+'" id="'+QCategory_SubID+'" class="'+QCategory_SubClass+'">"';
			html += '<option value=" ">加载中..</option>';
			html += '</select>';
			$('#'+QCategory_SubName).replaceWith(html);
			$('#'+QCategory_SubName).attr('disabled','disabled');
			
			
			$('#'+QCategory_CategoryName).attr('disabled','disabled');
			QCategory_MainValue = obj.value;
			if(QCategory_MainValue == 0)
			{
				$('#'+QCategory_SubName).empty();
				$('#'+QCategory_CategoryName).empty();
				return false;
			}
			this.getData(obj.value,QCategory.sub);
		},
		grade:function( nums ){
			if( nums >= QCategory_grade){
				return '';
			}
			switch(nums){
				case 1:
					return 'onchange="QCategory.mainChange(this)"';
					break;
				case 2:
					return 'onchange="QCategory.subChange(this)"';
					break;
				default:
					return '';
			}
		},
		sub:function(data){
			var html = '';
			html += '<select style="width:100px;" '+QCategory.grade(2)+' name="'+QCategory_SubName+'" id="'+QCategory_SubID+'" class="'+QCategory_SubClass+'">"';
			html += '<option value=" ">请选择分类</option>';
			var i = 0;
			if(data != ''){
				for(key in data){
					val = data[key];
					html += '<option value="'+val.id+'" '+QCategory.getSelect(val.id,QCategory_SubValue)+'>'+val.sort_name+'</option>';
				}
			}
			html += '</select>';
			$('#'+QCategory_SubName).replaceWith(html);
			$('#'+QCategory_CategoryName).attr('disabled','disabled');
		},
		subChange:function(obj){
			html = '<select style="width:100px;" '+QCategory.grade(3)+' name="'+QCategory_CategoryName+'" id="'+QCategory_CategoryID+'" class="'+QCategory_CategoryClass+'">"';
			html += '<option value=" ">加载中..</option>';
			html += '</select>';
			$('#'+QCategory_CategoryName).replaceWith(html);
			$('#'+QCategory_CategoryName).attr('disabled','disabled');
			QCategory_SubValue = obj.value;
			if(QCategory_SubValue == 0)
			{
				$('#'+QCategory_CategoryName).empty();
				return false;
			}
			this.getData(obj.value,QCategory.subCategory);
		},
		subCategory:function(data){
			var html = '';
			html += '<select style="width:100px;" name="'+QCategory_CategoryName+'" id="'+QCategory_CategoryID+'" '+QCategory.getCall(QCategory_call)+'>"';
			html += '<option value=" ">请选择分类</option>';
			if(data != ''){
				for(key in data){
					val = data[key];
					html += '<option value="'+val.id+'" '+QCategory.getSelect(val.id,QCategory_CateValue)+'>'+val.sort_name+'</option>';
				}
			}
			html += '</select>';
			$('#'+QCategory_CategoryName).replaceWith(html);
		},
		getSelect:function(id,sid){
			if(id == sid){
				return 'selected=true';
			}else{
				return '';
			}
		},
		getCall:function(_call){
			if(_call == '' || typeof(_call) == 'undefined'){
				return false;
			}
			return 'onChange="'+_call+'(QCategory_MainValue, QCategory_SubValue ,this.value)"';
		}
}