Sagar Khurd
Sagar Khurd

Reputation: 15

Date Sort Method in W2ui Grid

I want to sort the W2ui grid date column in 'dd/mm/yyy' but when multiple records are in grid the date not sorted properly

i am using w2ui['grid'].sort('dd/mm/yyyy',event)

Upvotes: 0

Views: 1296

Answers (2)

ÖMER TAŞCI
ÖMER TAŞCI

Reputation: 566

Sorting on a date column on w2ui grid should be like below. You should provide an extra field, name as "yourDateSortable", which contains sortable string properly. In my example "yourDateSortable" is formated version of "yourDate" in "YYYYMMDD" date format. Notice that "yourDateSortable" is hidden field. It is used for only sorting instead of "yourDate".

var dataParam = [
{
"id": 1,
"data": {
  "yourDate": "27/09/2014 00:00:00",
  "yourDateSortable": "20140927"
}
},
{
"id": 2,
"data": {
  "yourDate": "01/03/2014 00:00:00",
  "yourDateSortable": "20140301"
}
},
{
"id": 3,
"data": {
  "yourDate": "10/01/2022 00:00:00",
  "yourDateSortable": "20220110"
}
},
{
"id": 4,
"data": {
  "yourDate": "25/04/2015 00:00:00",
  "yourDateSortable": "20150425"
}
}
];

$('#resultList').w2grid({
...
columns: [
{field: 'data.yourDateSortable', text: 'Your_Date_Field Hidden', size: '90px', min: '150', sortable: true, hidden:true },
{field: 'yourDateVisible', text: 'Your_Date_Field', size: '90px', min: '150', sortable: true, 
     render: function (record) {
            var a = moment(record.data.yourDate, "DD/MM/YYYY hh:mm").format('DD/MM/YYYY hh:mm') ;
                    return '<span title="' + a + '">' + a + '</span>';
                } }
],
records: dataParam,
...
onSort: function (event){
            if (event.field == "yourDateVisible") {
                event.preventDefault();
                w2ui['grid'].sort('data.yourDateSortable');
                event.onComplete = function () {
                    w2ui['grid'].sort('data.yourDateSortable');
                }
            }
        }
});

Upvotes: 0

Mike Scotty
Mike Scotty

Reputation: 10782

Sorry, but your code w2ui['grid'].sort('dd/mm/yyyy',event) does not make any sense.

Assuming your grid has a column called date that you want to sort descending, you'd have to call: w2ui.grid.sort('date', 'desc')

(Note that w2ui['grid'] and w2ui.grid is the same, you can pick whichever syntax you prefer).

Please see the docs for grid.sort: http://w2ui.com/web/docs/w2grid.sort

If this does not help you should provide a basic jsfiddle, with your grid columns and a few sample records.

Upvotes: 0

Related Questions