【问题标题】:Kendo UI Angular 2 grid date editor TypeError: date.getTime is not a functionKendo UI Angular 2 网格日期编辑器 TypeError:date.getTime 不是函数
【发布时间】:2018-04-20 15:48:29
【问题描述】:

我有一个网格,我将列编辑器设置为 [editor]="'date'",现在当我尝试编辑单元格时它会抛出错误。我收到以下错误。 json 日期看起来像一个 ISO 格式的日期,我认为这可能是问题,但我不知道如何解决它。

{"Date":"2016-12-14T00:00:00"}

ERROR TypeError: date.getTime is not a function
    at Object.exports.cloneDate (clone-date.js:15)
    at DatePickerComponent.writeValue (datepicker.component.js:295)
    at setUpControl (shared.ts:57)
    at FormControlDirective.ngOnChanges (form_control_directive.ts:107)
    at checkAndUpdateDirectiveInline (provider.ts:276)
    at checkAndUpdateNodeInline (view.ts:519)
    at checkAndUpdateNode (view.ts:462)
    at debugCheckAndUpdateNode (services.ts:389)
    at debugCheckDirectivesFn (services.ts:476)
    at Object.eval [as updateDirectives] (CellComponent.html:17)

【问题讨论】:

    标签: angular kendo-ui-angular2


    【解决方案1】:

    您可能必须将日期字符串更改为有效的日期对象。

    如果您从 api 调用中获取 if,则可以使用 Observables 自动转换该日期字符串。 Angular 2 Date deserialization

    一般而言,您只需说 new Date('2016-12-14T00:00:00') 即可获得有效的 Date 对象。 请注意,您的日期缺少时区信息。有效的 iso 字符串如下所示:2016-12-14T00:00:00.000Z

    【讨论】:

    • 这可能是解决方案,意思是链接的帖子,但它不适合我。我在另一篇文章中发布了我的问题。
    • 为什么我在这一行得到一个错误:var data = res.json().data || [];当我尝试使用此代码时? "Promise 类型上不存在属性数据"
    【解决方案2】:

    对于同样遇到此错误的人

    如果你有像kendo reactive form example 这样的网格,你可以这样做:

    ts:

       //form initialization
        const createFormGroup = dataItem => new FormGroup({
      'publicationId': new FormControl(dataItem.publicationId), 
      'publicationDate': new FormControl(new Date(dataItem.publicationDate), Validators.required),
      'authors': new FormControl(dataItem.authors)
    });
    
    //new form creating
    public createForm() {
    this.formGroup = this.fb.group({
      'publicationDate': new Date(),
      'authors': new FormControl()
    });
    }
    

    html:

     <kendo-grid-column field="publicationDate" title="Publication Date" format="{0: dd MMM yyyy}" editor="date" width="160">
    <ng-template kendoGridCellTemplate let-dataItem>
      {{log(dataItem)}}
     
      {{dataItem.publicationDate | date:ShortDate}}
    </ng-template>
    

    希望对某人有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多