Accessing side loaded JSON data

Accessing side loaded JSON data

NeighlydNeighlyd Posts: 10Questions: 4Answers: 0

Hey everyone,

This is probably more of a jquery question, but I was hoping someone here could help me answer it. My API data is currently nested, but that is causing my calls to be really slow. Since I am referencing the same data repeatedly, I decided that it would be better to side-load the information. Such that my data looks something like this:

{ data: [
    "people": [ 
         {"id": 1, 
           "first_name": "John", 
           "last_name": "Doe", 
           "village": 1
          } , 
          {"id": 2, 
            "first_name": "Jane", 
            "last_name": "Doe", 
            "village": 1
            } , 
           {"id": 3, 
            "first_name": "Harold", 
            "last_name": "Smith", 
            "village": 2
           } 
     ] , 
     "village":[ 
          {"id": 1, 
           "name": "Badbury"
          },
          {"id":2, 
           "name": "Christian Malford" 
         }
      ]
]
}

The problem I am having is in displaying the appropriate village name for the individuals. I understand that I need to use dataSrc: 'data' and then data: 'person.first_name' (etc.) for the columns. I'm just stuck on what to do for village.

Do I do data: 'person.village' and then do some jquery json parsing in the render: display: function() section? That seems like it would increase client-side overhead by a lot (and I'm honestly not even sure how I would go about doing that (I would assume with JSONparser() and $.each, but again, that seems like a lot of work, especially since my person list is over a thousand items long).

Regardless, any help would be extremely appreciated here.

Thanks in advance!

Answers

  • NeighlydNeighlyd Posts: 10Questions: 4Answers: 0

    In the event that anyone else finds this, here is how I solved the problem:

    I had to dig through meta in the row's render function to extract the json payload and assign it appropriately. I used the find() function to look through the array and map the object to a variable. It seems that meta is the only variable that has access to the parent json load.

    Here is my code:

    var village = meta.settings.json.villages.find(x => x.id === row.village);
    return "<a href='" + Urls['village:detail'](row.village) + "'>" + village.name + "</a>";
    
This discussion has been closed.