User987
User987

Reputation: 3823

jQuery datatable c# server side default sort column giving null

I have a jquery datatable for whom I've set a default sort column like this:

$('#datatable-responsive').DataTable({
                "oLanguage": {
                    sProcessing: "<img src='/images/gears.svg'>"
                },
                order: ['3', 'desc'],

Please note this part:

order: ['3', 'desc'],

Now the default sort column should be the 4th one (0,1,2 => 3 (4th))

When I first load a page I get a correct result for the following in my C# action:

var sortColumnDir  = Request["order[0][dir]"];

Looks good first time, sorcolumndir is "desc" as required...

Now the second time when I click table's head to sort the column in ascending order... I get a null as result in this line:

 var sortColumnDir  = Request["order[0][dir]"]; // this is now null for some reason ?

When I switch between pages, without clicking to try to change the order of column [3] to ascending order, it's always good... But when I try to change it into ascending order it throws me exception and returns null :(

Any ideas guys why this might happen ? What am I doing wrong here?

P.S. Guys I need to have this column [3] always in descending order as default if user hasn't clicked to sort it otherwise, or some other column.

Upvotes: 2

Views: 2370

Answers (2)

Baouche Iqbal
Baouche Iqbal

Reputation: 105

maybe u have to write a loop in order to find the right colmun to be ordered. something that looks like that:

 var o = 0; var orders = new List<DatatablesOrder>(); 
while (!string.IsNullOrEmpty(Request["order[" + o + "][column]"])) 
{ 
   orders.Add(new DatatablesOrder { Column = Convert.ToInt32(form["order[" + o +"][column]"]), 
   Dir = Request["order[" + o + "][dir]"] });
   o++; 
} 

Upvotes: 1

Mustufa
Mustufa

Reputation: 131

use this in js file

"order": [[3, "DESC"]],

and in your controller using this you can get default sorting column and direction

 var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
 var sortDirection = Request["sSortDir_0"]; // asc or desc

Upvotes: 0

Related Questions