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

In a custom adapter how do I change the format of the data sent to the server?

Object resulting from selecting grid criteria perspectivally

Criteria from grid "where id greater than 1" with fields id and lastname
Output object:
{ where: {id: {gt: 1}}, skip: 1, fields: ['id','lastName]};

Criteria from grid "where Last Name = Brown"
Output object:
{ where: {lastName:'Brown'} }

Then I will serialize this for the url using $.param( myObject )
I know I can transform the object produced by the datagrid in the beforSend method.

1) Is that the right location?
2) Should I inherit from the url adapter or just start from scratch with the base adapter?
Also it would be nice to have an example of transforming the data sent to the server as in the above to your documentation. I have read the docs but they don't really cover transforming data to a non standard format.
Thanks


7 Replies

KR Keerthana Rajendran Syncfusion Team August 23, 2017 10:57 AM UTC

Hi Dan,   
   
Thank you for contacting Syncfusion support.   
   
Query: I know I can transform the object produced by the datagrid in the beforSend method. Is that the right location?   
   
Yes, you can transform the object in beforeSend method.   
   
Query : Should I inherit from the url adapter or just start from scratch with the base adapter?   
   
You can use custom adaptor in this case by extending Url adaptor.   
   
We have prepared a sample for your reference. Please refer to the below given link   
   
   
In the sample in processQuery method, you can process the query before fetching data as per your requirement.    
   
   
Query: Also it would be nice to have an example of transforming the data sent to the server as in the above to your documentation. I have read the docs but they don't really cover transforming data to a non standard format.   
   
   
Please refer the below links for more details   
   
   
   
If we have misunderstood your requirement, please provide a clear detail of your requirement. If possible, share us your sample along with the requirement so that we can proceed further.   
   
Regards,      
Keerthana.  
 



DA dan August 23, 2017 01:25 PM UTC

The xml docs refer to converting data received from the server.  The header docs has nothing to do with data format sent to the servers but just adding headers

What I what I was looking for was an example of how you would Send data to the server in a specific format. My server expects a Where clause data Sent to the server to look like this  { where: {id: {gt: 1}}, skip: 1, fields: ['id','lastName]}; I don't understand how your example produces a different json format Sent to the server.


Further clarification

So in the grid on the id column. In the filter menu if I choose Greater Than 1 then the I expect where: where: {id: {gt: 1}}, skip: 0, fields: ['id']};   to be sent to the server.


Thank you.




AS Alan Sangeeth S Syncfusion Team August 24, 2017 10:37 AM UTC

Hi Dan, 
 
The filter query will be passed in below JSON format. So, we suggest you to model bind the filter value based on this format. 
 
"[{"Field":\"OrderID","IgnoreCase":false,"IsComplex":false,"Operator":"greaterthan","Condition":null,"value":10266,"predicates":null}]" 

For your convenience we have created a sample and the same can be downloaded from below link 

Regards, 
Alan Sangeeth S 



DA dan August 24, 2017 12:14 PM UTC

I don't understand this example at all.  I don't see a custom adapter in the code, I am used to the javascript version as I know nothing about .net.  How can we produced the json in the format I need without a custom adapter?


Alan I think you miss understood I need to send data to the server in a specific format json format as outlined.


I am sorry to take up so much of your time and I really do appreciate your help 



PN Preethi Nesakkan Gnanadurai Syncfusion Team August 25, 2017 05:24 AM UTC

Subject:  In a custom adapter how do I change the format of the data sent to the server?, has been edited. 

I am at a loss.  I have spent the whole day trying to get this working without overriding UrlAdapter's processQuery.  I just can't figure out what I am doing wrong.  I have tried using beforeSend but you do not have access to the query parameter only the already built url.  I have tried convertToQueryString and inside that method I got the url params I wanted but the final url was not the same additional information such as fields where added. 
 
I need the object that the grid produces to be. I also would need to be able to add a skip object and any other objects  
data = {where: {title: {nlike: 'M.-XY'}}
 
 
If you want me to paste the code of what I have tried but is not working please let me know. 
 
Thank you. 




PN Preethi Nesakkan Gnanadurai Syncfusion Team August 25, 2017 05:26 AM UTC

Subject:  In a custom adapter how do I change the format of the data sent to the server?, has been edited. 

The adapter I am trying to write is for LoopBack Api  https://loopback.io/  just in case you know of an adapter that is already written. 
loopback.io 
IBM and the StrongLoop team are committed to maintaining and improving the LoopBack open-source project! Building on LoopBack's success as an open-source Node.js ... 
 



FS Farveen Sulthana Thameeztheen Basha Syncfusion Team August 28, 2017 05:57 PM UTC

Hi dan, 

You can change the filter query format by extending the custom Adaptor. In the custom Adaptor you can change the filter format in the processQuery  function according to your requirement. 

Please refer to the code example:- 

<script type="text/javascript"> 
        $(function () { 
 
            var customAdaptor = new ej.Adaptor().extend({ 
                 
                processQuery: function (ds, query) { 
                  var result = ds.dataSource.json.slice(0), count = result.length, cntFlg = true, ret, key, agg = {}; 
 
                    for (var i = 0; i < query.queries.length; i++) { 
                        key = query.queries[i]; 
                        ret = this[key.fn].call(this, result, key.e, query); 
                        if (key.fn == "onAggregates") 
                            agg[key.e.field + " - " + key.e.type] = ret; 
                        else 
                            result = ret !== undefined ? ret : result; 
 
                        if (key.fn === "onPage" || key.fn === "onSkip" || key.fn === "onTake" || key.fn === "onRange") cntFlg = false; 
 
                        if (cntFlg) count = result.length; 
                    } 
 
                    //handle where query here 
                    if (query._requiresCount) { 
                        result = { 
                            result: result, 
                            count: count, 
                            aggregates: agg 
                        }; 
                    } 
 
                    return result; 
                }, 
                processResponse: function (data, ds, query, xhr) { 
                    if (data.d) 
                    return data.d; 
                    return data; 
                } 
            }); 
 
 
 
            var dataManager = new ej.DataManager(window.gridData); 
            // assigning custom adaptor to datamanager 
            dataManager.adaptor = new customAdaptor(); 
 
            $("#Grid").ejGrid({ 
                dataSource: dataManager, 
                allowFiltering: true, 
                columns: ["OrderID", "CustomerID", "EmployeeID"] 
            }); 
        }); 
    </script> 

Please get back to us if you need any further assistance. 

Regards, 

Farveen sulthana T 


Loader.
Live Chat Icon For mobile
Up arrow icon