将JSON结果转换为动态表

时间:2015-02-03 05:10:01

标签: javascript jquery json dynamic

我有一段时间用MVC(变量列计数)创建动态表。我使用了json结果,但是使用jquery .each()迭代器会导致我的表列自动排序(Chrome)。有没有办法移动列?我需要名称位置作为第一个。

var data =  
[{"Name":"Craven Moorehead","Position":"SV-WA-1","601036":"x","600001":"x","600003":"x"},{"Name":"Vinny MCCuzin","Position":"SV-TECH-1","601036":"x","600001":"x","600003":"x"},{"Name":"Harold Balzonia","Position":"SV-TECH-0","601036":"x","600001":"x","600003":"x"},{"Name":"Phil  McCrevic","Position":"PT-PM-1","601036":"x","600001":"x","600003":"x"},{"Name":"Hewenta Jared","Position":"SV-TECH-1","601036":"x","600001":"x","600003":"x"}]


    var tablehtml = "<table class='table table-striped table-bordered table-condensed'>";
            var header = "<thead><tr>";
            var datain = data[0];
            $.each(datain, function (key, value) {                    

                header += "<th>" + key + "</th>";
                console.log(key);
            });
            header += "</tr></thead>";
            var rowhtml = "<tbody>";
            $.each(data, function () {
                rowhtml += "<tr>";
                $.each(this, function (k, v) {
                    /// make a row
                    rowhtml += "<td>";

                    if (v == "x") {

                        rowhtml += "<input type='checkbox'/>";
                    } else {

                        rowhtml += v;
                    }
                    rowhtml+= "</td>";
                });
                rowhtml += "</tr>"
            });

            tablehtml += header + rowhtml + "</tbody></table>";
$("#headers").append(tablehtml);


<pre id="output"></pre>
<pre id="headers"></pre>

输出如下:

JSFiddle

0 个答案:

没有答案