Author Topic: Options in columnSelector sorted  (Read 1980 times)

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Options in columnSelector sorted
« on: September 15, 2017, 01:19:18 pm »
Would it be possible to have the options in the columnSelector sorted instead of using the colModel order?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Options in columnSelector sorted
« Reply #1 on: September 15, 2017, 05:48:27 pm »
Since options is javascript array, native array sort method can be used to sort it.

//if options is array of strings.
options.sort();

//if options is array of objects
Code: [Select]
options.sort( function( obj1, obj2 ){
  return obj1[ prop ] - obj2 [ prop ];
})

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Options in columnSelector sorted
« Reply #2 on: September 15, 2017, 07:49:55 pm »
Seems to be array of objects:

Code: [Select]
options: function(ui) {
        //options in the select list correspond to all columns.
        return this.getColModel().map(function(column) {
          var obj = {};
          obj[column.dataIndx] = column.title;
          return obj;
        });
      },

But where do I execute the sort method?
« Last Edit: September 15, 2017, 07:52:59 pm by queensgambit9 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Options in columnSelector sorted
« Reply #3 on: September 16, 2017, 09:59:08 am »
Code: [Select]
options: function (ui) {

//options in the select list correspond to all columns.
return this.getColModel().map(function(column){
var obj = {};
obj[ column.dataIndx ] = column.title;
return obj;
}).sort(function(col1, col2){
var val1 = col1[ Object.keys(col1)[0] ],
val2 = col2[ Object.keys(col2)[0] ];
return val1.localeCompare(val2);
});
},