1

I have a grid that has a JSON date field. I want to format the field using Moment.JS but can't get the cellFormatter nor cellFilter fields to work.

See the following fiddle: http://jsfiddle.net/cooper/8NNAk/5/ that shows the problem.

I want to apply the formatting to the HTML / data-grid, not the data, for example I don't want to do this:

function viewModel(){
    myArray = ko.observableArray([
        { MyDate: moment("/Date(1355875200000+0000)/").format('DD/MM/YYYY') },
        { MyDate: moment("/Date(1355875300000+0000)/"}.format('DD/MM/YYYY')} ]);
}

Update:
This fiddle doesn't appear to be working in IE.

Charles
  • 50,943
  • 13
  • 104
  • 142
Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
  • 1
    The fiddle doesn't work because of IE blocking css and js for having mime type mismatches. For example [https://raw.github.com/timrwood/moment/1.7.2/min/moment.min.js] has a mimetype of text/plain instead of application/javascript, therefore ie blocks it. It should work fine from within your own application/ – heads5150 Jan 03 '13 at 11:26

1 Answers1

1

It should work with the following cellFilter function:

cellFilter: function(data) { return moment(data).format('DD/MM/YYYY') }

Here is how the full columndef looks like:

columnDefs: 
    [
        { field: 'MyDate', displayName: 'DateTime', 
        cellFilter: function(data) { return moment(data).format('DD/MM/YYYY') } }
    ]

Demo JSFiddle.

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • It also helps to ensure the latest version of kogrid-*.js is referenced in your project...Oops. – Mark Cooper Jan 03 '13 at 11:36
  • hi @nemesv I have this in a js view model.If I inspect the data object in chrome developer tools I can see that each row has a different value for the "Timestamp" field, however KOGrid is presenting all rows with the same value - any ideas? – Rob Bowman Apr 27 '16 at 15:04
  • 1
    @RobBowman without seeing your actual code it is quite hard to tell what could cause this. You should try to reproduce this issue in jsfiddle and ask it in a separate question. – nemesv Apr 27 '16 at 18:37
  • Yes, sorry about that - I was going to create a question then ran out of time (travelling now). I will try and get it done tonight. – Rob Bowman Apr 27 '16 at 18:42
  • Just posted a question here: http://stackoverflow.com/questions/36899396/ko-grid-repeating-value. I am trying to recreate in jsfiddle now – Rob Bowman Apr 27 '16 at 19:39