ITeye博客

DataTable Custom Sorting On Column

caiwenhn2008 · 2016-06-07 18:28 · J2EE/UI · 企业架构 · Markdown

Requirement: we want to filter out the rows which is with empty value("", or " ") when sorting, always leave the rows with empty value column to the bottom of the data table. 
jQuery.fn.dataTableExt.oSort['customstring-asc'] = function(x,y) {
		var retVal;
		x = $.trim(x);
		y = $.trim(y);

		if (x==y) retVal= 0;
		else if (x == "" || x == " ") retVal= 1;
		else if (y == "" || y == " ") retVal= -1;
		else if (x > y) retVal= 1;
		else retVal = -1;
		return retVal;
	};
	
	jQuery.fn.dataTableExt.oSort['customstring-desc'] = function(x,y) {
		var retVal;
		x = $.trim(x);
		y = $.trim(y);

		if (x==y) retVal= 0;
		else if (x == "" || x == " ") retVal= 1;
		else if (y == "" || y == " ") retVal= -1;
		else if (x > y) retVal= -1;
		else retVal = 1;
		return retVal;
	};
	
	/** Initiate Data Table * */
	oTable = $('#activityTable').dataTable({
		"oLanguage" : {
			"sSearch" : "SEARCH FLOW:"
		},
		bJQueryUI : true,
		bLengthChange : false,
		bPaginate : false,
		bInfo : false,
		aoColumnDefs : [  {
			"stype" : "customstring",
			"atargets" : [ 1 ]
		}
	});

Refer to https://datatables.net/examples/plug-ins/sorting_sType.html

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。