gordyr
gordyr

Reputation: 6276

How to access nested data with jQuery templates

I am trying to get my head arround the jQuery templates plugin.

My JSON is formatted like this:-

{
"sEcho":1,
"total":"1710",
"aaData":[
    [
        "Help",
        "http:\/\/www.mysite.com\/wp-content\/uploads\/2011\/09\/dt_intfc4e732d1f1276d_4e76fab1e95bd.mp3?King_of_Spain_Entropy_02_Animals_Part_1.mp3",
        "1784",
        "3",
        0,
        null,
        "0000-00-00 00:00:00"
    ],
    [
        "A Day In The Life",
        "http:\/\/www.mysite.com\/wp-content\/uploads\/2011\/09\/dt_intfc4e732d1f1276d_4e76f5fc253a1.mp3?JenWood_Zeppelin.mp3",
        "3573",
        "3",
        0,
        null,
        "0000-00-00 00:00:00"
    ]
}

I am trying to use jQuery templates to take this JSON array and apply a tr node template for each object in this within "aaData" in this array as well as render seperate 'td' nodes for the individual pieces of data.

However all of the examples I have seen have JSON formatted with unique names for each piece of data within an object i.e. "name", "value" etc. I am unable to re-format the JSON source so I need to access the nested data via it's index.

My template and code is currently like this:-

$.ajax({
    "dataType": 'json',
    "url": '/wp-content/hovercard.php',
    "data": 'user=' + $artistid + '&start=' + $start + '&finish=' + $finish + '&order=' + $orderValue + '&orderColumn=' + $columnValue,
    "success": function(data, textStatus, jqXHR) {

        var domain = data.domain;
        var array = data.aaData;
        var $table = $('#favourites-hovercard-table-'  + $artistid );

        var markup = '<tr class="odd">'
                         + '<td class="sorting_1"><div class="audio"><a href="${1}" class="sm2_button"><div class="play_button"></div></a></div></td>'
                         + '<td class="hovercard-song"><div class="hovercard-row"><span class="hovercard-songs ajax"><a href="' + domain + 'song/?songsID=${2}">${0}</a></span></div><div title="${5} people have voted on this song" class="ministars minitip dark-tooltip"><div class="star_1 ratings_stars ratings_vote"></div><div class="star_2 ratings_stars ratings_vote"></div><div class="star_3 ratings_stars ratings_vote"></div><div class="star_4 ratings_stars ratings_vote"></div><div class="star_5 ratings_stars ratings_vote"></div><div class="star_6 ratings_stars ratings_vote"></div><div class="star_7 ratings_stars ratings_vote"></div><div class="star_8 ratings_stars"></div><div class="star_9 ratings_stars"></div><div class="star_10 ratings_stars"></div></div></td>'
                         + '<td><div class="hovercard-row hovercard-rating hidden_column">${4}</div></td><td><div class="hidden_column hovercard-row">3</div></td>'
                         + '<td><div class="hidden_column hovercard-row"></div></td>'
                         + '<td class="hovercard-time"><div title="${6}" class="hovercard-row uploaded_time minitip timeago dark-tooltip">${6}</div></td>'
                    + '</tr>';

        $.template( "favouritesTemplate", markup );
        $.tmpl( "favouritesTemplate", array ).appendTo($table);



        }

});

As you can see above, within my template I am trying to use ${1}, ${2}, ${3} etc.. to access the index of the data within the object since it is unnamed.

Clearly this is the wrong approach and I must be missing something vital here.

How do I access this data? within my template in order to append more rows to my table on demand.

Also, I would like to be able to alternate the class of the tr node between 'odd' and 'even'. Is this at all possible with jQuery templates?

Many thanks.

EDIT:

I have managed to conditionally modify the server side script so this for this instance it will output the JSON data complete with object names like this:-

     {
    "sEcho":1,
    "total":"1710",
    "aaData":[
        [
            "song_name": "Help",
            "song_file": "http:\/\/www.mysite.com\/wp-content\/uploads\/2011\/09\/dt_intfc4e732d1f1276d_4e76fab1e95bd.mp3?King_of_Spain_Entropy_02_Animals_Part_1.mp3",
            "songsID": "1784",
            "user": "3",
            "rating": 0,
            "ratings_count": null,
            "uploaded_time" "0000-00-00 00:00:00"
        ],
}

And have now updated my template as follows:-

var markup = '<tr class="odd">'
                + '<td class="sorting_1"><div class="audio"><a href="${song_file}" class="sm2_button"><div class="play_button"></div></a></div></td>'
                + '<td class="hovercard-song"><div class="hovercard-row"><span class="hovercard-songs ajax"><a href="' + domain + 'song/?songsID=${songsID}">${song_name}</a></span></div><div title="${ratings_count} people have voted on this song" class="ministars minitip dark-tooltip"><div class="star_1 ratings_stars ratings_vote"></div><div class="star_2 ratings_stars ratings_vote"></div><div class="star_3 ratings_stars ratings_vote"></div><div class="star_4 ratings_stars ratings_vote"></div><div class="star_5 ratings_stars ratings_vote"></div><div class="star_6 ratings_stars ratings_vote"></div><div class="star_7 ratings_stars ratings_vote"></div><div class="star_8 ratings_stars"></div><div class="star_9 ratings_stars"></div><div class="star_10 ratings_stars"></div></div></td>'
                + '<td><div class="hovercard-row hovercard-rating hidden_column">${rating}</div></td><td><div class="hidden_column hovercard-row">${user}</div></td>'
                + '<td><div class="hidden_column hovercard-row"></div></td>'
                + '<td class="hovercard-time"><div title="${uploaded_time}" class="hovercard-row uploaded_time minitip timeago dark-tooltip">${uploaded_time}</div></td>'
            + '</tr>';

But it is still not rendering the data in the tr/td nodes that it creates. According to firebug all the data is being tied to the initial 'tr' node.

I'm unsure where I am going wrong?

Upvotes: 0

Views: 507

Answers (1)

Richard
Richard

Reputation: 4415

I don't think you can do this. All the variables need to be named. Have a look at this page:

http://api.jquery.com/jQuery.template/

Where are you getting the JSON from?

Upvotes: 1

Related Questions