We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Customize column based on other

I have a table as follow:



Column "Type2" is based on "Type1". What I want is to get one column "Type 2" the Name not the Id. It works for other columns without foreign key.

How I can get the Name of this column in other column?

My code:

  $(function () {

            dataManagerType = ej.DataManager({
                url: "/api/ProductType1",
                adaptor: new ej.WebApiAdaptor()
            });


            var dataManager = ej.DataManager({
                url: "/api/Product",
                adaptor: new ej.WebApiAdaptor(),
                offline: true
            });

            dataManager.ready.done(function (e) {
                $("#Grid").ejGrid({
                    dataSource: ej.DataManager({
                        json: e.result,
                        adaptor: new ej.remoteSaveAdaptor(),
                        insertUrl: "/api/Product/Insert",
                        updateUrl: "/api/Product/Update",
                        removeUrl: "/api/Product/Remove",
                    }),
                    toolbarSettings: {
                        showToolbar: true,
                        toolbarItems: ["add", "edit", "printGrid", "search"]
                    },
                    editSettings: {
                        allowEditing: true,
                        allowAdding: true,
                        allowDeleting: false,
                        showDeleteConfirmDialog: true,
                        editMode: "dialogtemplate",
                        dialogEditorTemplateID: "#templateForm",
                    },
                    isResponsive: true,
                    enableResponsiveRow: true,
                    allowSorting: true,
                    allowSearching: true,
                    allowFiltering: true,
                    filterSettings: {
                        filterType: "excel",
                        maxFilterChoices: 100,
                        enableCaseSensitivity: false
                    },
                    allowPaging: true,
                    pageSettings: { pageSize: 100, printMode: ej.Grid.PrintMode.CurrentPage },
                   

                    columns: [
                        { field: "ProductId", type: 'number', headerText: 'OD-No.', defaultValue: 0, isPrimaryKey: true, isIdentity: true, visible: false },
                        { field: "Barcode", type: 'string', headerText: 'P/N', validationRules: { required: true } },
                        { field: "ISO", type: 'string', headerText: 'ISO', validationRules: { required: true } },
                        { field: "TypeList", headerText: 'Type1', foreignKeyField: "TypeId", foreignKeyValue: "Name", dataSource: dataManagerType, validationRules: { required: true } },
                        { field: "SupplierPN", type: 'string', headerText: 'SupplierPN', visible: false },
                        { headerText: "Type2", template: "<span>{{:TypeList}}</span > " },
                      
                    ],

                    actionComplete: "complete",
                });


            });


        });

3 Replies

MP Manivannan Padmanaban Syncfusion Team November 8, 2019 10:05 AM UTC

Hi Marvin, 

Greetings from Syncfusion Support. 

From the shared query, we are able to understand that you want to show the name value for the column "Type 2" instead of ID value. In the shared code example, we could see that in the “Type 2” column template you have used the “TypeList” field value (i.e.  template: "<span>{{:TypeList}}</span > "). In grid data result for field “TypeList” it contains only the ID value not the Name value, due to the above reason its shows the ID value in the “Type 2” column instead of Name.  

To achieve your requirement, we suggest you to store the foreignkey datasource in the window variable at the done function of foreignkey datamanager and using our templateRefresh event of ejGrid set the filtered data to the column “Type 2”.  

Refer the below code example, 
  $(function () { 
 
        window.foreignkeydata = {}; 
 
        var dataManager = ej.DataManager({ 
            url: "/api/Default", 
            adaptor: new ej.WebApiAdaptor(), 
            offline: true 
        }); 
 
 
        dataManager.ready.done(function (e) { 
            window.foreignkeydata = e.result; // store the foreignkey dataSource in window variable. 
        }) 
 
            $("#Grid").ejGrid({ 
                ………… 
                templateRefresh: function (args) { 
                    if (args.column.headerText == "Type2") { 
                        var value = window.foreignkeydata.filter(e => e.EmployeeID == args.data.EmployeeID); // filter the foreignkey datasource using foreignkey field. 
                        args.cell.innerText = value[0].FirstName;  
                    } 
                }, 
                columns: [ 
…………………………………………. 
                    { field: "EmployeeID", headerText: 'Type1', foreignKeyField: "EmployeeID", foreignKeyValue: "FirstName", dataSource: dataManager, validationRules: { required: true } }, 
                    { headerText: "Type2", template: "<span></span > " }, 
 
                ], 
 
            }); 
  
 
        }); 

 

Kindly get back to us, if you need further assistance. We will be happy to assist you. 

Regards, 
Manivannan Padmanaban. 




MA Marvin November 8, 2019 05:51 PM UTC

Thanks but I have follow problem:
I have two dataadapter. 

If I use your code I can only use the Product datamanager. 
But I need the Foreign adaptor ProductType.

   $(function () {

            window.foreignkeydata = {}; 

            var dataManagerType = ej.DataManager({
                url: "/api/ProductType1",
                adaptor: new ej.WebApiAdaptor(),
                 offline: true
            });

            dataManagerType.ready.done(function (e) {
                window.foreignkeydata = e.result;
            });

            var dataManager = ej.DataManager({
                url: "/api/Product",
                adaptor: new ej.WebApiAdaptor(),
                offline: true
            });



            dataManager.ready.done(function (e) {

                window.foreignkeydata = e.result;

                $("#Grid").ejGrid({
                    dataSource: ej.DataManager({
                        json: e.result,
                        adaptor: new ej.remoteSaveAdaptor(),
                        insertUrl: "/api/Product/Insert",
                        updateUrl: "/api/Product/Update",
                        removeUrl: "/api/Product/Remove",
                    }),
                    toolbarSettings: {
                        showToolbar: true,
                        toolbarItems: ["add", "edit", "printGrid", "search"]
                    },
                    editSettings: {
                        allowEditing: true,
                        allowAdding: true,
                        allowDeleting: false,
                        showDeleteConfirmDialog: true,
                        editMode: "dialogtemplate",
                        dialogEditorTemplateID: "#templateForm",
                    },
                    isResponsive: true,
                    enableResponsiveRow: true,
                    allowSorting: true,
                    allowSearching: true,
                    allowFiltering: true,
                    filterSettings: {
                        filterType: "excel",
                        maxFilterChoices: 100,
                        enableCaseSensitivity: false
                    },
                    allowPaging: true,
                    pageSettings: { pageSize: 100, printMode: ej.Grid.PrintMode.CurrentPage },

                    templateRefresh: function (args) {
                        if (args.column.headerText == "Type2") {
                            var value = window.foreignkeydata.filter(e => e.TypeId == args.data.TypeId); // filter the foreignkey datasource using foreignkey field. 
                            args.cell.innerText = value[0].Name;
                        }
                    }, 

                    columns: [
                        { field: "ProductId", type: 'number', headerText: 'OD-No.', defaultValue: 0, isPrimaryKey: true, isIdentity: true, visible: false },
                        { field: "Barcode", type: 'string', headerText: 'P/N', validationRules: { required: true } },
                        { field: "ISO", type: 'string', headerText: 'ISO', validationRules: { required: true } },
                        { field: "TypeList", headerText: 'Type1', foreignKeyField: "TypeId", foreignKeyValue: "Name", dataSource: dataManagerType, validationRules: { required: true } },
                        { field: "SupplierPN", type: 'string', headerText: 'SupplierPN', visible: false },
                        { headerText: "Type2", template: "<span></span > " },

                    ],

                    actionComplete: "complete",
                });


            });


        });


MP Manivannan Padmanaban Syncfusion Team November 12, 2019 04:00 AM UTC

Hi Marvin, 

Thanks for the update. 

Query: If I use your code I can only use the Product datamanager. But I need the Foreign adaptor ProductType. 

You can use both adaptors, we suggest you to store the foreignkey adaptor result in the global variable in foreignkey adaptor done function only (i.e. not in grid dataSource adaptor done function). Refer the below code example, 

 
    $(function () { 
 
        window.foreignkeydata = {}; 
 
        var ForiegnkeyData = ej.DataManager({ 
            url: "/api/Default", 
            adaptor: new ej.WebApiAdaptor(), 
            offline: true 
        }); 
 
        var GridData = ej.DataManager({ 
            url: "/api/Grid", 
            adaptor: new ej.WebApiAdaptor(), 
            offline: true 
        }); 
 
 
 
        ForiegnkeyData.ready.done(function (e) { 
            window.foreignkeydata = e.result; 
        }) 
 
        GridData.ready.done(function (e) { 
            $("#Grid").ejGrid({ 
                dataSource: ej.DataManager({ 
                    json: e.result, 
                    adaptor: new ej.remoteSaveAdaptor() 
                }), 
                ……………. 
                templateRefresh: function (args) { 
                    if (args.column.headerText == "Type2") { 
                        var value = window.foreignkeydata.filter(e => e.EmployeeID == args.data.EmployeeID); 
                        args.cell.innerText = value[0].FirstName; 
                    } 
                }, 
                columns: [ 
…………………………………… 
                    { field: "EmployeeID", headerText: 'Type1', foreignKeyField: "EmployeeID", foreignKeyValue: "FirstName", dataSource: ForiegnkeyData, validationRules: { required: true } }, 
                    { headerText: "Type2", template: "<span></span > " }, 
 
                ], 
 
……………………… 
            }); 
        }); 
 
        }); 
</script> 

For your convenience we have created the sample, kindly refer the below link. 

After referred the above sample, still facing an issue. Please get back to us with the issue reproducible sample Or reproduce the issue in the shared sample. 

Regards, 
Manivannan Padmanaban. 



Loader.
Live Chat Icon For mobile
Up arrow icon