TypeError occurs with ui-grid and Date type cells
I'm using ui-grid to show many rows of data (about 8 columns). Some of the columns are Dates. The Date columns coming back from a REST is JSON, and is encoded in the following way:
{..."myDate" : "2014-11-10", ....} as per what I understand is required for angular to interpret this as a Date.
This shows up properly in the grid upon request completion. However, when I try to double-click on the cell to edit the date, I do get the Date picking widget....BUT, there is a JavaScript error in the Console:
TypeError: Object doesn't support property or method 'getTime' at Anonymous function (http://localhost:9091/bower_components/angular-ui-grid/ui-grid.js:11309:15) at ngModelWatch (http://localhost:9091/bower_components/angular/angular.js:17776:9) at $digest (http://localhost:9091/bower_components/angular/angular.js:12533:21) at $apply (http://localhost:9091/bower_components/angular/angular.js:12806:13) at beginEdit (http://localhost:9091/bower_components/angular-ui-grid/ui-grid.js:11109:15) at dispatch (http://localhost:9091/bower_components/jquery/jquery.js:5094:6) at handle (http://localhost:9091/bower_components/jquery/jquery.js:4765:5)
On Google Chrome, the Date appears to have been correctly modified. But on IE11, we never even get the Date picking widget. Here is my partial html, controller, services, etc.
template:
<!--3rd party javascript-->
<script type="text/javascript" src="bower_components/jquery/jquery.js"></script>
<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/angular-route/angular-route.min.js"></script>
<script type="text/javascript" src="bower_components/angular-resource/angular-resource.min.js"></script>
<script type="text/javascript" src="bower_components/angular-ui-grid/ui-grid.js"></script>
<script type="text/javascript" src="bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>
<script type="text/javascript" src="bower_components/bg-splitter/js/splitter.js"></script>
<script type="text/javascript" src="bower_components/angular-spinner/angular-spinner.min.js"></script>
<script type="text/javascript" src="bower_components/spin.js/spin.js"></script>
controller JS: angular.module('myControllers') .controller('firstController', ['$scope', '$filter', '$log', '$q', '$interval', 'RowService', function ($scope, $filter, $log, $q, $interval, RowService) { ... $scope.myOverridesData = RowService.query(); $scope.gridOptions = { columnDefs: [ ... {field: 'fDate', displayName: 'First Date', type: 'date', enableCellEdit: true, cellFilter: 'date:"dd-MMM-yyyy"'}, {field: 'sDate', displayName: 'Second Date', type: 'date', enableCellEdit: true, cellFilter: 'date:"dd-MMM-yyyy"'}, {field: 'tDate', displayName: 'Third Date', type: 'date', enableCellEdit: true, cellFilter: 'date:"dd-MMM-yyyy"'}, ... ], data: "myData", //enableCellEditOnFocus: true, enableCellSelection: true, enableColumnResizing: true, enableFiltering: true, enableGridMenu: true, enableRowSelection: true, minRowsToShow: 50, multiSelect: true, rowHeight: 20, rowEditWaitInterval: 200, showFooterRow: false }; ....
services JS: 'use strict';
var myServices = angular.module('myServices', ['ngResource']);
myServices.factory('RowService', ['$resource', function ($resource) { return $resource('/app/rows', { query: {method: 'GET', isArray: true}, ... }); }]);
It appears either angular, or ui-grid, is not properly identifying model objects as Dates, even when they are Dates. Please fix this, as IE11 is broken without this.
Note that the template partial from my program didn't paste properly, here it is again:
Template:
@jpuri, is this something you've looked at before?
Hello @pmariduena,
The date editor of ui-grid behaves exactly same as date editor in angular. The value which you are using for date editing should also be javascript 'Date' type and not string.
You can check this tutorial http://ui-grid.info/docs/#/tutorial/201_editable or please let us know if you need more information.
Please share fiddle of issue, if you think its still not helping.
Hi @jpuri
Thanks for the suggestion. I do see in the example you provided, we are explicitly converting the String that comes from the JSON into a new Date() in the callback from the REST $http call. This seems to prevent the issue from occurring.
However, I still have a question since, I have explicitly declared the type as 'date" in the columnDefs for the ui-grid (see below).
$scope.myGridOptions = {
columnDefs: [
... {field: 'myDate', displayName: 'My Date', type: 'date', enableCellEdit: true, cellFilter: 'date:"dd-MMM-yyyy"'}, ... ]};
My understanding is that ui-grid should "auto-detect" the incoming data as a Date because we have set the "type" to 'date'; making the explicit JS Date conversion from the example unneeded. It seemed like we were trying to detect the date in either angular.js or ui-grid.js.
I am correct about the Date auto-detection, or is this currently working as designed?
Hi @pmariduena , ui-grid date editor works exactly like the type="date" directive in Angular 3, it does not auto detect date types. You need to provide actual javascript date type.
Hi @pmariduena,
you need to explicitly convert it to date...
On Wed, Nov 12, 2014 at 10:37 PM, pmariduena [email protected] wrote:
Rephrasing last question: Do I really need to be explicitly converting the String from the JSON to a JS Date in the callback? Or shouldn't angular and/or ui-grid just take the incoming String, compare that with the "type" set on the column definitions, and automatically convert it to a Date for me?
— Reply to this email directly or view it on GitHub https://github.com/angular-ui/ng-grid/issues/2055#issuecomment-62753693.
Regards, Jyoti
@jpuri, I think the question is whether it would be desirable to provide a feature in ui-grid that, if the column type was declared to be date, automatically did this so that the user didn't have to massage their json before passing it to the grid.
Hi @PaulL1 ,
Me and @swalters had discussions around this when the date editor feature was initially implemented, but we wanted to keep it and in fact all editors similar to their angular counterparts.
OK, understand. Closing then.
As a follow-up, I think it'd be dangerous to do behind the scenes forced conversion, as there are culture-specific ways of formatting dates (order of month and day for example) that we can't be dogmatic on. It really needs to be up to the user. On Nov 12, 2014 11:42 PM, "Paul" [email protected] wrote:
Closed #2055 https://github.com/angular-ui/ng-grid/issues/2055.
— Reply to this email directly or view it on GitHub https://github.com/angular-ui/ng-grid/issues/2055#event-192495970.
Thank you @PaulL1 and @jpuri
As a final question. Now that I'm following the tip you gave, I'm still seeing issues on IE11. When I click on the cell, it's not popping up the default date picker widget, but it does so on Chrome. Is this a separate issue, and if so, how to resolve it?
Hi, I want to implement filtering for dates i.e. less than, greater that and equal to... but not sure how to do it. Do I have to set the term to a Date object? a string? Any clue?
I think you could try a date object. But I think (from memory) it tries to convert whatever it's given into a number, and then uses that. But if you give a Date, and the column itself contains Dates, then it'll probably convert both into numbers and it'd work OK. If not, suggest raising a new issue rather than commenting on an old closed one...
This plunker seems to work fine with date objects: http://plnkr.co/edit/mtrzeRXD2oOmoDPg09my?p=preview
thank you so much @PaulL1 I'll take a look at the plunker
angular.js:11655 Unable to assign type from data, so defaulting to string, Im getting this error for Date while populating data in UIgrid.
My code is : for(i = 0; i < data.length; i++){ data[i].Date = new Date(data[i].Date);} , that displays nan infact