JavaScript Grid

Invoke the sortby method of the jqxGrid. Author: http://jqwidgets.com

by Don Fleming

HTML

<script src="http://www.jqwidgets.com/jquery-widgets-demo/demos/jqxgrid/generatedata.js"></script>
<link rel="stylesheet" href="http://jqwidgets.com/public/jqwidgets/styles/jqx.base.css">
<link rel="stylesheet" href="http://jqwidgets.com/public/jqwidgets/styles/jqx.energyblue.css">
<script src="http://jqwidgets.com/public/jqwidgets/jqx-all.js"></script>
<div><p>Sort by clicking on a column header to see 'direction' parameter sent to custom sort function.'</p>
    <p>Sort by drop menu on column header to see 'direction' parameter set to custom sort function.</p>
</div>
<input type="button" style="margin: 10px;" id="jqxbutton" value="Sort the first column using sortby desc" />
<div id='output'>sort using getstate loadstate : </div>

<div id='jqxWidget'>
    <div id="jqxgrid"></div>
</div>

JavaScript

var data = generatedata(500);

 var customsortfunc = function (column, direction) {
     var sortdata = [];
     $("#output").html(
         $("#output").html() +  direction + "<br />"
     );
     if (direction == 'ascending') direction = true;
     if (direction == 'descending') direction = false;
     if (direction !== null) {
         for (i = 0; i < data.length; i++) {
             sortdata.push(data[i]);
         }
     } else sortdata = data;
     var tmpToString = Object.prototype.toString;
     Object.prototype.toString = (typeof column == "function") ? column : function () {
         return this[column];
     };

     if (direction !== null) {
         sortdata.sort(compare);
         if (!direction) {
             sortdata.reverse();
         }
     }
     source.localdata = sortdata;
     $("#jqxgrid").jqxGrid('databind', source);
     Object.prototype.toString = tmpToString;
 };

 // custom comparer.
 var compare = function (value1, value2) {
     value1 = String(value1).toLowerCase();
     value2 = String(value2).toLowerCase();
     try {
         var tmpvalue1 = parseFloat(value1);
         if (isNaN(tmpvalue1)) {
             if (value1 < value2) {
                 return -1;
             }
             if (value1 > value2) {
                 return 1;
             }
         } else {
             var tmpvalue2 = parseFloat(value2);
             if (tmpvalue1 < tmpvalue2) {
                 return -1;
             }
             if (tmpvalue1 > tmpvalue2) {
                 return 1;
             }
         }
     } catch (error) {
         var er = error;
     }
     return 0;
 };

 var source = {
     localdata: data,
     datafields: [{
         name: 'firstname',
         type: 'string'
     }, {
         name: 'lastname',
         type: 'string'
     }, {
         name: 'productname',
         type: 'string'
     }, {
         name: 'date',
         type: 'date'
     }, {
         name: 'quantity',
         type: 'number'
     }, {
        ...