Articles in this section
Category / Section

How to use edit template to edit foreign key column?

1 min read

 

The Edit template feature of the Grid allow us to use custom editor for a column. When using custom editor, it is our responsibility to tell the Grid, how to read and write in the custom editor using the read and write functions of the edit template respectively.

 

When using edit template for a foreign key column, the foreign key mapping should be handled externally and the appropriate value should be returned from the read function. 

 

 Let us discuss how to use an ejAutocomplete to edit a foreign key column.

 

Grid initialization

 

JS

 

<div id="Grid"></div>

 

 

<script type="text/javascript">
    $(function () {
        $("#Grid").ejGrid({          
            dataSource: window.gridData,
            allowPaging: true,           
            editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, editMode: "normal" },            
            toolbarSettings: { showToolbar: true, toolbarItems: [ej.Grid.ToolBarItems.Add, ej.Grid.ToolBarItems.Edit, ej.Grid.ToolBarItems.Delete, ej.Grid.ToolBarItems.Update, ej.Grid.ToolBarItems.Cancel] },             
            columns: [
                    { field: "OrderID", isPrimaryKey: true, width: 50, headerText: "Order ID", textAlign: ej.TextAlign.Right },
                    {
                        field: "CustomerID", width: 100, headerText: "Company Name",
                        foreignKeyField: "CustomerID", dataSource: window.ordersView, foreignKeyValue: "CompanyName",
                        editTemplate: {
                            create: "create",
                            read: "read",
                            write: "write"
                        }
                    },
                    { field: "EmployeeID", width: 100 },                    
                    { field: "Freight", width: 100 }
            ]
        });
    });    
</script>

 

MVC

 

 
@(Html.EJ().Grid<OrdersView>("Grid")    
    .Datasource((IEnumerable)ViewBag.dataSource)
    .AllowPaging()
    .Columns(col =>
    {
        col.Field(o => o.OrderID).HeaderText("Order ID").Width(50).IsPrimaryKey(true).Add();
        col.Field(o => o.CustomerID).ForeignKeyField("CustomerID").ForeignKeyValue("CompanyName").HeaderText("Company Name")
            .DataSource((IEnumerable)ViewBag.foreignData).Width(100)
            .EditTemplate(et => et.Create("create").Read("read").Write("write"))
            .Add();
        col.Field(o => o.EmployeeID).Width(100).Add();
        col.Field(o => o.Freight).Width(100).Add();
 
    })
    .EditSettings(edit => edit.AllowEditing().AllowDeleting().AllowAdding().EditMode(EditMode.Normal))
    .ToolbarSettings(tool => tool.ShowToolbar().ToolbarItems(new List<ToolBarItems>() {
        ToolBarItems.Add, 
        ToolBarItems.Edit, 
        ToolBarItems.Delete, 
        ToolBarItems.Update,
        ToolBarItems.Cancel
    })))
 

 

ASP

 

 
        <ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True">           
            <EditSettings AllowAdding="True" AllowDeleting="True" AllowEditing="True"></EditSettings>
            <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
            <Columns>
                <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="True" TextAlign="Right" Width="50"/>                                  
                <ej:Column Field="CustomerID" HeaderText="Company Name" Width="100" ForeignKeyField="CustomerID" ForeignKeyValue="CompanyName">
                    <EditTemplate Create="create" Read="read" Write="write" />
                </ej:Column>                     
                <ej:Column Field="EmployeeID" Width="100" />
                <ej:Column Field="Freight" Width="100"/>                                                    
            </Columns>
         </ej:Grid>
 

 

The create, read and write functions of the edit template are as follows.

 

 
    function create() {
        return $("<input />");
    }
 
    function read(e) {
        var data = e.ejAutocomplete("getSelectedItems")[0];
 
        if (!data)
            return {};
 
        return data["CustomerID"];   //Return the value after mapping 
    }

    function write(e) {
        e.element.ejAutocomplete({
            dataSource: window.ordersView, //Foreign key dataSource
            fields: { text: "CompanyName", key: "CustomerID" },
            width: "100%"
        })
        .ejAutocomplete("selectValueByKey", e.rowdata["CustomerID"] || "");
    }
 

 

In the above code example, the ejAutocomplete is configured to display CompanyName and return the CustomerID in the read function.

 

The following is displayed as the output of the above behavior.

 

Figure: Foreign key column with autocomplete editor 

 

Batch editing

The read function will different slightly when using batch edit mode in foreign key column. In this case, the read function should return an object with keys text and value, which should contains the text to be displayed in the cell and value to be saved in the grid data source respectively.

 

 
function read(e) {
        var data = e.ejAutocomplete("getSelectedItems")[0];
 
        if (!data)
            return {};
 
        return {
            value: data["CustomerID"], //Will be saved in grid datasource
            text: data["CompanyName"] //Will be used to display in cell
        };
    }
 

  

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied