Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
825
Having trouble using restSettings with a JSON dataSource
posted

I am trying to set up an igGrid to do CRUD via REST. I have two questions, either one of which might be enough to get us unstuck.

1. We bind the query results (JSON array) to the grid, as follows:

    $.ajax({
        type : 'GET',
        url : BASE_URL + '/ar_invoices',
        data : params,
        dataType : 'json',
        success : function(invoiceRows) {
            $("#ar_invoices").igGrid("option", "dataSource", invoiceRows);
        },
    });

This works well for display. However, when I add the "Updating" feature and restSettings to the grid, things don't work. Not only does the grid not send any updates to the server, but the grid behavior is wrong:

  • Adding a row: when I click the "Done" button the row disappears.
  • Editing a row: when I click the "Done" button the row reverts to its previous values.
  • Deleting a row: the row gets crossed out, but when I call igGrid("saveChanges") nothing is sent to the server and the row remains crossed out.

So the first question is: can restSettings be used if the dataSource is NOT a URL, but is instead a JSON array? Every REST example I've seen uses a URL dataSource so I'm thinking that might be part of our problem.

2. To try and answer question #1 I tried to set our dataSource to the URL instead:

    var url = BASE_URL + '/ar_invoices';
    $("#ar_invoices").igGrid("option", "dataSource", url);


The GET request sent by the grid includes Accept = "*/*" in the header. Unfortunately the service supports both JSON and XML using the same URL. It relies on the Accept header to determine what format to send back, but defaults to XML. So we'd need the grid to explicitly request JSON in the header(*). Is there a way to customize the headers it sends?

(*) Note that the XML feed isn't working: igGrid is throwing a parse error. But as igGrid seems to prefer JSON (and so do we) we don't want to spend any time on the XML issue if we can help it.

FWIW we'd prefer the approach in #1, as this gives us full control of the Ajax query. (We might need that to work around CORS issues, for example.) So if there's nothing inherently wrong with that approach then I'll probably have to post a followup message with more details.

Thanks for any assistance,

Matt