Callback before setting data from ajax

Callback before setting data from ajax

irxn84irxn84 Posts: 12Questions: 5Answers: 0

I have a very strange problem in my datatable. For a column, there is the wrong data loaded.
The data from the ajax call is okay, I checked the json.
So for one column, datatable takes the data from my backbone model class. Every value of this column is from the model variable. When I change the value in my model class, the value of the datatable column changes also.
I tried to rename the variable in the model, but datatable still takes the value from the model.
For debugging, I want to know if it is possible to have a callback before the data is set.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @irxn84 ,

    DataTables will just use the data in the DOM or the data you pass it - it sounds like your models are doing something clever. We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • irxn84irxn84 Posts: 12Questions: 5Answers: 0
    edited May 2019

    My datatable is a bit complicated, the columns are dynamic for every view.

    My ajax configuration:

    ajax: {
                            url: ldUrls.operation.dynamicView(that.model.get('viewDef').get('forClass'), env),
                            type: 'POST',
                            dataType: 'json',
                            data: function (data) {
                                _.each(matchFilterIndexes, function (val, index) {
                                    var key = 'columns[' + val + '][unique]';
                                    data[key] = true;
                                })
                            },
                        },
    

    The json I get:

    {"data":[{"id":239746502,"generated":"2019-04-10T09:50:49Z","environment":null,"type":"WORKFLOW_RELEASE","error":false,"direction":null,"workflowname":"RIT_PROBLEM To SIAM Core","connectorname":null,"itiltoolname":null,"alerthandler":null,"description":"Name: null\nDescription: null","boxusername":"PK","messageId":0,"messageSize":0},{"id":239746511,"generated":"2019-04-10T09:50:49Z","environment":"test","type":"WORKFLOW_DEPLOY","error":false,"direction":null,"workflowname":"RIT_PROBLEM To SIAM Core","connectorname":null,"itiltoolname":null,"alerthandler":null,"description":"Release ID: 239746506\nName: null\nDescription: null","boxusername":"PK","messageId":0,"messageSize":0}, ........
    

    Every data is displayd okay, except of the column environment.

    Columns are defined as an array with render information and data attribute.
    The data attribute is like in json id, error, type, environment.
    Everything is okay, but just the environment is not loaded from json.

    I can debug this myself. But I need a hint where datatables is setting the data from the json. So than I can check, where the wrong environment is coming from

  • kthorngrenkthorngren Posts: 21,144Questions: 26Answers: 4,918

    Your data example seems to render just fine here:
    http://live.datatables.net/jukokoge/1/edit

    In order to troubleshoot I would start with looking at the response in the Developer Tools. You could use the ajax.dataSrc or the xhr event to see the JSON that Datatables is using. You can use columns.render for the environment column to console.log the data parameter.

    Otherwise maybe you can update or post an example replicating the issue.

    Kevin

  • irxn84irxn84 Posts: 12Questions: 5Answers: 0

    This example used in my code does not work. I still get another value for environment.
    When I rename the column for example to environment1, everything works fine.
    So somewhere an environment variable is set in my code in the background. But I just did not find out where. In the render method there is already the wrong data. I now did not use ajax, but the data directly like in the example

  • colincolin Posts: 15,237Questions: 1Answers: 2,598
    Answer ✓

    As Kevin said, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • irxn84irxn84 Posts: 12Questions: 5Answers: 0

    Okay, I renamed the column in my application. I think this is the easier way to handle this. When I ever figure out, what the real problem was, I write it here in the thread.

This discussion has been closed.