javascript - 根据数据构建表状结构

标签 javascript jquery jquery-mobile

有没有一种方法可以根据网站上的数据集(使用javascript)动态构建类似表格的结构?

用伪代码类似:

 function pseudo(dataset) {
    <table>
    <th>dataset.name, dataset.id</th>
    foreach dataset.schedular.array as a {
     <tr><td>a.start_time</td><td>a.end_time</td><td>a.client.name</td></tr>
    }
    </table>
 }


并执行如下:

 <div>
 <script>pseudo(json[employee[0]]);</script>
 </div>


在php中,我使用smarty-templates将数据“填充”到类似的掩码中,现在我需要在javascript中使用类似的东西。
我在寻找jquery小部件或插件吗?在哪里可以找到有用的教程或书籍?

最佳答案

我使用jQuery和以下构造。 employees是对象的JS数组。 HTML表是这样准备的:

<table id="clients">
<thead><tr><th>Start</th><th>End</th><th>Name</th></tr></thead>
<tbody></tbody>
</table>


然后,使用each的以下jQuery循环用于添加以下行:

$.each(employees, function(index, employee){
    $('#clients > tbody').append(listItem(index, employee)); 
});


其中listItem()是一个返回表行的函数。当然,可以更优雅地完成此操作:

function listItem(index, employee) {
    var item =  '<tr>';
    item += '<td>' + employee.start_time + '</td>';
    item += '<td>' + employee.end_time + '</td>';
    item += '<td>' + employee.name + '</td>';
    item += '</tr>';
    return item;
}

关于javascript - 根据数据构建表状结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16812081/

相关文章:

html - 使用jQuery Mobile的Apex:无圆 Angular 的图像

ruby-on-rails - 如何在没有 Rhosync 或 RhoConnect 的情况下使用 Rhodes?

javascript - &lt;script src =…进行跨框架脚本查找站点的状态码-安全问题?

javascript - AngularJS 淡入/淡出 View

jquery - 使用 jQuery UI 使可拖动元素在可放置中可排序

jquery - window.history.pushState 不会回到历史

javascript - jQuery Mobile 按钮单击事件不起作用

javascript - jQuery-ZeroClipboard仅在第二次单击时有效

javascript - 如何去除折叠面板时出现的蓝色拖线

javascript - 在 Node 中导出各种功能的方法是什么?