gpt4 book ai didi

Angular 2 -Kendo UI 网格。排序实现的问题

转载 作者:搜寻专家 更新时间:2023-10-30 21:30:24 25 4
gpt4 key购买 nike

我正在 visual studio 中使用 asp.net 开发一个 angular 2 应用程序。呈现的 HTML 使用 Kendo UI 数据网格。我正在尝试实现、滚动和排序。滚动效果很好。但是在尝试实现排序时遇到问题。我收到一个错误

错误是“一个对象文字在严格模式下不能有多个同名的属性。重复的标识符数据”。这是编译时错误

此错误涉及 loadRisks() 方法中的数据属性分配。我是 typescript 的新手,需要一些帮助。如何将 order by 子句分配给数据网格。有人可以帮忙吗。

risk-list.component.ts

import { Component, OnInit } from '@angular/core';
import { Risk } from './risk';
import { RiskService } from './risk.service';
import { GridModule, GridDataResult, PageChangeEvent, SortDescriptor, orderBy } from '@progress/kendo-angular-grid';

@Component({
moduleId: module.id,
selector: 'rm-risks',
templateUrl: '/app/risk-list.component.html',
providers: [RiskService]
})

export class RiskListComponent implements OnInit {
private gridView: GridDataResult;
private sort: SortDescriptor[] = [];
private data: any[];
private pageSize: number = 10;
private skip: number = 0;
title = 'Risk List';
risks: Risk[];

constructor(private _riskService: RiskService) {
this.data = [];
this.getRisks();
this.loadRisks();
}

protected pageChange(event: PageChangeEvent): void {
this.skip = event.skip;
this.loadRisks();
}

protected sortChange(sort: SortDescriptor[]): void {
this.sort = sort;
this.loadRisks();
}


private loadRisks(): void {
this.gridView = {
data: this.data.slice(this.skip, this.skip + this.pageSize),
data: orderBy(this.risks, this.sort),
total: this.data.length
};
}

getRisks(): void {
this._riskService.getRisks().then(risks => this.risks = risks);

}

ngOnInit(): void {
this.getRisks();
}
};

risk-list.component.html

<kendo-grid [data]="risks"
[skip]="skip"
[pageSize]="pageSize"
[scrollable]="'virtual'"
[rowHeight]="36"
[height]="300"
(pageChange)="pageChange($event)"
[sortable]="{ mode: 'multiple' }"
[sort]="sort"
(sortChange)="sortChange($event)">
<kendo-grid-column field="reference" title="Reference" width="120">
</kendo-grid-column>
<kendo-grid-column field="insuredName" title="Insured Name" width="120">
</kendo-grid-column>
<kendo-grid-column field="inceptionDate" title="Inception Date" width="120">
</kendo-grid-column>
<kendo-grid-column field="riskType" title="Risk Type" width="120">
</kendo-grid-column>
<kendo-grid-column field="status" title="Indication" width="120">
</kendo-grid-column>
<kendo-grid-column field="grossPremium" title="Gross Premium" width="120">
</kendo-grid-column>
<kendo-grid-column field="allocatedTo" title="Allocated To" width="120">
</kendo-grid-column>
<kendo-grid-column field="allocatedCompany" title="Allocated Company" width="120">
</kendo-grid-column>
<kendo-grid-column field="Discontinued" width="100">
<template kendoCellTemplate let-dataItem>
<input type="checkbox" [checked]="dataItem.Discontinued" disabled />
</template>
</kendo-grid-column>
</kendo-grid>

risk.service.ts

import { Injectable } from '@angular/core';
import { Risk } from './risk';
import { Risks } from './mock-risk';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';


@Injectable()
export class RiskService {

getRisks(): Promise<Risk[]> {
return Promise.resolve(Risks);
}

}

最佳答案

该错误意味着您不能像在 loadRisks 方法中那样拥有重复的属性。查看对象中重复的 data 属性。

private loadRisks(): void {
this.gridView = {
data: this.data.slice(this.skip, this.skip + this.pageSize),
data: orderBy(this.risks, this.sort),
total: this.data.length
};
}

您应该合并这些表达式或创建一个返回所需数据值的函数。一个例子可以是:

private loadRisks(): void {
this.gridView = {
data: this.handleData(),
total: this.data.length
};
}

private handleData() {
var pagedData = this.data.slice(this.skip, this.skip + this.pageSize)
if (!this.sort) { return pagedData; }

var orderedAndPagedData = orderBy(pagedData, this.sort);
return orderedAndPagedData;
}

Plunkr:http://plnkr.co/edit/ZtaExNxX9eOQrCzYwO1N?p=preview

关于Angular 2 -Kendo UI 网格。排序实现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40200596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com