@(Html.EJ().Grid<OrdersView>("Events")
.Datasource((IEnumerable<object>)ViewBag.dataSource)
. ..
.. . .
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Add();
col.Field("CustomerID").HeaderText("Customer ID").Add();
col.Field("EmployeeID").ForeignKeyField("EmployeeID")
.EditTemplate(a => {
a.Create("create").Read("read").Write("write");
})
.DataSource(ViewBag.dropData)
.ForeignKeyValue("Roles").HeaderText("Employee ID").Add();
col.Field("Freight").HeaderText("Freight").Format("{0:C}").Add();
col.Field("ShipCity").HeaderText("Ship City").Add();
})
.ClientSideEvents(eve =>
{
eve.QueryCellInfo("queryCellInfo");
})
)
<script>
function create() {
return "<input>";
}
function read(args) {
var dropObj = args.ejDropDownList("instance");
var data = args.ejDropDownList("getSelectedValue");
var val = ej.DataManager(dropObj.model.dataSource).executeLocal(new ej.Query().where("Roles", "equal", data.split(",")[0]))[0].EmployeeID
if (!data)
return {};
return val;
}
function write(args) {
var val = ej.isNullOrUndefined(args.rowdata["EmployeeID"]) ? "" : args.rowdata["EmployeeID"];
var datas = ej.DataManager(args.column[2].dataSource).executeLocal(new ej.Query().where("EmployeeID", "equal", val));
var selIndex = [];
for (var s = 0; s < datas.length; s++) {
selIndex.push(args.column[2].dataSource.indexOf(datas[s]));
}
args.element.ejDropDownList({
dataSource: args.column[2].dataSource,
fields: { text: "Roles" },
showCheckbox: true,
width: "100%",
selectedItems: selIndex
});
}
function queryCellInfo(args) {
if (args.column.field == "EmployeeID") {
var datas = ej.DataManager(args.column.dataSource).executeLocal(new ej.Query().where("EmployeeID", "equal", args.data.EmployeeID))
var str = "";
for (var s = 0; s < datas.length; s++) {
str = s == 0 ? str.concat(datas[s].Roles) : str.concat("," + datas[s].Roles)
}
$(args.cell).text(str);
}
}
</script> |
UserID | Name | Roles |
1 | Fred | admin,manager |
2 | Wilma | manager,cashier |
private void BindDataSource()
{
//Column DataSource
emp.Add(new Roles(1, "Tester"));
emp.Add(new Roles(2, "Engineer"));
. . .
. .
emp.Add(new Roles(9, "Build analyst"));
int code = 10000;
for (int i = 1; i <= 9; i++)
{
string str = i.ToString() +"," + (i + 1).ToString();
//Main dataSource for Grid
Ord.Add(new Orders(code + 1, "ALFKI", i,str));
code += 5;
}
} |
@(Html.EJ().Grid<OrdersView>("Events")
.Datasource((IEnumerable<object>)ViewBag.dataSource)
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
. .
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Add();
.. .
. .
col.Field("RoleID")
.EditTemplate(a =>
{
a.Create("create").Read("read").Write("write");
})
.DataSource(ViewBag.dropData).HeaderText("Roles").Add();
})
.ClientSideEvents(eve =>
{
eve.QueryCellInfo("queryCellInfo");
})
)
<script>
function queryCellInfo(args) {
if (args.column.field == "RoleID") {
var field = args.column.field, merge = false; preds = [];
var txt = args.data.RoleID.split(",");
//create a query
var predicate = new ej.Predicate(field, "equal", ej.parseInt(txt[0]));
for (var tx = 1; tx < txt.length; tx++) {
preds.push(new ej.Predicate(field, "equal", txt[tx]));
merge = true;
}
if (merge) {
preds.unshift(predicate);
predicate = ej.Predicate.or(preds);
}
//filter the dataSource
var datas = ej.DataManager(args.column.dataSource).executeLocal(new ej.Query().where(predicate));
var str = "";
for (var s = 0; s < datas.length; s++) {
str = s == 0 ? str.concat(datas[s].Role) : str.concat("," + datas[s].Role)
}
$(args.cell).text(str);
}
}
</script> |
@(Html.EJ().Grid<OrdersView>("Events")
.Datasource((IEnumerable<object>)ViewBag.dataSource)
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
. . .
. ..
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Add();
col.Field("CustomerID").HeaderText("Customer ID").Add();
col.Field("EmployeeID").HeaderText("Employee ID").Add();
col.Field("RoleID")
.EditTemplate(a =>
{
a.Create("create").Read("read").Write("write");
})
.DataSource(ViewBag.dropData).HeaderText("Roles").Add();
col.Field("Freight").HeaderText("Freight").Format("{0:C}").Add();
})
. . .
)
<script>
function create() {
return "<input>";
}
function read(args) {
var data = args.ejDropDownList("getSelectedValue");
if (!data)
return {};
return data;
}
function write(args) {
var val = ej.isNullOrUndefined(args.rowdata["RoleID"]) ? "" : args.rowdata["RoleID"];
args.element.ejDropDownList({
dataSource: args.column[3].dataSource,
fields: { text: "Role", value: "RoleID" },
showCheckbox: true,
width: "100%",
value: val
});
}
</script> |
[Index.cshtml]
@(Html.EJ().Grid<Object>
("FlatGrid")
.Datasource((IEnumerable<object>)ViewBag.DataSource)
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
.ClientSideEvents(events => events.ToolbarClick("toolbarClickHandler"))
.ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
{
. . . .
items.AddTool(ToolBarItems.ExcelExport);
}))
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Add();
col.Field("EmployeeID").HeaderText("Employee ID").Add();
col.Field("CustomerID").HeaderText("Customer ID").Add();
col.Field("ShipPostalCode").HeaderText("Ship Postal Code").EditTemplate(a => { a.Create("create").Read("read").Write("write"); }).Add();
})
)
<script type="text/javascript">
function toolbarClickHandler(args) {
if (args.itemName.indexOf("Excel Export") != -1) {
var edittemplatecolumn = this.model.columns.filter(function (e) {
return e.editType == "edittemplate"
});
var Index = this.getColumnIndexByField(edittemplatecolumn[0].field); // Get the edit template column index here.
delete this.model.columns[Index].editType; // Delete the edittemplate model while exporting.
}
}
</script>
|