Column with array not working

Column with array not working

jdoensjdoens Posts: 18Questions: 4Answers: 0

I had added an array object into another JSON object

JSON data:

[{A: 4, B: "test", C: [{x: "inserted", y: "test"}]},
{A: 1, B: "test2", C: [{x: "blah", y: "asdf"}]}]

I'm trying to access the C column and populate it in DataTables, however, nothing seems to work.

I've tried the follow:

    .....
    "columns": [
        { "data": "A" },
        { "data": "B" },
        {
            "data": null,
            "render": "C[, ]"
            //"render": function (data, type, full, meta) {
            //    return data.x;
            //}
    .....

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929
    edited March 2018

    First, if you can, I would remove the array so it looks more like this:

    [
        {
            A: 4,
            B: "test",
            C: {
                x: "inserted",
                y: "test"
            }
        },
        {
            A: 1,
            B: "test2",
            C: {
                x: "blah",
                y: "asdf"
            }
        }
    ]
    

    Are you wanting to display the C object in one column or multiple?

    If multiple the Nested Objects example will show you how.

    If not then here is an example render:

        columns: [
          {
            data: "A"
          },
          {
            data: "B"
          },
          {
            data: "C",
            render: function ( data, type, row, meta ) {
              return 'X: ' + data.x + ' Y: ' + data.y;
            }
          }
        ]
    

    If you leave it in the array then you can use:
    return 'X: ' + data[0].x + ' Y: ' + data[0].y;

    Kevin

  • jdoensjdoens Posts: 18Questions: 4Answers: 0

    Since C sometimes contains an array of X and Y, it has to stay that way. Unfortunately, I tried what you said and had gotten an error saying Cannot read property 0 of undefined. I've looked at the object and everything looks fine.

                    "data": "C",
                    "render": function (data, type, full, meta) {
                        return data[0].x;
                    }
    
  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929
    Answer ✓

    I might be missing something but it seems to work here:
    http://live.datatables.net/faxezage/1/edit

    Kevin

  • jdoensjdoens Posts: 18Questions: 4Answers: 0

    The issue is something with my object. Once I try to stringify it, the entire object is blank. I tried to add a new column to the object by doing
    jsonObj[i].C = data;

    The object would appear with the C array added, but once stringified, it turns into a blank object.

  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929

    Why are you stringifying it?

    I guess a better question is what is the data source for Datatables?

    Maybe you can post your code around how Datatables is getting the data.

    Kevin

  • jdoensjdoens Posts: 18Questions: 4Answers: 0

    I'm getting the data via ajax and passing it to another function that iterates through every element of "A" to get it's corresponding data "C".

    What I'm doing is adding the "C" array to the original JSON object then passing it to the Datatables. It actually appears that C isn't getting added.

    I tried to do it using:

    var newObj = [];
     jQuery.each(items, function (i, val) {
            $.ajax({
                type: 'GET',
                url: "http://myRequestUrl",
                crossDomain: true,
                dataType: 'json',
                "async": true,
                success: function (data) {
                    //newObj.push(
                    //    {
                    //        A: val.A,
                    //        B: val.B,
                    //        C: [ data ]
                    //    }
                    //);
                   originalData[i].C = data;
                },
    

    The commented portion is when I tried to construct a new array and then stringify it.

  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929
    Answer ✓

    I might be missing something but it looks like for every value in items you are fetching data via the ajax call. Unless you are changing the URL it seems like each pass through the loop would retrieve the same data.

    I wonder if you can get all of C in one query then use that data structure to build originaData. Better yet maybe you can structure your server script's DB query to use some sort of join method to get all the data in one query and already have the associated data together.

    BTW, its not recommended to use "async": true, in your ajax calls as that pauses the web page while the ajax request is being processed. Not always a bad thing depending on when and how much delay the request takes.

    Maybe you can describe in more detail your environment, how the data relates in the backend, etc.

    Kevin

  • jdoensjdoens Posts: 18Questions: 4Answers: 0

    I figured out the issue. I'm iterating through each element and having to make separate calls. I thought setting async true would wait for the application to complete and allow the iteration to complete to completely finish forming my object. After that I needed to call another method and pass that object over. What's happening is that it calls that function before it even finishes causing the DataTables not to load the necessary info.

This discussion has been closed.