ignore word from column sort
ignore word from column sort
I have a table of books with titles, authors, publishers, and dates in it. Under the title column a lot of the book titles start with the word "The." How can I setup the sort to ignore the first word if it is "The" when sorting this column?
This discussion has been closed.
Replies
What would need to be done is to make use of a sorting plug-in ( http://datatables.net/plug-ins/sorting ) which will strip the word "The" from the string being sorted. I can't post the code for this just now (on my phone...) but it's a nice idea for a plug-in - I'll do this when I get back on a real computer... :-)
Regards,
Allan
[code]
jQuery.fn.dataTableExt.oSort['no-the-asc'] = function(a,b) {
var x = a.toLowerCase().replace(/^the /, "");
var y = b.toLowerCase().replace(/^the /, "");
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['no-the-desc'] = function(a,b) {
var x = a.toLowerCase().replace(/^the /, "");
var y = b.toLowerCase().replace(/^the /, "");
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]
You'll need to set the sType for the column to "no-the": http://datatables.net/usage/columns#sType
Allan
[code]
jQuery.fn.dataTableExt.oSort['anti-the-asc'] = function(a,b) {
var x = a.replace(/^the /i, "");
var y = b.replace(/^the /i, "");
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['anti-the-desc'] = function(a,b) {
var x = a.replace(/^the /i, "");
var y = b.replace(/^the /i, "");
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$(document).ready(function() {
/* Initialise datatables */
var oTable = $('#courseindex').dataTable({
"bPaginate": false,
"bAutoWidth": false,
"sDom": '<"top"f><"showing"i>rt',
"aoColumns": [
{ "sType": "anti-the-asc" },
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
]
});
$("#advanced_search").appendTo("#courseindex_wrapper .top");
$("#advanced_search").removeClass("hidden");
/* Add event listeners to the two range filtering inputs */
$('select#filterday').change( function() { oTable.fnFilter( $(this).val(), 13 ); } );
$('select#filtercredits').change( function() { oTable.fnFilter( $(this).val(), 4 ); } );
$('select#filterstanding').change( function() { oTable.fnFilter( $(this).val(), 3 ); } );
$('select#filteroffered').change( function() { oTable.fnFilter( $(this).val(), 5 ); } );
$("#theSelect").change(function(){
var value = $("#theSelect option:selected").val();
if (value === '') return;
var theDiv = $(".is" + value);
theDiv.fadeIn().removeClass("hidden");
$("#theSelect option:selected").attr('disabled','disabled');
$(this).val('');
});
$("div a.remove").click(function () {
var value = $(this).attr('rel');
var theDiv = $(".is" + value);
var colid = $(this).parent().attr('rel');
oTable.fnFilter( "", colid );
$("#theSelect option[value=" + value + "]").removeAttr('disabled');
$(this).parent().fadeOut(function() { $(this).addClass("hidden"); });
});
} );
[/code]