mleibman / SlickGrid

A lightning fast JavaScript grid/spreadsheet
http://wiki.github.com/mleibman/SlickGrid
MIT License
6.81k stars 1.98k forks source link

The sorting works correct on some columns and wrong on some of them... #685

Closed esameer closed 11 years ago

esameer commented 11 years ago

Hi, Below is my code for the grid var options = { editable: true, enableCellNavigation: true, multiColumnSort: true };

    var columns = [
      {id: "id", name: "id", field: "id", resizable: true,sortable:true},
      {id: "JOGITEM", name: "Product", field: "JOGITEM", resizable: true},
      {id: "JOGDESC", name: "Description", field: "JOGDESC",resizable: true, formatter: linkFormatter},
      {id: "QTY", name: "QTY", field: "QTY", resizable: true, editor: QuantityEditor,sortable:true},
      {id: "JPRUOM", name: "UOM", field: "JPRUOM", resizable: true,editor: UOMSelectEditor},
      {id: "JPACK", name: "Pack", field: "JPACK", resizable: true},
      {id: "JSIZE", name: "Size", field: "JSIZE", resizable: true},
      {id: "JBRAND", name: "Brand", field: "JBRAND", resizable: true},
      {id: "JCUSTMAT", name: "CIN", field: "JCUSTMAT", resizable: true,sortable:true},
      {id: "JWEEKAVG", name: "WA", field: "JWEEKAVG", resizable: true,formatter: WKAVGFormatter,sorter: NumericSorter,sortable:true}

    ];

    for (var key in str) {

    var d = (jsonObj[key] = {});
    d["id"] = key;
    d["JOGITEM"] =  str[key].JOGITEM.toString();
    d["JOGDESC"] =  str[key].JOGDESC.toString();
    d["QTY"] =  "";
    d["JPRUOM"] =  str[key].JPRUOM.toString();
    d["JPACK"] =    str[key].JPACK.toString();
    d["JSIZE"] =    str[key].JSIZE.toString();
    d["JBRAND"] =  str[key].JBRAND.toString();
    d["JCUSTMAT"] = str[key].JCUSTMAT.toString();
    d["JWEEKAVG"] = str[key].JWEEKAVG.toString();
    d["JPRUOM_HIDE"] =  str[key].JALLUOM.toString();;
    d["hidden"] =  str[key].JOGITEM.toString() + str[key].JPRUOM.toString() + str[key].JBRAND.toString() + str[key].JOGDESC.toString() + str[key].JCUSTMAT.toString() ;
}

    dataView1 = new Slick.Data.DataView({ inlineFilters: true });
    grid = new Slick.Grid($("#myGrid"),dataView1, columns, options);

grid.onSort.subscribe(function (e, args) {
  var cols = args.sortCols;
  var comparer = function (dataRow1, dataRow2) {
    for (var i = 0, l = cols.length; i < l; i++) {
      var field = cols[i].sortCol.field;
      var sign = cols[i].sortAsc ? 1 : -1;
      var value1 = dataRow1[field], value2 = dataRow2[field];
      var result = (value1 == value2 ? 0 : (value1 > value2 ? 1 : -1)) * sign;
      if (result != 0) {
        return result;
      }
    }
    return 0;
  };

  dataView1.sort(comparer);
  grid.invalidate();
  grid.render();
});

finally I have

    grid.setSelectionModel(new Slick.RowSelectionModel());

    //grid.init();

    dataView1.beginUpdate();
    dataView1.setItems(jsonObj);
    dataView1.setFilter(myFilter);
    dataView1.endUpdate();
    // if you don't want the items that are not visible (due to being filtered out
    // or being on a different page) to stay selected, pass 'false' to the second arg
    dataView1.syncGridSelection(grid, false);

    //$("#gridContainer").resizable();
    //the event bellowj just triggers keyup on the filter text box so that the grid gets populated...never delete this statement...
    $("#txtSearch").triggerHandler("keyup");

The issue with this example is the sort works perfectly for some columns example "CIN" column, but works pretty bad for some columns like "id" and "JWEEKAVG".

Check the below images .

before sort on id: before_sort after sorting on id field: after_sort_on_id

mleibman commented 11 years ago

You're doing a lexicographic sort in your comparer, which makes sense since you're comparing strings.

esameer commented 11 years ago

Thanks a lot sir !! you deserve a beer :) will do my bit for making this grid popular among my office fellas!