jquery easyui dataGrid dynamically changes the sorting field name, the sorting column name is inconsistent

 

If the property name is propertyName and the column name is property_name, then the sorting will report an error,

jQuery jqGrid can specify the sorting column name, but easyui's dataGrid does not.

 

Need to do column name conversion,

 

1. The foreground is converted through js

Advantages: efficient, easy to edit, save server resources.

Disadvantages: Expose the listing, (but for general intranet systems, this is acceptable)

 

Sort conversion before loading

dataGrid specification

sortName:'propertyName',

sortOrder:'asc',

multiSort:true,

onBeforeLoad:dgOnBeforeLoad

 

 

//sort conversion map
var sortMap = {};
sortMap['propertyName']='property_name';

//sort conversion
//dataGrig's onBeforeLoad event reference
function dgOnBeforeLoad(param){
	if(param.sort){ //Another parameter: param.order
		var sortResult = "";
		var sorts = param.sort.split(",");
		was black;
		for(var i=0; i<sorts.length; i++){
			sort = sorts[i].trim();
			if(sortMap && sortMap[sort]){
				sortResult + = sortMap [sort];
			} else {
				sortResult + = sort;
			}
			sortResult += ",";
		}
	}
	if(sortResult.length>0){
		sortResult = sortResult.substring(0,sortResult.length-1);
	}
	param.sort = sortResult;
}
 

 

 

sortName writes the attribute name, not the column name, so it will not appear, and the problem of which column to sort by cannot be seen in the dataGrid

 

Complex methods, generally do not need to configure sortMap

//sort conversion
function dgOnBeforeLoad(param){
	if(param.sort){
		param.sort=sortConvert(param.sort,null,null,true);
	}
}

/**
 * Sort conversion, camelCase naming method with an underscore before uppercase letters (uppercase letters are converted to lowercase)
 * @param paramSort sorting string, such as: addTime,modifyTime,id
 * @param sortMap specifies the sort Map, no, you can pass null
 * var sortMap = {};
 *						sortMap['propertyName']='property_name';
 * @param ignoreConvert ignore conversion list, separated by commas, such as: propertyName1, propertyName2, you can pass null
 * @param otherConvert other, whether to convert
 * @returns {String}	//add_time,modify_time,id
 */
function sortConvert(paramSort, sortMap, ignoreConvert, otherConvert){
	var ignoreMap = {};
	if(ignoreConvert && ignoreConvert.length>0){
		var ignoreConverts = ignoreConvert.split(",");
		for(var i=0; i<ignoreConverts.length; i++){
			ignoreMap[ignoreConverts[i].trim()] = true;
		}
	}
	var sortResult = "";
	var sorts = paramSort.split(",");
	was black;
	var ch;
	for(var i=0; i<sorts.length; i++){
		sort = sorts[i].trim();
		if(sortMap && sortMap[sort]){ //First convert the specified conversion
			sortResult + = sortMap [sort];
		} else if(ignoreMap[sort]){ //Ignored, no conversion
			sortResult + = sort;
		} else if(otherConvert) { //Default conversion method
			for(var j=0; j<sort.length; j++){
				ch = sort.charAt(j);
				if(isUpperCase(ch)){
					sortResult += "_"+ch.toLowerCase();
				} else {
					sortResult + = ch;
				}
			}
		} else {
			sortResult + = sort;
		}
		sortResult += ",";
	}
	if(sortResult.length>0){
		sortResult = sortResult.substring(0,sortResult.length-1);
	}
	//alert(sortResult);
	return sortResult;
}

 

Second, the background through java conversion

Advantage: does not expose column names

Disadvantages: using server resources, it is inconvenient to modify

 

On the server side, I wrote one, not going to use it, for reference

/**
 * Sorting conversion, camelCase named methods are underlined
 * addTime desc, modifyTime desc,id asc
 * add_time desc,modify_time desc,id asc
 * @param orderByStr
 * @return
 */
public String orderByConvert(String orderByStr){
	String[] orderBys = orderByStr.split(",");
	String sort;
	String order;
	char ch;
	StringBuffer sb = new StringBuffer();
	for(String orderBy : orderBys){
		orderBy = orderBy.trim();
		String[] sortOrders = orderBy.split(" ");
		sort = sortOrders[0];
		order = sortOrders[1];
		for(int i=0; i<sort.length(); i++){
			ch = sort.charAt(i);
			if(Character.isUpperCase(ch)){
				sb.append('_');
				sb.append(Character.toLowerCase(ch));
			} else {
				sb.append(ch);
			}
		}
		sb.append(' ');
		sb.append(order);
		sb.append(',');
	}
	//log.info("---orderByStr:"+orderByStr);
	//log.info("---orderByStr:"+sb.toString());
	if(sb.length()>0){//
		return sb.substring(0, sb.length()-1);
	} else {
		return sb.toString();
	}
}

 

refer to:

jquery easyui dataGrid dynamically change the sorting field name

http://blog.csdn.net/lht0211/article/details/45395637

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327042844&siteId=291194637