使用 JavaScript 数组中的数据创建 HTML 表的最快方法是什么?

发布于 2024-11-27 09:49:20 字数 1773 浏览 1 评论 0原文

我有以下代码将 JSON 数据集转换为 html 表。 我想知道这是否是最快的方法,或者我应该使用jqote2和jquery来编写模板?

需求:

  • 可以通过单击更改列定义(客户端可以更改查看表的方式,列定义数组将更改,并且可以重建表)
  • 排序,过滤&分页(我相信原始数据可以排序并且表格可以重建)
  • 转换(如果说1列包含不同单位长度的数据,则可以在原始数据集中添加一个新列,其中包含1个相同单位的数据,以便数据可以已排序)

那么如果我走在正确的轨道上或者我正在研究已经存在的东西,有人可以指导我吗?

<div id="hii"><!-- table loads here --></div>

<script>
var set1={ // JSON dataset & col definitions
    'col':[ // definition/template of data to load in column cells
        [function(x){return '<a href="?sid='+x[1]+'">'+x[0]+'</a>';}],
        [function(x){return '<a href="?id='+x[2]+'">'+x[2]+'</a>';}],
    ],
    'data':[ // raw data, output table has only 2cells/row using these 3 values from each row
        ['Name 1','00000001','Value 1'],
        ['Name 2','00000002','Value 2'],
        ['Name 3','00000003','Value 3'],
        ['Name 4','00000004','Value 4'],
        ['Name 1','00000001','Value 5'],
        ['Name 5','00000005','Value 1'],
        ['Name 6','00000006','Value 1'],
        ['Name 7','00000007','Value 2'],
        ['Name 8','00000008','Value 6'],
        ['Name 9','00000009','Value 3'],
        ['Name A','00000010','Value 7'],
        ['Name B','00000011','Value 7'],
        ['Name C','00000012','Value 1'],
    ],
};
function tbody(x){ // function to create table, using dataset name passed to x
    for(i=0,data='';i<x.data.length;i++){
        data+='<tr>';
        for(j=0;j<x.col.length;j++){
            data+='<td>'+x.col[j][0](x.data[i])+'</td>';
        }
        data+='</tr>';
    }
    return data;
}
document.getElementById('hii').innerHTML='<table>'+tbody(set1)+'</table>';
</script>

I have the following code to convert my JSON dataset to html table.
I want to know if it is the fastest method, or should I use jqote2 with jquery to write template?

Requirements:

  • can change col definitions with a click (client can change how to view table, col definition array will change, and table can be rebuilt)
  • sorting, filter & pagination (I believe raw data can be sorted and table can be rebuilt)
  • conversions (If say 1 col contains data of length in different units, than a new col in raw data set can be added having data in 1 same unit so that data can be sorted)

So can somebody guide me if I am on right track or am I working on something that already exists?

<div id="hii"><!-- table loads here --></div>

<script>
var set1={ // JSON dataset & col definitions
    'col':[ // definition/template of data to load in column cells
        [function(x){return '<a href="?sid='+x[1]+'">'+x[0]+'</a>';}],
        [function(x){return '<a href="?id='+x[2]+'">'+x[2]+'</a>';}],
    ],
    'data':[ // raw data, output table has only 2cells/row using these 3 values from each row
        ['Name 1','00000001','Value 1'],
        ['Name 2','00000002','Value 2'],
        ['Name 3','00000003','Value 3'],
        ['Name 4','00000004','Value 4'],
        ['Name 1','00000001','Value 5'],
        ['Name 5','00000005','Value 1'],
        ['Name 6','00000006','Value 1'],
        ['Name 7','00000007','Value 2'],
        ['Name 8','00000008','Value 6'],
        ['Name 9','00000009','Value 3'],
        ['Name A','00000010','Value 7'],
        ['Name B','00000011','Value 7'],
        ['Name C','00000012','Value 1'],
    ],
};
function tbody(x){ // function to create table, using dataset name passed to x
    for(i=0,data='';i<x.data.length;i++){
        data+='<tr>';
        for(j=0;j<x.col.length;j++){
            data+='<td>'+x.col[j][0](x.data[i])+'</td>';
        }
        data+='</tr>';
    }
    return data;
}
document.getElementById('hii').innerHTML='<table>'+tbody(set1)+'</table>';
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情愿 2024-12-04 09:49:20

您可以使用数据表。它可以与 jQuery 配合使用,并且可以进行主题化。您可以设置很多选项来满足您的要求,甚至更多。

You can use Datatables. It works with jQuery and is also themable. There are quite a few options you can set that can meet your requirements, and even more.

物价感观 2024-12-04 09:49:20

这并不容易做到。看一下 Sencha (extJS),尤其是 Grids - http://www.sencha.com/learn /legacy/Tutorialshttp://www.sencha.com/learn/legacy/Tutorials#Grids

It is not so easy to do. Take a look at Sencha (extJS), especially Grids - http://www.sencha.com/learn/legacy/Tutorials, http://www.sencha.com/learn/legacy/Tutorials#Grids

随风而去 2024-12-04 09:49:20

jquery/jqote 也可以做到这一点:

$('#container tbody').empty().html($('#row_template').jqote(set1));

<script type="text/x-jqote-template" id="row_template">
<![CDATA[
    <tr id="<%=this.id%>_valuebox">
        <td><%=this.name%></td>
        <td><%=this.value%></td>
    </tr>
]]>
</script>

jquery/jqote can do this too:

$('#container tbody').empty().html($('#row_template').jqote(set1));

<script type="text/x-jqote-template" id="row_template">
<![CDATA[
    <tr id="<%=this.id%>_valuebox">
        <td><%=this.name%></td>
        <td><%=this.value%></td>
    </tr>
]]>
</script>
小糖芽 2024-12-04 09:49:20

看一下 jQuery 模板插件。

http://api.jquery.com/category/plugins/templates/

<ul id="movieList"></ul>

<script id="movieTemplate" type="text/x-jquery-tmpl">
    <li><b>${Name}</b> (${ReleaseYear})</li>
</script>

<script type="text/javascript">
    var movies = [
        { Name: "The Red Violin", ReleaseYear: "1998" },
        { Name: "Eyes Wide Shut", ReleaseYear: "1999" },
        { Name: "The Inheritance", ReleaseYear: "1976" }
    ];

    // Render the template with the movies data and insert
    // the rendered HTML under the "movieList" element
    $( "#movieTemplate" ).tmpl( movies )
        .appendTo( "#movieList" );
</script>

Take a look at the jQuery Templates Plugin.

http://api.jquery.com/category/plugins/templates/

<ul id="movieList"></ul>

<script id="movieTemplate" type="text/x-jquery-tmpl">
    <li><b>${Name}</b> (${ReleaseYear})</li>
</script>

<script type="text/javascript">
    var movies = [
        { Name: "The Red Violin", ReleaseYear: "1998" },
        { Name: "Eyes Wide Shut", ReleaseYear: "1999" },
        { Name: "The Inheritance", ReleaseYear: "1976" }
    ];

    // Render the template with the movies data and insert
    // the rendered HTML under the "movieList" element
    $( "#movieTemplate" ).tmpl( movies )
        .appendTo( "#movieList" );
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文