Jquery DataTable Sorting Numeric value column not working properly

半城伤御伤魂 提交于 2019-12-21 21:37:46

问题


In my Asp.net app, I have a Gridview Control and bind it on code behind.
And on client side I use Jquery DataTable version 1.9.4 for better Sorting,Searching functionality, Here i got one problem Numeric value not sorting properly.

I googled and come to knw by using sType": "numeric" will solved, but when i use this my whole working stoped my Column is at 9 position so i set aTragets 9

Here's the JS FIDDLE

Javascript:

$(document).ready(function () {
    $('#ctl00_ContentPlaceHolder1_GridView3').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers"
       // "aoColumnDefs": [{ "sType": "numeric", "aTargets": [9] }]

       });
  }); 

Code nehind: On Page_PreRender

if (GridView3.Rows.Count > 0)
        {
            GridView3.UseAccessibleHeader = true;
            GridView3.HeaderRow.TableSection = TableRowSection.TableHeader;
        }

On pageLoad :

GridView3.DataSource = sortedDT;
GridView3.DataBind();

回答1:


I think aaSorting is what you have to use

$(document).ready(function () {
  $('#ctl00_ContentPlaceHolder1_GridView3').dataTable({
      "aaSorting": [[ 9, "asc"]], /*row count starts from 0 in datatables*/
      "bJQueryUI": true,
      "sPaginationType": "full_numbers"
     // "aoColumnDefs": [{ "sType": "numeric", "aTargets": [9] }]

     });
}); 

UPDATE

The problem was as stated in my comment It clearly does not understand the numeric datatype of the column. Your problem is that inside your ... you had also text.

See a working fiddle http://jsfiddle.net/6Pxwy/1




回答2:


This is how i solved:

Gridview with Item template:

In Item template their might be label control, so it convert into span, we need to remove that span tag ie html tag using .contents().unwrap();

Code:

$("#Gridview_ID span").contents().unwrap();
$('#Gridview_ID ').dataTable({
       "bJQueryUI": true,
       "sPaginationType": "full_numbers",
        "aoColumns": [{ "bSortable": false }, null, null, { "sType": "numeric" }, { "sType": "date" }, null, { "bSortable": false}]
         });

Gridview with Bound Field:

$('#Gridview_ID ').dataTable({
   "bJQueryUI": true,
   "sPaginationType": "full_numbers",
    "aoColumns": [{ "bSortable": false }, null, null, { "sType": "numeric" }, { "sType": "date" }, null, { "bSortable": false}]
     });


来源:https://stackoverflow.com/questions/16832139/jquery-datatable-sorting-numeric-value-column-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!