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

Columns object Parameter Binding

Hi
I’m dynamically loading the grid columns at runtime from json config data.
All is well, however I’m not able to bind the column parameters.
The code below is fine if the dataSource is supplied within the json column config, however this is not practical


[    
    {
    "field": "GM_PHONE_A",
    "type": "string",
    "visible": true,
    "customAttributes": {"class": "cell-ind-uul"},
    "isPrimaryKey": false,
    "textAlign": "",
    "headerText": "Phon A",
    "headerTextAlign": "",
    "width": 130
    },
    {
    "field": "GM_PH_TYPE_A",
    "type": "string",
    "visible": true,
    "foreignKeyField": "value",
    "foreignKeyValue": "label",
    "dataSource": [{"label": "", "value": 0}, {"label": "home", "value": 1}, {"work": "222", "mobile": 2}, {"label": "after hours", "value": 3}],
    "isPrimaryKey": false,
    "textAlign": "center",
    "headerText": "Ph type",
    "headerTextAlign": "center",
    "width": 80
    }
]

What I need to do is to bind the dataSource via the typescript code, and have this data returned from a service.

e.g.   "[dataSource]": “getListData(‘phoneTypesData’)”
or     "[dataSource]": “phoneTypesData”

Any clues on how I could achieve this when supplying a columns object ?

thanks again
Ron

1 Reply 1 reply marked as answer

SK Sujith Kumar Rajkumar Syncfusion Team February 16, 2021 12:24 PM UTC

Hi Ron, 
 
Greetings from Syncfusion support. 
 
Based on the provided information we suspect that you requirement is to directly bind dynamic data returned from a service to the Grid when setting column object to columns property. If so you can achieve it by binding the returned JSON data to the Grid’s dataSource property as demonstrated in the below code snippet, 
 
// Button click event function 
onClick() { 
    // Request is sent to fetch data from service 
    var ajax = new Ajax({ 
        url: "https://ej2services.syncfusion.com/production/web-services/api/Orders", 
        type: "GET", 
        contentType: "application/json; charset=utf-8", 
    }); 
    ajax.onSuccess = result => { 
        // The returned JSON result is set as Grid’s data source 
        this.gridObj.dataSource = JSON.parse(result); 
    } 
    ajax.send(); 
} 
 
We have prepared a sample based on this for your reference. You can find it below, 
 
 
 
Also since you have mentioned that data is retrieved from service, we suggest you to use the custom binding or remote data binding with adaptor support available with the Grid to achieve the same. We have explained this in detail below, 
 
Custom binding: 
 
If you are using the API calls to perform Grid action from the server, then we suggest you to custom binding approach to bind data in the Grid. With this you can bind data from an API call by providing your own custom queries(as required by your API) and handle all the Grid actions(Sort, Page, Filter, etc. ) with it. The Grid’s custom binding approach is explained below, 
 
For using custom binding, you need to bind the response data(Grid data) returned from your API as an object of result(JSON data source) and count(Total data count) properties and set it to the Grid’s dataSource property. On setting the data source in this format, the ‘dataStateChange’ event will be triggered with corresponding query for every Grid action performed like Paging, Sorting and Filtering.., and the ‘dataSourceChanged’ event will be triggered while performing CRUD action in Grid. So using this you can send the queries in your required format to your API, process and return the result and then assign the returned result to the Grid’s dataSource property as an object of result and count properties. 
 
More details on custom binding can be checked in the below help documentation link, 
 
 
 
 
Note:  The ‘dataStateChange’ event will not be triggered on Grid initial render. So for this case you need to return the data in the above mentioned format on initialization and assign it to the Grid’s dataSource property. 
 
Remote data binding: 
 
The EJ2 Grid supports the following adaptors to communicate with the data source in the back-end – Json, URL, OData, ODataV4, Remote Save, Web API and WebMethod adaptor. Each adaptor uses a different way to send and receive requests and response from remote services. They are explained in the below documentation link which you can check for more details, 
 
 
 
You can use the adaptor which best suits your requirement for binding data and performing grid actions(Sort, Page, Filter, etc. ) from the server. 
 
Please get back to us if you require any further assistance. 
 
Regards, 
Sujith R 


Marked as answer
Loader.
Up arrow icon