$(function () { var obj = [ . . . . ]; var list = [{ header: "Grid Header 1", value: "value1" }, { header: "Grid Header 2", value: "value2" }, { header: "Grid Header 3", value: "value3" }]; $("#Grid").ejGrid({ dataSource: obj, allowPaging: true, columns: [ { field: "EmployeeID", isPrimaryKey: true, headerText: "EmployeeID", textAlign: ej.TextAlign.Right, width: 90 }, { field: "LastName", headerText: 'LastName', width: 90 }, { field: "FirstName", headerText: 'FirstName', textAlign: ej.TextAlign.Right, width: 75 }, { field: "Freight", headerText: 'Freight', textAlign: ej.TextAlign.Right, width: 75, format: "{0:C}" }, { field: "Title", headerText: 'Title', width: 90 }, { field: "City", headerText: 'City', width: 90 } ], dataBound: function (args) { for (var fld = 0; fld < list.length; fld++) { var column = { field: list[fld].value, headerText: list[fld].header, column: "", textAlign: "left", type: "string", visible: true, width: 75 }; this.model.columns.push(column); //add dynamic columns } this.columns(this.model.columns); } }); |
Is there a way to change the ordering of the dynamic columns?
I like the dynamic columns to be added in the middle but the last column is still a static.
$(function () { var obj = [ . . . . ]; var list = [{ header: "Grid Header 1", value: "value1" }, { header: "Grid Header 2", value: "value2" }, { header: "Grid Header 3", value: "value3" }]; $("#Grid").ejGrid({ dataSource: obj, allowPaging: true, columns: [ { field: "EmployeeID", isPrimaryKey: true, headerText: "EmployeeID", textAlign: ej.TextAlign.Right, width: 90 }, { field: "LastName", headerText: 'LastName', width: 90 }, { field: "FirstName", headerText: 'FirstName', textAlign: ej.TextAlign.Right, width: 75 }, { field: "Freight", headerText: 'Freight', textAlign: ej.TextAlign.Right, width: 75, format: "{0:C}" }, { field: "Title", headerText: 'Title', width: 90 }, { field: "City", headerText: 'City', width: 90 } ], dataBound: function (args) { var count = 0; for (var fld = 0; fld < list.length; fld++) { var column = { field: list[fld].value, headerText: list[fld].header, column: "", textAlign: "left", type: "string", visible: true, width: 75 }; this.model.columns.splice(3 + count, 0, column);// adding dynamic column after 3rd column count++; } this.columns(this.model.columns); } }); |
var list1 = [{ header: "Header 1", value: "value1" }, { header: "Header 2", value: "value2" }, { header: "Header 3", value: "value3" }]; var data1 = [ . . . . ]; var data2 = [ . . . . ]; $(function () { $('#selectDataSource').ejDropDownList({ change: function (args) { var gridObj = $("#Grid").ejGrid("instance"); if (args.value == "data1") $("#Grid").ejGrid("model.dataSource", data1);// set the new datasource to grid else $("#Grid").ejGrid("model.dataSource", data2);// set the new datasource to grid var oldColumns = ej.DataManager(gridObj.model.columns).executeLocal(new ej.Query().select("field"));// geting field name in array var newColumns = []; for (var field in gridObj.model.dataSource[0]) { $("#Grid").ejGrid("columns", field, "add");// add dynamic columns form new data source newColumns.push(field); } var removeColumn = []; $.grep(oldColumns, function (el) { if (jQuery.inArray(el, newColumns) == -1) removeColumn.push(el); }); $("#Grid").ejGrid("columns", removeColumn, "remove");// removing columns if it is not in new data source } }); $("#Grid").ejGrid({ dataSource: data1, allowPaging: true, columns: [ { field: "EmployeeID", isPrimaryKey: true, headerText: "EmployeeID", textAlign: ej.TextAlign.Right, width: 90 }, { field: "LastName", headerText: 'LastName', width: 90 }, { field: "FirstName", headerText: 'FirstName', textAlign: ej.TextAlign.Right, width: 75 }, { field: "Freight", headerText: 'Freight', textAlign: ej.TextAlign.Right, width: 75, format: "{0:C}" }, { field: "Title", headerText: 'Title', width: 90 }, { field: "City", headerText: 'City', width: 90 } ], dataBound: function (args) { var count = 0; for (var fld = 0; fld < list1.length; fld++) { var column = { field: list1[fld].value, headerText: list1[fld].header, column: "", textAlign: "left", type: "string", visible: true, width: 75 }; this.model.columns.splice(3 + count, 0, column); count++; } this.columns(this.model.columns); } }); |