I have the following columnDefs
self.columnDefs = [
{ width: 150, field: 'timeReceived', displayName: 'Time Received', cellFilter: function (data) { return moment(data).format('DD/MM/YYYY h:mm a') } },
{ width: 500, field: 'name', displayName: 'Name' },
{ width: 150, field: 'LinkToTimeSent', displayName: 'Time SentX', cellTemplate: '<a data-bind="text:$parent.entity.timeSent, attr: { href: $parent.entity.linkToTimeSent}" ></a>' },
];
My problem is with the Time SentX. I'd like this to display the content of entity.timeSent but converted for human consumption using the moment library.
How can I call the function moment($parent.entity.timeSent).format('DD/MM/YYYY h:mm a') from within my columnDefs?
In the following plunk, line 96 needs to contain something like
text:moment($parent.entity.TimeSent, "DD/MM/YYYY h:mm a") but I can't get it to work!
Edit: My answer was a bit too general. An attempt to be more specific.
Map your
WorkflowRulesto their own "viewmodels", and you can do anything you like:Then, in your template:
Which is defined in js:
I hope I finally got your question correctly...
(https://plnkr.co/edit/93ucvDLk5bUFtU4dB1vn?p=preview, moved some stuff around)
Previous, more general answer:
When you create a knockout binding, knockout automatically wraps the second part of the binding in a function. For example:
Is processed as:
Additionally, knockout uses this function to create a
computedObservable. This will create a subscription to any observable used inside the function, making sure the data-bind is re-evaluated if any of them changes.This means that in your case, you can define your format rule inside your data-bind like so (assuming
timeSentis an observable`):Knockout will see that the
timeSentobservable is called and make sure the whole binding gets updated correctly. Here's an example:My advice however is to create a separate computed observable inside your viewmodel. After all, this is what viewmodels are meant to do, and it will help you out a great deal when fixing bugs. I.e.: