<e-column field='Bo2.0.Address1' headerText='Address 1'></e-column>
<e-column field='Bo2.0.Address2' headerText='Address 2'></e-column> |
// Accessing the ‘Bo2’ object’s first array data
<e-column field='Bo2.0.Address1' headerText='Address 1_1'></e-column>
<e-column field='Bo2.0.Address2' headerText='Address 2_1'></e-column>
// Accessing the ‘Bo2’ object’s second array data
<e-column field='Bo2.1.Address1' headerText='Address 1_2'></e-column>
<e-column field='Bo2.1.Address2' headerText='Address 2_2'></e-column> |
// Grid’s load event handler
function onLoad() {
// Data that is bound to the Grid
var data = @Html.Raw(Json.Serialize(ViewBag.Data));
// The complex data is retrieved from the data to form the columns
// This is performed considering all the data array has same complex object so the first data object alone is retrieved
// If there are different complex data then you need to iterate through each data and retrieve its corresponding complex object and for that you need to perform the below operation
var complexObj = data[0].Bo2;
var complexCols = [];
// Column object is formed based on the complex data object
complexObj.forEach((x, index) => {
if (x.Address1) complexCols.push({ field: "Bo2." + index.toString() + ".Address1" }); if (x.Address2)
complexCols.push({ field: "Bo2." + index.toString() + ".Address2" })
});
// Each complex data column formed is pushed to the Grid column model
complexCols.forEach(x => this.columns.push(x));
} |
EmployeeId | FirstName | LastName | Address1 | Address2 | |
123456789 | Darth | Vader | This is child 1 address 1 | This is child 1 address 2 | |
123456789 | Darth | Vader | This is child 2 address 1 | This is child 2 address 1 |
What is the best way to accomplish that?
@{
var address1 = "valueAccessorFn1";
var address2 = "valueAccessorFn2";
}
<div id="parent">
<ejs-grid id="Grid" dataSource="@ViewBag.Data">
<e-grid-columns>
.
.
<e-grid-column headerText="Address 1" valueAccessor="address1" width="120"></e-grid-column>
<e-grid-column headerText="Address 2" valueAccessor="address2" width="120"></e-grid-column>
</e-grid-columns>
</ejs-grid>
</div>
<script>
var index1 = 0;
var index2 = 0;
// Custom formatted value to be displayed in the Address1 column
function valueAccessorFn1(field, data, column) {
// Data is accessed from the object array based on an index value
var value = data.Address[index1].Address1;
// The index value is incremented so that the next index can be accessed for the consecutive rows
index1++;
// Value to be displayed is returned
return value;
}
// Custom formatted value to be displayed in the Address2 column
function valueAccessorFn2(field, data, column) {
// Data is accessed from the object array based on an index value
var value = data.Address[index2].Address2;
// The index value is incremented so that the next index can be accessed for the consecutive rows
index2++;
// Value to be displayed is returned
return value;
}
</script> |
// Custom formatted value to be displayed in the Address1 column
function valueAccessorFn1(field, data, column) {
// Data is accessed from the object array based on an index value
//var value = data.Address[index].Employees[0].Division;
var value = data.Employees[0].Division;
// The index value is incremented so that the next index can be accessed for the consecutive rows
index++;
// Value to be displayed is returned
return value;
} |