BipinBaglung
BipinBaglung

Reputation: 524

DevExtreme AngularJs DataGrid lookup column with dataSource within same object

I have data as follows :

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
    }
];

I would like to represent that data in dxDataGrid using AngularJs and DevExtreme. So that Company column will be lookup and selected company's ID to be bound with CompanyID.

I would like to achieve something like :

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

Upvotes: 0

Views: 2432

Answers (2)

BipinBaglung
BipinBaglung

Reputation: 524

As mentioned by @Dmitry adding the editCellTemplate can be used to represent the data. But dx-item-alias may not be required.

[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>

Upvotes: 0

Dmitry Levkovsky
Dmitry Levkovsky

Reputation: 180

You can add an editCellTemplate to the column to achieve your goal. Something like that:

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

And define the template:

<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>

Upvotes: 1

Related Questions