JPN
JPN

Reputation: 683

How to sort numeric with string values in Kendo-Grid

I am using Kendo-Grid which is having a column has values of both number & string (NA). Any idea how to sort them?

Upvotes: 8

Views: 12348

Answers (4)

JPN
JPN

Reputation: 683

Its working with the code avilable in http://jsbin.com/egoneWe/3/edit

Upvotes: 1

Atanas Korchev
Atanas Korchev

Reputation: 30671

You can sort them using a custom compare function. Here is some sample code which will put items with 'N/A' on top:

$("#grid").kendoGrid({
  dataSource: [
    { price: 1 },
    { price: "N/A" },
    { price: 20 },
    { price: 2 }
  ],
  sortable: true,
  columns: [
    {
      field: "price",
      sortable: {
        compare: function(a, b) {
          var x = a.price;
          var y = b.price;

          if (x == 'N/A') {
            x = 0;
          }

          if (y == 'N/A') {
            y = 0;
          }

          return x - y;
        }
      }
    }
  ]
});

Here is a live demo: http://jsbin.com/urUXOCa/1/edit

Upvotes: 8

Robin Giltner
Robin Giltner

Reputation: 3055

As far as I know, there isn't a way to create your own sort function your for a field. I did find http://sympletech.com/how-to-enable-case-insensitive-sorting-on-kendo-ui-grid/ where someone implemented something like what you are asking (he was just doing case in-sensitive sorting).

I've had to do this once (thankfully on a non-editable grid, was just showing data), and I just sort of tricked the grid using a template. By going through the data before it was dataBound, adding another property to it that represented the data that would sort properly, and binding the grid to that column instead of the original column, but using a template that returned the original data value.

See jsbin http://jsbin.com/ETaZOSu/1/edit

Upvotes: 0

aaron
aaron

Reputation: 687

Make the field values as numbers. Then, if required, add the string at the time of displaying.

Kindly refer this for help :sorting numbers (dollar amt and percentage) not working

Upvotes: 0

Related Questions