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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback
Essential JS2

Syncfusion jQuery based widgets are no longer in active development. Switch to our pure JavaScript based next generation Essential JS 2 library.

Customize column based on other

Thread ID:

Created:

Updated:

Platform:

Replies:

148873 Nov 6,2019 07:06 PM UTC Nov 12,2019 04:00 AM UTC jQuery 3
loading
Tags: ejGrid
Marvin
Asked On November 6, 2019 07:06 PM UTC

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",
                });


            });


        });

Manivannan Padmanaban [Syncfusion]
Replied On 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. 



Marvin
Replied On 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",
                });


            });


        });

Manivannan Padmanaban [Syncfusion]
Replied On 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. 



CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon