代码之家  ›  专栏  ›  技术社区  ›  Chan Yoong Hon

角度4:来自Web服务的ag网格绑定数据

  •  0
  • Chan Yoong Hon  · 技术社区  · 7 年前

    我成功地从后端获取数据。但我没能把它绑定到我的ag网格上。 有什么解决办法吗?怎么解决?

    component.html

          <ag-grid-angular #agGrid style="width: 100%; height: 570px;" class="ag-theme-balham"
    
              [enableSorting]="true"
              [pagination]="true"
              [gridOptions]="gridOptions"
              (gridReady)="onGridReady($event)">
          </ag-grid-angular>
    

    组件技术

    constructor(private router: Router, private _profileDistService: ProfileDistService) { 
      this.gridOptions = <GridOptions>{
          paginationPageSize: 18,
          animateRows: true,
          rowSelection: 'multiple'
        };
      this.gridOptions.columnDefs = [
        {
            field: "",
            width: 110,
            checkboxSelection: true
    
        },
        {
            headerName: "Distributor Code",
            field: "distCd",
            width: 330,
            sortingOrder: ["asc", "desc"]
        },
        {
            headerName: "Distributor Name",
            field: "distName",
            width: 330,
            sortingOrder: ["asc", "desc"]
        },
        {
            headerName: "Status",
            field: "status",
            width: 330,
            sortingOrder: ["asc", "desc"]
        },
        {
            field: "", 
            width: 110, 
            cellRenderer: (data) => {
                return `<mat-icon class="mat-icon material-icons mat-icon-no-color" role="img" aria-hidden="true">
                keyboard_arrow_right</mat-icon>`;
            },
            onCellClicked: (event)=> {
                this.router.navigateByUrl('distributors/General/Edit');
            }
        }
    ];
    const tenantID=   '86930E70D74EF95E16000C02475A5357' ;
    this._profileDistService.getProfileDist(tenantID).subscribe((DistData) => {
        var DistDataArray = Object.values(DistData);
        this.gridOptions.rowData = DistDataArray;
    },
    response => {
        console.log("Error : " + JSON.stringify(response));  
    });
    
    
    }
    ngOnInit() {
        this.showGridList = true;
        this.showDistDetail = false;
    }
    
    
    onGridReady(params){
        console.log(params);
        this.gridApi = params.api;
        this.gridColumnApi = params.columnApi;
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Paritosh    7 年前

    尝试将数据直接绑定到网格-而不使用 gridOptions .

    <ag-grid-angular #agGrid style="width: 100%; height: 570px;" class="ag-theme-balham"
          [enableSorting]="true"
          [rowData]="rowData"
          [pagination]="true"
          [gridOptions]="gridOptions"
          (gridReady)="onGridReady($event)">
    </ag-grid-angular>
    

    声明 rowData 作为属性并在组件内部初始化 this.rowData = [] .

    this._profileDistService.getProfileDist(tenantID).subscribe((DistData) => {
      var DistDataArray = Object.values(DistData);
      this.rowData = DistDataArray;
    }
    

    我想 用于配置网格的初始状态。所以,如果你已经 行数据 当你定义的时候 网格选项 ,则只考虑数据。

    推荐文章