direct-fuel-injection / bbGrid

Grid system on Backbone.js and Bootstrap, sure jQuery :)
http://direct-fuel-injection.github.com/bbGrid
138 stars 59 forks source link

Datetime from json result #58

Open wickstargazer opened 10 years ago

wickstargazer commented 10 years ago

Hi, i am using bbgrid in c# MVC development. The problem i get is, the Date(xxxx) the json data gets back from the server is not rendered as date in the grid.

I am trying to find a way to solve it.

By the way, i have added a couple of events in the bbgrid code, like such

       ContentListGrid.bind('sort', function (event) {
            //console.log(event);
            $('.confirm-delete').on('click', enableDeleteButton);
        });
        ContentListGrid.bind('aftersort', function () {
            $('.star').on('click', onHighlight);
            $('.confirm-delete').on('click', enableDeleteButton);
        });
        ContentListGrid.bind('afterfilter', function () {
            $('.star').on('click', onHighlight);
            $('.confirm-delete').on('click', enableDeleteButton);
        });
        ContentListGrid.bind('afterpageChanged', function () {
            $('.star').on('click', onHighlight);
            $('.confirm-delete').on('click', enableDeleteButton);
        });

to help sustain some events after the page changes or sorting is commenced. If you would like me to submit up to branch, will do so :)

wickstargazer commented 10 years ago

This is how i solved it in the render function

switch (col.type) { case 'date': var value = self.getPropByStr(self.model.attributes, col.name); if (value) { var regex = /-?\d+/; var m = regex.exec(value); var datetimeValue = new Date(parseInt(m[0])); if (col.dateformat) { //datetimeValue.format(col.dateformat); } col.value = datetimeValue; } break; default: col.value = self.getPropByStr(self.model.attributes, col.name); break; }

putting this into the config for column

{ title: 'Published Date', name: 'PublishedOn', index: true, sorttype: 'date', type: 'date', dateformat: 'dd/m/yy' }

wickstargazer commented 10 years ago

Lastly for the formatting

if (col.dateformat) { if (!$.datepicker) throw new Error("Cannot format date, please include jquery datepicker, column name : " + col.name ); datetimeValue = $.datepicker.formatDate(col.dateformat, datetimeValue); }