Data in nested JSON response gets "lost" when passed to a render function

Data in nested JSON response gets "lost" when passed to a render function

crazyPhDcrazyPhD Posts: 6Questions: 3Answers: 0

Hi everyone,

after reading the docs on columns.render I think I found an error in the docs or the implementation:

According to the docs the parameter row passed to a render function will be the full data source for the row and that columns.data has no effect on this. But I experienced the exact opposite with DataTables 1.10.18: When a JSON attribute name is not mentioned in columns.data, it will not be part of the object passed as row to render().

Now to the example:

HTML

<h2>Maintenance requests ready</h2>
<table id="dt_submission_ready"
       class="table table-hover table-striped responsive display nowrap"
       data-ajax="{% url 'api:v1-overview-submission_ready' %}?format=datatables"
       width="100%" cellspacing="0">
    <thead>
        <tr>
            <th data-searchable="false" data-orderable="false"
                data-data="category">
                {% include 'overview/datatable/cell_category_select_button.html' %}
            </th>
            <th data-searchable="true" data-orderable="true"
                data-data="request_id">
                ID
            </th>
            <th data-searchable="false" data-orderable="false"
                data-data="comments_exists"></th>
            <th data-searchable="true" data-orderable="true"
                data-data="created">
                Created
            </th>
            <th data-searchable="false" data-orderable="false" data-data="age">
                Age
            </th>
            <th data-searchable="true" data-orderable="true"
                data-data="created_by">
                Submitter
            </th>
            <th data-searchable="true" data-orderable="true"
                data-data="packages" data-name="packages">
                Packages
            </th>
            <th data-searchable="true" data-orderable="true"
                data-data="codestreams">
                Codestreams
            </th>
            <th data-searchable="true" data-orderable="true"
                data-data="channels">
                Products
            </th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

JSON response

{
    "data": [
        {
            "age": "30 20:32:52.159494",
            "category": {
                "name": "security"
            },
            "channels": [
                "Channel 1"
            ],
            "codestreams": [
                "Code Stream 1"
            ],
            "comments_exists": true,
            "created": "2019-08-13T12:33:29Z",
            "created_by": "user1",
            "deadline": null,
            "existing_running_incidents": [],
            "existing_staged_incidents": [],
            "incident": null,
            "kind": "MI",
            "packages": [
                "Package 1",
                "Package 2",
                "Package 3"
            ],
            "rating": null,
            "references": [
                {
                    "name": "CVE-2018-5390",
                    "tracker": 6,
                    "url": "url1"
                },
                {
                    "name": "bnc#1102682",
                    "tracker": 2,
                    "url": "url2"
                }
            ],
            "reject_reason": null,
            "request_id": 198758,
            "status": {
                "name": "new"
            },
            "unfinished_reviews": [],
            "url": "url3"
        },
        ...
    ],
    "draw": 1,
    "recordsFiltered": 16,
    "recordsTotal": 16
}

Object passed as row to render

{
  "request_id": 198758,
  "age": "30 20:35:36.307719",
  "category": {
    "name": "security"
  },
  "comments_exists": "url4",
  "created_by": "user1",
  "created": "2019-08-13T12:33:29Z",
  "channels": [
    "Channel 1"
  ],
  "packages": [
    "Package 1",
    "Package 2",
    "Package 3"
  ],
  "codestreams": [
    "Code Stream 1"
  ]
}

Conclusion

IMHO the docs imply that the complete JSON object relevant for the current row including unused attributes and nested structures is passed as row to render functions. My guess is, that the data is reduced for performance reasons, so I will not find any friends by asking to disable/remove this feature? But maybe rephrasing the docs might help others.

Answers

  • kthorngrenkthorngren Posts: 6,474Questions: 22Answers: 1,415

    I've not experienced this problem. I took your JSON example and placed into this test case and all of the data is there:
    http://live.datatables.net/lelawebu/2/edit

    Since I can't use ajax to fetch your data I also put a server side (look like you are using server side processing) example with objects here. All of the data is in the row even though its not referenced by columns.data.

    Do you have your parameters in the correct order for the render function? Sounds like maybe you have row and data swapped.

    Kevin

  • crazyPhDcrazyPhD Posts: 6Questions: 3Answers: 0

    Hi Kevin,

    sorry for the late reply. I have double checked, my render functions seem fine and in one of them I'm looking at the entire row. That's where I miss data in the row argument, which is present in the JSON response of the server. By simply adding an invisible column without assigning a render function, the missing data appears in the render function of the other column.

    If you can't reproduce this with the shortened example data, DataTables might have an issue with "larger" datasets?

  • colincolin Posts: 6,208Questions: 0Answers: 1,066

    Hi @crazyPhD ,

    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

Sign In or Register to comment.