Problem while Sorting an anchor tag whose innerhtml is a number.
Problem while Sorting an anchor tag whose innerhtml is a number.
PFB my code. No matter what I do anchor tag is not getting sorted. Please help :( .
jQuery.extend(jQuery.fn.dataTableExt.oSort, { "num-html-pre": function (a) { var sValue = parseInt(a.innerHTML()); }, "num-html-asc": function (a, b) { return ((a < b) ? -1 : ((a > b) ? 1 : 0)); }, "num-html-desc": function (a, b) { return ((a < b) ? 1 : ((a > b) ? -1 : 0)); } }); (function ($) { /* * Function: fnGetColumnData * Purpose: Return an array of table values from a particular column. * Returns: array string: 1d data array * Inputs: object:oSettings - dataTable settings object. This is always the last argument past to the function * int:iColumn - the id of the column to extract the data from * bool:bUnique - optional - if set to false duplicated values are not filtered out * bool:bFiltered - optional - if set to false all the table data is used (not only the filtered) * bool:bIgnoreEmpty - optional - if set to false empty values are not filtered from the result array * Author: Benedikt Forchhammer * Edited by : Sashank Bandhakavi Edit : - aiDisplayMaster is used because bstatesave is enabled. When bstatesave is true aiDisplay will not retrive all the rows. - if aData[iColumn] has a span tag its inner HTML is considered. */ $.fn.dataTableExt.oApi.fnGetColumnData = function (oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty) { // check that we have a column id if (typeof iColumn == "undefined") return new Array(); // by default we only want unique data if (typeof bUnique == "undefined") bUnique = true; // by default we do want to only look at filtered data if (typeof bFiltered == "undefined") bFiltered = true; // by default we do not want to include empty values if (typeof bIgnoreEmpty == "undefined") bIgnoreEmpty = true; // list of rows which we're going to loop through var aiRows; // Edit: Sashank Bandhakavi // ****** aiDisplayMaster is used because bstatesave is enabled. When bstatesave is true aiDisplay will not retrive all the rows. aiRows = oSettings.aiDisplayMaster; // all row numbers // set up data array var asResultData = new Array(); for (var i = 0, c = aiRows.length; i < c; i++) { iRow = aiRows[i]; var aData = this.fnGetData(iRow); if (aData[iColumn].indexOf(">") < 0) { var sValue = aData[iColumn]; } //Edit - Sashank Bandhakavi //****** if aData[iColumn] has a span tag its inner HTML is considered. else { var sValue = aData[iColumn].substring(aData[iColumn].indexOf(">") + 1, aData[iColumn].indexOf("<", aData[iColumn].indexOf("<") + 1)); } // ignore empty values? if (bIgnoreEmpty == true && sValue.length == 0) continue; // ignore unique values? else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue; // else push the value onto the result data array else asResultData.push(sValue); } // to sort data present in the select controls. return asResultData.sort(function (a, b) { if (a.indexOf('/') < 0) { var a = parseInt(a); var b = parseInt(b); return ((a < b) ? 1 : ((a > b) ? -1 : 0)); } else { var arrA = a.split('/'); var arrB = b.split('/'); if (arrA[2] == arrB[2]) { if (arrA[0] == arrB[0]) { return ((arrA[1] < arrB[1]) ? 1 : ((arrA[1] > arrB[1]) ? -1 : 0)); } else { return ((arrA[0] < arrB[0]) ? 1 : ((arrA[0] > arrB[0]) ? -1 : 0)); } } else { return ((arrA[2] < arrB[2]) ? 1 : ((arrA[2] > arrB[2]) ? -1 : 0)); } } }); } $.fn.dataTableExt.oApi.NumSort = function () { if (typeof iColumn == "undefined") return new Array(); // by default we only want unique data if (typeof bUnique == "undefined") bUnique = true; } }(jQuery)); $(document).ready(function () { $("b").replaceWith(function () { return this.innerHTML; }); $('#<%= GrdRequestList.ClientID%>').dataTable({ "bFilter": false, "bPaginate": false, "binfo": false }); /* Initialise the DataTable */ var oTable = $('#<%= GrdDistList.ClientID%>').dataTable({ "bStateSave": true, columnDefs: [ { type: 'num-html', targets: 0 } ] }); // To apply datatables and advanced filtering feature to distribution grid view. $('#MainContent_GrdDistList').dataTable().columnFilter({ sPlaceHolder: "head:before", aoColumns: [null, null, null, {type: "select", values: oTable.fnGetColumnData(3)}, { type: "select" ,values:oTable.fnGetColumnData(4)}, { type: "select",values:oTable.fnGetColumnData(5) }, { type: "select",values:oTable.fnGetColumnData(6)}, { type: "select", values: oTable.fnGetColumnData(7)}] }) });This discussion has been closed.
Replies
Please link to a test case showing the issue, as required in the forum rules.
Allan
Hi allan I have created a sample fiddle. Please have a look. No matter what I do the column with anchor tags is not getting sorted. Need urgent help.
http://jsfiddle.net/95AXx/4/
You are using an old version of DataTables. I'd suggest you try using 1.10 which will sort that data correctly.
Allan
Thank you allan. Will do that.