dhtmlxGrid - Case Insenstive Sorting

Still not worked out if you can reply to an answer in the dhtmlx Knowledge Base???



Here is the original question with answer beneath:



dhtmlxGrid - Case Insensitive Sorting



Is there any way to make the alphabetic sorting of a standard dhtmlxGrid control case insensitive?

I couldn’t work out why when I clicked on the column header that something like N came before a!!

I realised it is because “N” is in upper case and this comes before a lower case “a”.



I don’t really want to convert the column data to lower case to make this work but that is obviously one solution.

So was wondering if there was an option somewhere to make the sorting case insensitive.



If I have to write a custom sort function, can anyone give me a code snippet to do this.



Answer posted on Nov 09, 2007 05:47

By default sorting is case sensitive, you can change such behavior by using custom sorting.



grid.setCustomSorting(function(a,b,order){

a=(a||"").toString().toLowerCase();

b=(b||"").toString().toLowerCase();



if (order) return (a>b?1:-1);

else return (a<b?1:-1);



},INDEX);



where INDEX - index of column for which custom sorting must be used



I didn’t realise when I posted my original question but Custom Sorting is only available in the Professional Edition of the Grid.

Fair enough but is there really no other way to switch off case sensitive (alphabetic) sorting?

I would have thought that (case insensitive) alphabetic sorting was “Standard” behaviour?



Can I have a hidden column (Column Y) in a grid which contains the same data as (visible) Column X but is all in lower case.

Then when I click on the header in Column X, it sorts according to Column Y.

Just a thought.

>>Still not worked out if you can reply to an answer in the dhtmlx Knowledge Base???

You can more complex frontend for KB
    dhtmlx.com/docs/products/kb/index.php


>>I would have thought that (case insensitive) alphabetic sorting was “Standard” behaviour?
yes, the flexible way available only in pro edition, but you can switch any “str” sorting to case insentensive by next code modification

dhtmlxgrid.js , line 1477-1483 ( related to not compressed version of code )


             if(type==‘str’){
             s[sort](function(a,b){
             if(order==“asc”)
                  return (arrTS[a.idd]||"").toString().toLowerCase()>(arrTS[b.idd]||"").toString().toLowerCase()?1:-1 //modified
             else
                  return (arrTS[a.idd]||"").toString().toLowerCase()<(arrTS[b.idd]||"").toString().toLowerCase()?1:-1 //modified
             });

by such modification all “str” type sorting will work case insensitive


>>Can I have a hidden column
(Column Y) in a grid which contains the same data as (visible) Column X
but is all in lower case.


>>Then when I click on the header in Column X, it sorts according to Column Y.


Actually this is possible as well, grid provides event for header click so you can have something similar to next

    grid.attachEvent(“onHeaderClick”,function(ind){
       if (ind == INDEX){
          grid.sortRows(INDEX2,“str”,“asc”);
          return false;
        }
       return true;
    });


The modifications to dhtmlxgrid.js work fine - I now have case insensitive sorting for all “str” columns.
Thanks for that.

As to the final point, this doesn’t quite do what I expected:
    >>Can I have a hidden column
(Column Y) in a grid which contains the same data as (visible) Column X
but is all in lower case.

    >>Then when I click on the header in Column X, it sorts according to Column Y.

    Actually this is possible as well, grid provides event for header click so you can have something similar to next

    grid.attachEvent(“onHeaderClick”,function(ind){
       if (ind == INDEX){
          grid.sortRows(INDEX2,“str”,“asc”);
          return false;
        }
       return true;
    });

This has all come about because I cannot get “date” sorting to work!!
So I now have column 7 containing my date in, what I thought was, “dd/mm/yyyy hh:mm:ss” format.
And in column 8, I have the same date in “YYYYMMDDhhmmss” format - this I know for a fact is sortable as an “str”.
Column 8 is zero width (ie hidden).

I added code to intercept the clicking of the header column for column index 7.
And I added code to do the grid.sortRows on column index 8 in this instance.

However, this always sorts ascending due to parameter 3 being passed as “asc”.
How can I tell what is the current direction of the sort for a particular column?
I used grid.imgSort.src and extracted the image filename to give me a clue but there must be a better way???

Regardless, after the sort, because of the return false immediately afterwards, it does not display the direction indicator in the column header.
The sort indicator remains on the previously sorted column.

If I can’t get this to work, maybe a different approach would be to ask if it’s possible to have the data in the cell as one thing but display something else?
In other words, the physical data in cell[x] contains the date in YYYYMMDDhhmmss format; but it displays the value in dd/mm/yyyy hh:mm:ss format?
Is this possible?
I then want to sort on the physical data whilst still showing the formatted date.

How can I tell what is the current direction of the sort for a particular column?
There is two functions which can be used to get and set sorting state

to get current state
    var state=grid.getSortingState();
    state[0] - index of column by which grid was sorted last time
    state[0] - direction of sorting ( asc or des )

>>The sort indicator remains on the previously sorted column.

    grid.setSortImgState(state,ind,order);
    grid.setSortImgState(true,INDEX,‘asc’);

>>In other words, the physical data in cell[x] contains the date in
YYYYMMDDhhmmss format; but it displays the value in dd/mm/yyyy hh:mm:ss
format?
>>Is this possible?
It possible by creation custom excell, which can show one data and return different data as its value (all string formating excells use such approach ( dyn, price, etc ), while showing formated value they used original one while sorting )