下面的 JS 在解析 JSON 后为我返回一个电影标题列表。每个电影标题节点都有更多尚未显示的属性和值。当用户单击列表中的电影标题时,我希望它返回该节点中与该标题匹配的其他值。我该怎么做呢?谢谢。
我发布了一个数据示例。JS 创建所有电影标题的 HTML 列表。如您所见,同一电影标题有多个节点,但“位置”不同。我希望能够单击列表中的电影标题并让它在 HTML 中显示特定电影标题的所有不同位置。
这是它现在正在做的事情的 JS 小提琴。JSfiddle
JSON数据:
fl({
"nodes" : [
{
"node" : {
"title" : "180",
"ReleaseYear" : "2013",
"Director" : "Jayendra",
"Writer" : "Umarji Anuradha, Jayendra, Aarthi Sriram, & Suba ",
"Address" : "\n \n \n 555 Market St. \n San Francisco, CA\n United States\n \n \n See map: Google Maps \n \n",
"Actor 1" : "Siddarth",
"Actor 2" : "Nithya Menon",
"Actor 3" : "Priya Anand",
"Latitude" : "37.789952",
"Longitude" : "-122.400158"
}
},
{
"node" : {
"title" : "180",
"ReleaseYear" : "2013",
"Director" : "Jayendra",
"Writer" : "Umarji Anuradha, Jayendra, Aarthi Sriram, & Suba ",
"Address" : "\n \n \n Epic Roasthouse (399 Embarcadero) \n San Francisco, CA\n United States\n \n \n See map: Google Maps \n \n",
"Actor 1" : "Siddarth",
"Actor 2" : "Nithya Menon",
"Actor 3" : "Priya Anand",
"Latitude" : "37.797677",
"Longitude" : "-122.394339"
}
},
{
"node" : {
"title" : "180",
"ReleaseYear" : "2013",
"Director" : "Jayendra",
"Writer" : "Umarji Anuradha, Jayendra, Aarthi Sriram, & Suba ",
"Address" : "\n \n \n Mason & California Streets (Nob Hill) \n San Francisco, CA\n United States\n \n \n See map: Google Maps \n \n",
"Actor 1" : "Siddarth",
"Actor 2" : "Nithya Menon",
"Actor 3" : "Priya Anand",
"Latitude" : "37.791556",
"Longitude" : "-122.410766"
}
}
]
})
JAVASCRIPT:
var sort_by = function(field, reverse, primer){
var key = function(x) {return primer ? primer(x[field]) : x[field]};
return function(a,b) {
var A = key(a), B = key(b);
return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1,1][+!!reverse];
}
}
$.ajax({
type: 'GET',
url: 'list',
async: false,
jsonpCallback: 'fl',
contentType: 'application/json',
dataType: 'jsonp',
success: function(data) {
var tmp = [];
for(var i=0;i<data.nodes.length;i++) {
tmp.push(data.nodes[i].node);
}
tmp.sort(sort_by('title', true));
var div = document.createElement('div');
$('#film').append(div);
$(div).attr( 'data-role', 'collapsible' );
var heading = tmp[0]['title'][0];
var h3 = document.createElement('h3');
h3.innerHTML = heading;
$(div).append(h3);
h3=null;
var title = '';
for(var i=0;i<tmp.length;i++) {
if(tmp[i]['title'] != title){ title=tmp[i]['title'];
if(tmp[i]['title'][0] !== heading){
heading = tmp[i]['title'][0];
var div = document.createElement('div');
$('#film').append(div);
$(div).attr( 'data-role', 'collapsible' );
var h3 = document.createElement('h3');
h3.innerHTML = heading;
$(div).append(h3);
h3=null;
var p = document.createElement('p');
$(div).append(p);
}
var film = "<a>"+tmp[i]['title']+"</a> <br />";
$(p).append(film);
film=null;
//$("#sf_film").collapsibleset();
$("#film").collapsibleset("refresh");
}
}
}
});
function fl(json){
console.log(json);
}