0

我的数据如下:

var customers = [
    {
     "ID": 1,
     "Name":"John Mayor"
     "Company": [{"ID": 1, "Name":"Super Mart"},{"ID": 2, "Name":"Big Mart"}]
     "CompanyID":1
    }, {
     "ID": 2,
     "Name": "Rick Bard",    
     "Company": [{"ID": 1, "Name":"Oracle"},{"ID": 2, "Name":"Google"}]
     "CompanyID":2
    }
];

我想使用 AngularJs 和 DevExtreme 在 dxDataGrid 中表示该数据。因此该Company列将被查找并选择要绑定的公司 ID CompanyID

我想实现类似:

$scope.dataGridOptions = {
    dataSource: customers,
    columns: ["Name", 
              { 
                dataField: "CompanyID", 
                lookup: {
                        dataSource:customers[rowindex].Company,
                        valueExpr: 'ID', 
                        displayExpr: 'Name'
                        },
                 caption: 'Company' 
                }]
};
4

2 回答 2

1

您可以在列中添加一个editCellTemplate来实现您的目标。像这样的东西:

$scope.valueChangeAction = function (e) {
    this.setValue(e.value);
};
...
columns: [{ 
    dataField: "CompanyID",
    editCellTemplate: "lookupEditCell"
}]

并定义模板:

<div data-options="dxTemplate:{ name:'lookupEditCell' }" dx-item-alias="cell">
    <div dx-lookup="{
        dataSource: cell.data.Company,
        valueExpr: 'ID',
        displayExpr: 'Name',
        onValueChanged: $parent.valueChangeAction
    }"></div>
</div>
于 2017-04-11T08:33:06.693 回答
0

正如@Dmitry 所提到的,添加editCellTemplate可用于表示数据。但dx-item-alias可能不是必需的。

[JAVASCRIPT]

$scope.Customers =  [{
      "Name": "John Mayor",
      "CompanyId": 1,
      "Company": [{"ID": 1,"Name": "Super Mart"}, {"ID": 2,"Name": "Big Mart"}]
    }, {
      "Name": "Rick Bard",
      "CompanyId": 2,
      "Company": [{ "ID": 1, "Name": "Google" }, { "ID": 2, "Name": "Oracle" }]
    }];

  $scope.dataGridOptions= {
    dataSource: $scope.Customers,
    editing: {
      mode: 'cell',
      allowUpdating: true,
    },
    columns: [{
      dataField: "Name",
      allowEditing: false

    }, {
      dataField: 'CompanyId',
      allowEditing: true,
      showEditorAlways: true,
      editCellTemplate: 'lookupCell',
      caption:'Company'
    }]
  }

  $scope.getOnValueChangeAction = function(row) {
    return function(e) {
        row.setValue(e.value);
    }    
  };

[HTML]

<div dx-data-grid="dataGridOptions">
    <div data-options="dxTemplate:{ name:'lookupCell' }">
        <div dx-select-box="{
                        dataSource: data.Company,
                        value: data.CompanyId,
                        valueExpr: 'ID',
                        displayExpr: 'Name',
                        onValueChanged: $parent.getOnValueChangeAction(this)
                       }">
        </div>
    </div>
</div>
于 2017-04-13T07:29:56.320 回答