@(Html.EJ().Grid<OrdersView>("ForeignKey")
.Datasource((IEnumerable<object>)ViewBag.dataSource1)
.AllowPaging()
.ToolbarSettings(toolbar =>
{
toolbar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.Search);
});
})
.Columns(col =>
{
. . .
col.Field("EmployeeID").HeaderText("Employee Name").ForeignKeyField("EmployeeID")
.ForeignKeyValue("FirstName").DataSource((IEnumerable<object>)ViewBag.dataSource2)
.TextAlign(TextAlign.Left).Width(90).Add();
. . .
}).ClientSideEvents(eve =>
{
eve.ActionBegin("actionbegin");
})
)
</div>
<script>
function actionbegin(args) {
if (args.requestType == "searching") {
if (ej.DataManager(this.model.dataSource).executeLocal(new ej.Query().search(args.keyValue, ["OrderID", "EmployeeID", "Freight", "ShipName", "ShipCountry"], true)).length == 0) {
var length = this.model.columns.length;
for (var i = 0; i < length; i++) {
if (this.model.columns[i]["foreignKeyValue"] == "FirstName") {
var data = ej.DataManager(this.model.columns[i]["dataSource"]).executeLocal(new ej.Query().search(args.keyValue, [this.model.columns[i]["foreignKeyValue"]], true)), foreignkeyFieldValue = (data[0].EmployeeID).toString();
this.model.searchSettings.fields = [this.model.columns[i]["foreignKeyField"]]; //Search the data in foreignkey field
this.model.searchSettings.operator = "equal"; // set equal operator for get the equal values from foreignkey data source
this.model.searchSettings.key = foreignkeyFieldValue; // set the foreignkey field
}
}
}
else {
this.model.searchSettings.fields = ["OrderID", "EmployeeID", "Freight", "ShipName", "ShipCountry"]; //Search the data source with selected fields
this.model.searchSettings.operator = "contains"; // Set the operator to contains
this.model.searchSettings.key = args.keyValue; // Set the foreign key value
}
}
}
</script>
|