Rick Calder
Rick Calder

Reputation: 18705

Resetting sort on filter of jQuery Datatables

I have a site that is doing standings of the NHL. I have the standings in a table and using jQuery Datatables for sorting and filtering.

What I would like is when someone changes a filter for the original sort to be reset. So I have an order that is set to show the teams in order based on tie breakers. When someone sorts by say Wins, then changes which conference or division they're viewing I want it to reset back to the original sort, right now it stays in the order of Wins.

I've tried a few methods but none of them seem to do anything at all. Thoughts?

The code below is the filtering function. WhichOne relates to which conference or division. I have tried the .order in a number of spots but that has no bearing.

if( whichType == "conference") {
  $(".standings-type").html(whichOne)
  standings
    .columns([12, 11]) // columns holding type and name of division/conference
    .search("") // reset the table to have all teams
    .column(12) // the column that holds the conference names
    .search(whichOne)  //searches for the name based on a variable.
    .order([0, "desc"]) // <<< want to resort by column 0, does not work
    .draw();
}

Upvotes: 2

Views: 915

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58880

Try the code below:

standings
  .columns([12, 11]) // columns holding type and name of division/conference
  .search(""); // reset the table to have all teams

standings
  .column(12) // the column that holds the conference names
  .search(whichOne);  //searches for the name based on a variable.

standings
  .order([0, "desc"])
  .draw();

Upvotes: 1

Related Questions