2013-03-08 65 views
0

這是一個包含座位信息的json文件。jQuery從json數據中添加類

var jsondata = { 
    "who": "RSNO", 
    "what": "An American Festival", 
    "when": "2013-02-08 19:30", 
    "where": "User Hall - Main Auditorium", 
    "seats": ["00000000000000000011111111111111000000000000000000", "0000000000000001111111111111111aaa0000000000000000", "00000000000000aa111111111111111aaaaa00000000000000", "00000000000001111111111111111111111111000000000000", "000000000aa00aaaaaaaaaaaaaaaaaaaaaa1100aa000000000", "00000001111001111111111111111111111100111100000000", "00000aaaaaa0011aaaaaaaaa11111111aaa1100aaaaaa00000", "00001111111001111111111111111111111100111111100000", "000aaaaaaa110011111111111111111111110011aaaaaaa000", "00111111111100111111111111111111111001111111111000", "00aaaaa1111110011111111111111111111001111aaaaaaa00", "11111111111100111111111111111111111001111111111110", "0aaaaaaaaaaaa001111111111111111111100aaaaaaaaaaaa0", "01111111111110011111111111111111110011111111111100", "00000000000000001111111111111111110000000000000000", "01111111111111001111111111111111100111111111111100", "01111111111111001111111111111111110011111111111110", "01111111111111001111111111111111100111111111111100", "00a11111111111100111111111111111100111111111111a00", "00111111111111100111111111111111001111111111111000", "00011111111111110011111111111111001111111111111000", "00111111111111100111111111111111001111111111111000", "00011111111111110011111111111111001111111111111000", "00011111111111110011111111111110011111111111110000", "0000000111a111111001111a1111a110011111111110000000", "00000000111111110011111111111110011111111000000000", "00000000001111111001111111111110011111110000000000", "00000000000000111001111111111100111000000000000000"], 
    "rows": ["DD", "CC", "BB", "AA", "Z", "Y", "X", "W", "V", "U", "T", "S", "R", "Q", "P", "N", "M", "L", "K", "J", "H", "G", "F", "E", "D", "C", "B", "A"], 
    "seatPrice": ["     00000000000000     ", "    0000000000000000000    ", "    0000000000000000000000    ", "    0000000000000000000000000   ", "   00 000000000000000000000000 00   ", "  0000 00000000000000000000000 0000  ", "  000000 000000000000000000000000 000000  ", " 0000000 00000000000000000000000 0000000  ", " 000000000 0000000000000000000000 000000000 ", " 0000000000 000000000000000000000 0000000000 ", " 00000000000 00000000000000000000 00000000000 ", "000000000000 000000000000000000000 000000000000 ", " 000000000000 00000000000000000000 000000000000 ", " 000000000000 0000000000000000000 000000000000 ", "    000000000000000000    ", " 0000000000000 00000000000000000 0000000000000 ", " 0000000000000 000000000000000000 0000000000000 ", " 0000000000000 00000000000000000 0000000000000 ", " 0000000000000 0000000000000000 0000000000000 ", " 0000000000000 000000000000000 0000000000000 ", " 0000000000000 00000000000000 0000000000000 ", " 0000000000000 000000000000000 0000000000000 ", " 0000000000000 00000000000000 0000000000000 ", " 0000000000000 0011111111100 0000000000000 ", "  0000000000 111111111111 0000000000  ", "  00000000 1111111111111 00000000   ", "   0000000 111111111111 0000000   ", "    000 00000000000 000    "], 
    "priceLookup": [10, 20] 
}; 


$.getJSON('jsondata' , function(data) { 
    var tbl_body = ""; 
    $.each(jsondata.seats, function() { 
     var tbl_row = ""; 
     $.each(this, function(k , v) { 
      tbl_row += "<td>"+v+"</td>"; 
     }); 
     tbl_body += "<tr>"+tbl_row+"</tr>";     
    }); 
    $("#plan").html(tbl_body); 
}); 

末該功能可在使用座椅佈局方面很好,但是,我需要添加的類從函數生成的td S在seatPrice舉行的信息。我已經試過基本上覆制最後一個函數,但對於seatPrice對象並將其附加到表中,但這似乎不起作用。任何關於如何去做這件事的建議?

+0

如果我說得對,你問什麼,你需要使用兩個單獨的,但相等的長度,數組中串聯。使用香草方法'的(我在jsondata.seats)'和完成使用'jsondata.seats [I]'和'jsondata.seatPrice [I]' – Popnoodles 2013-03-08 01:44:16

+0

如何在seatPrice的信息是一個類的HTML?即使你只是把那裏的字符串分配給一個類,它也是無效的。 – 2013-03-08 01:46:24

+0

@JeffB B嵌套if語句將被用來改變將要追加的內容 即 'if(i ==「」){// append「blank」to class //}'等 – 2013-03-08 01:48:32

回答

1

你的代碼有一些問題要開始。如果您已有jsondata中的數據,爲什麼需要$.getJSON()?如果你真的從一個AJAX調用得到這個數據,這很好,但你需要參考data的功能,而不是jsondata,或重命名參數匿名函數。

在傳遞給$.each()函數的第一個參數是該陣列的索引。你可以用它來獲取相同的指數seatPrice

$.each(jsondata.seats, function (j, d) { 
    var tbl_row = ""; 
    $.each(this, function (k, v) { 
     var seatPrice = jsondata.seatPrice[j][k]; 
     var seatPriceClass = ''; 
     if(seatPrice == "0") { 
      seatPriceClass = "notawesome"; 
     } else if(seatPrice == "1") { 
      seatPriceClass = "awesome"; 
     } else { 
      seatPriceClass = "none"; 
     } 
     tbl_row += "<td class='"+seatPriceClass+"'>" + v + "</td>"; 
    }); 
    tbl_body += "<tr>" + tbl_row + "</tr>"; 
}); 

演示:http://jsfiddle.net/jtbowden/pJhgK/

+0

這在我的小提琴中起作用,但由於某種原因,而不是在我的實際文檔中,並沒有拋出任何錯誤......奇怪的是,但我會用它。謝謝! – 2013-03-08 02:18:43

0

好像你使用$。每()循環是複雜的問題。正如popnoodles所說,你可以使用香草循環。

是這樣的,你要去什麼?

$.getJSON('jsondata' , function(data) { 
    var tbl_body = ""; 
    var tbl_row = ""; 

    for (var i in data.seats){ 
     tbl_row += "<td>"+i+"</td>"; 
    } 


    for (var i in data.seatPrice){ 
     tbl_row += '<td class="' + i + '">this is a td</td>'; 
    } 

    tbl_body += "<tr>"+tbl_row+"</tr>"; 

    $("#plan").html(tbl_body); 
}); 
+0

除了你正在添加兩個元素。 OP希望使用來自'seat'和'seatPrice'的數據。另外,您正在使用'i'作爲您的第一個索引,然後使用'v'。 – 2013-03-08 02:08:13