DevExtreme AngularJs DataGrid 查找列与同一对象内的数据源
DevExtreme AngularJs DataGrid lookup column with dataSource within same object
我有如下数据:
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
列将被查找并选择要与 CompanyID
绑定的公司 ID。
我想实现这样的目标:
$scope.dataGridOptions = {
dataSource: customers,
columns: ["Name",
{
dataField: "CompanyID",
lookup: {
dataSource:customers[rowindex].Company,
valueExpr: 'ID',
displayExpr: 'Name'
},
caption: 'Company'
}]
};
您可以在该列中添加一个 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>
如@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>
我有如下数据:
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
列将被查找并选择要与 CompanyID
绑定的公司 ID。
我想实现这样的目标:
$scope.dataGridOptions = {
dataSource: customers,
columns: ["Name",
{
dataField: "CompanyID",
lookup: {
dataSource:customers[rowindex].Company,
valueExpr: 'ID',
displayExpr: 'Name'
},
caption: 'Company'
}]
};
您可以在该列中添加一个 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>
如@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>