Changing how a column sorts

Changing how a column sorts

CVernonCVernon Posts: 3Questions: 1Answers: 0
edited September 2016 in Free community support

I currently have a defined column render for a specific column like such:

"render": function (data, type, row, meta) {
return '<a href="' + getLink(data) + '">' + data[0] + </a>;
}

My issue is that the data variable is an array, such as [0, 1, "String", 1, "AnotherString", 4, "OneMoreString"] etc.
The reason the data variable is an array is that it contains properties to generate the link for the <a> element surrounding the value.

When I try to sort asc or desc on this column it always messes up and doesn't sort based on data[0], in which I want to as that is the value being displayed.

An example of data:
[0] => [4, 3, "string", 1, "string", 1, "string", 2, "string", 0, "string"]
[1] => [5, 1, "string", 1, "string", 1, "string", 1, "string", 1, "string" ]
[2] => [5, 1, "string", 1, "string", 1, "string", 1, "string", 1, "string" ]
[3] => [1, 1, "string", 0, "string", 0, "string", 0, "string", 0, "string" ]

As I'm displaying data[0], the following data is put into the table, row by row:
4, 5, 5, 1

When I sort them I'd expect to sort them as such:
1, 4, 5, 5 or 5, 5, 4, 1

But what happens is:
4, 5, 5, 1 or 1, 5, 5, 4

This question has an accepted answers - jump to answer

Answers

  • CVernonCVernon Posts: 3Questions: 1Answers: 0
    edited September 2016

    == Didn't mean to input answer ==

  • DirceuNazarethDirceuNazareth Posts: 44Questions: 1Answers: 12
    edited September 2016 Answer ✓

    Currently, your data is the result of the render function.
    So, first you need to set your data properly. There are many ways to do this job...
    My suggestion is:

    Not user columns.render, instead use columns.createdCell because it will not affect the sort.

    so the code will be something like this:

    {
      "data": yourData,
      "render": function (data, type, row, meta) {
          return data[0];
       },
       "createdCell": function( cell, cellData, rowData, rowIndex, colIndex ){
           var theData = rowData.yourData;
           $(cell).html( '<a href="' + getLink(theData) + '">' + theData[0] + '</a>');
      },
      "type": "num"
    }
    

    As I said there are a variety of way to solve the problem.

  • CVernonCVernon Posts: 3Questions: 1Answers: 0

    Thank you, this worked perfectly :)

This discussion has been closed.