jQuery Cell Referencing

时间:2013-03-08 05:26:15

标签: javascript jquery html json reference

此代码创建座位布局。这些类确定了座位的成本,jquery对象包含了布局,以及我确定类的数据。

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", "xx", "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]
};

var counter = 0;
var tbl_body = "";
var print = "";

$.each(jsondata.seats, function (j, d) {
var tbl_row = "";
$.each(this, function (k, v) {
    var seatPrice = jsondata.seatPrice[j][k];            
        if (v == "1" || v == "a") {
            counter++;
        }

        if (v == 0) {
            print = " ";
        } else {
            print = v;
        }
        var seatPriceClass = '';
        if (seatPrice == "0") {
            seatPriceClass = "p";
        } else if (seatPrice == "1") {
            seatPriceClass = "n";
        } else {
            seatPriceClass = "none";
        }
        if (v == "a") {
            seatPriceClass = "x";
        }


        tbl_row += "<td class=" + seatPriceClass + " id=" + counter + ">" + print + "</td>";

    });

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

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

在两个数组中,将创建表以填充以创建布局。计数器在找到座位时开始计算一行(即,座位为1,没有座位为0,所以当它达到1时,它开始计数直到行的结束)。到目前为止,这一切都正确且有效,除了我需要使用JSON文件中的rows对象创建一个行引用,我可以将其附加到计数器数据以给单元格一个id(即c7),然后我可以用作座位参考。

我已尝试嵌套此函数,但它不起作用,或抛出错误

 $.each(function(j, g){
    var row_id =jsondata.rows[j][g];
     });

我如何获得此功能,或类似的东西在我的阵列中工作?

1 个答案:

答案 0 :(得分:0)

只需使用jsondata.rows[j]

即可
tbl_row += "<td class=" + seatPriceClass + " id=" + jsondata.rows[j] + counter + ">" + print + "</td>";