Sorting by a column, then freezing some rows, then sorting by a different column.

Sorting by a column, then freezing some rows, then sorting by a different column.

PyrolightPyrolight Posts: 2Questions: 1Answers: 0

Here is the situation. I have two columns that need to be sorted, one is fruit and the second is date.

What I am looking to do is sort by the first column so that specific fruit is at the top and "freezing" those rows. Then a sort on the Date column would sort the frozen rows, keep them at the top and sort the rest of the rows by date.

Example: apple to be first

apple, 1980/8/1
orange, 1980/2/1
pear, 1980/5/1
orange, 1980/4/1
apple, 1980/1/1
pear, 1980/3/1

So:

apple, 1980/1/1
apple, 1980/8/1
orange, 1980/2/1
pear, 1980/3/1
orange, 1980/4/1
pear, 1980/5/1

Apple would always be first then sorted by date. The rest would just be sorted by date.

I did figure out a solution the ended up pulling "apple" out, sorting it and putting it back at the top but it was far from elegant. This did not allow for sorting via the asc/desc buttons.

Answers

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    I would consider using columns.render for this to affect sorting. Haven't tested it but I think something like this would work:

        render: function ( data, type, row, meta ) {
          if (type === 'sort') {
            return data === 'apple' ?
            '' :
            data;
          }
          return data;
        }
    
    

    You would replace 'apple' with the variable containing the fruit you want at the top. Of course you will need to setup your column ordering properly to have the date column follow the fruit column.

    Kevin

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Had time to check this out and found my initial answer is not quite correct. I created an example that seems to work:
    http://live.datatables.net/sowidusi/1/edit

    It uses columns.render but differently. If the row has "apple" or whatever is selected it subtracts 1000 years from the date just for the sorting process. It leaves the date alone for the other processes. This subtraction of 1000 years may or may not work for you but hopefully will get you headed in the right direction. Here is the updated columns.render:

           render: function ( data, type, row, meta ) {
             if (type === 'sort') {
               var dt = new Date(data);  //convert all to JS Date object
               return row[0] === newValue ?  
                  dt.setYear(dt.getYear - 1000) :  //if fruit col = selected value return Date object -1000 years
                  dt;   //otherwise just return current date
              }
              return data;   //return unmodified field for display and filter operations
            }
    

    Kevin

  • PyrolightPyrolight Posts: 2Questions: 1Answers: 0

    Sorry for the delay in replying, end of a sprint so didn't have time to check back. Will have to check this out now that I have a little time. Thanks.

This discussion has been closed.