John Maddox
John Maddox

Reputation: 1

jqGrid text Sort Order

I have a jqGrid that I set up like this

gridAltMpn.jqGrid({ 
  autowidth: true,
  shrinkToFit: true,
  datatype : 'local', 
  data : input, 
  height : '100',
  scrollrows: true,
  scrollOffset : '0', 
  hidegrid : false,
  colNames : [ 'P', 'MPN' ],
  colModel : [ 
     { name : 'Col1', width : 30, align:'center' }, 
     { name : 'Col2', width : 250, sorttype: 'integer'} 
  ],
  pager : '#altmpn_pager', 
  pagerpos : 'left', 
  scroll: 50,
  gridview : true, 
  caption : 'A useful table title',
  emptyRecordText : '<div id="no_data_msg" style="text-align:center"> No Results Found</div>', 
  hoverrows : true,      
  onSelectRow: function(id) {
     var gsr = gridAltMpn.jqGrid('getGridParam', 'selrow');
     if (gsr) {
        var rowData = gridAltMpn.jqGrid('getRowData', gsr);
        if ($("input[name='optInvInqType']:checked").val() == 'MPN') {
           getInvInq("MPN", rowData.MPN);
        }
     }
  },
  loadComplete: function() { 
     gridAltMpn.setSelection(gridAltMpn.getDataIDs()[0], true); 
  }
});

The data in this grid looks like this

XX 774860A6                   
   774860A8                   
   774860A4                   
   774860A3                   
   774860A10                  
   STARTER, PNEUM,PW4000 

When the grid is first loaded that it fine but if the user wants to sort by the second column it ends up like this

   774860A10                  
   774860A3                   
   774860A4                   
XX 774860A6                   
   774860A8                   
   STARTER, PNEUM,PW4000      

The 774860A10 should go after the 774860A8 just like in an integer sort. I cannot use an integer sort because these are not integers as there is some alpha characters in there. In other words I want a text entry to sort like an integer. Do I need to use a custom sort routine and then have my Javascript to do a integer like sort? I also don't need this sorted on the first time because my server sorts it by the first column. The user might want it sorted by the second column

Upvotes: 0

Views: 2174

Answers (1)

Sharun
Sharun

Reputation: 3082

You should use a custom function for this type of sorting.

For this, set sort type property of jqgrid as your custom function. As stated in this link, sort type can have the following values.

sorttype:

int/integer - for sorting integer
float/number/currency - for sorting decimal numbers
date - for sorting date
text - for text sorting
function - defines a custom function for sorting. To this function we pass the value to be sorted and it should return a value too.

And the custom function can be something like this: (From this link found in the answer by Oleg for this question.)

 colModel: [

{name:'Posn', index:'Posn', width:100, sorttype:

function(cell)

{
//Here you have to apply your own logic
if (cell=='GK') return '0';//returns the sort order 

if (cell=='DEF') return '1';

if (cell=='MID') return '2';

if (cell=='STR') return '3';

}

},

By the way, you can set the sortname property of jqgrid to set a column for initial loadtime sorting.

Upvotes: 1

Related Questions