简体   繁体   English

使用Javascript / jQuery,将列表呈现到表格中的最优雅的方法是什么?

[英]Using Javascript / jQuery, what is the most elegant way to render a list into a table?

I have an issue where I have an array of strings (that are image urls) coming back from an ajax callback and I need to render the images into a html table. 我有一个问题,我有一个从ajax回调返回的字符串数组(即图像url),我需要将图像渲染到html表中。 The issue is that I don't know the length of the list of course but I need to render a table with 6 columns always. 问题是我当然不知道列表的长度,但是我需要始终绘制一个包含6列的表。 Is it better to build up the table ( and then fill in the cells or build up the table on the fly (loop through number of columns within each .each statements below). The second one seems more dynamic but not very elegant. 最好建立表(然后填写单元格或动态建立表(遍历下面每个.each语句中的列数)。第二个表看起来更动态但不太优雅。

var tableHTML = [];
tableHTML.push("<table>");

$.each(data.ImageURLs, function(index, imageURL) {

    // create table content
    if (index % 1 == 0) {
        tableHTML.push("<tr>");
    }
    tableHTML.push("<td>" + imageURL + "</td>");

    if (index % 6 == 0) {
        tableHTML.push("</tr");
    }
});

tableHTML.push("</table>");
$("#myTable").html(tableHTML.join(""));

What I normally do is create a helper function to pad the additional elements to last row if needed. 我通常要做的是创建一个辅助函数,以在需要时将其他元素填充到最后一行。

You can get the number needed by comparing number of items to modulus of items per row. 您可以通过将项目数量与每行项目的模数进行比较来获得所需的数量。 I use slice to create each row of data 我使用slice来创建数据的每一行

var items_per_row = 6;

var tableHTML = ['<table border="1">'];
var num_items = data.length;



for (j = 0; j < num_items; j = j + items_per_row) {
    var rowEnd = j + items_per_row;
    var rowData = data.slice(j, rowEnd);

    tableHTML.push('<tr>');
    $.each(rowData, function (idx, item) {
        tableHTML.push('<td>' + item + '</td>')
    });
    /* pad last row if needed */
    if (rowEnd > num_items) {
        tableHTML.push(emptyCells(num_items, items_per_row));
    }
    tableHTML.push('</tr>');

}


tableHTML.push("</table>");

function emptyCells(num_items, items_per_row) {
    var qty = items_per_row - (num_items % items_per_row);

    var cells = [];
    for (i = 0; i < qty; i++) {
        cells.push('<td class="empty">Empty</td>')
    }

    return cells.join('')
}

DEMO: http://jsfiddle.net/VTLBF/2 演示: http : //jsfiddle.net/VTLBF/2

Can you try this? 你可以试试这个吗?

DEMO 演示

https://code.google.com/p/html5shiv/ https://code.google.com/p/html5shiv/

and this, using only <p> s 而且,仅使用<p>

@media only screen and (min-width: 740px) {
  #multicolumn { 
    column-count:6; 
    column-gap: 10px; 

    -webkit-column-count: 6; 
    -webkit-column-gap: 10px;

    -moz-column-count: 6;
    -moz-column-gap: 10px;
  }      
  p:first-of-type {
    margin-top:0 !important;
  }
}

I think a good way(maybe be the best) is to use templating plugin like jquery tmpl . 我认为一个好方法(也许是最好的)是使用像jquery tmpl这样的模板插件。

With using it you can draw an entire table or anything you want just by giving it an array or you can pass to is JSON with your data coming from server. 使用它,您可以通过给它一个数组来绘制整个表或任何您想要的东西,或者您可以使用来自服务器的数据传递给JSON。

Kindly check these questions : 请检查以下问题:

Can jQuery template bind to a raw array? jQuery模板可以绑定到原始数​​组吗?

How do I render a jQuery.tmpl Template to a String? 如何将jQuery.tmpl模板呈现为字符串?

and don't forget to check the jquery tmpl documentation for more information. 并且不要忘记查看jquery tmpl文档以了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Javascript arrays 计算集差的最快或最优雅的方法是什么? - What is the fastest or most elegant way to compute a set difference using Javascript arrays? 使用带有 JavaScript 的递归 function 显示键值对的最优雅方式是什么? - What is the most elegant way to display key value pairs using a recursive function with JavaScript? jQuery-存储javascript使用的数据的最优雅的方式 - JQuery - Most elegant way for storing data to be used by javascript 在 JavaScript 构造函数中接受和处理不同选项的最优雅的方式是什么? - What is the most elegant way to accept and handle different options in JavaScript constructor? 在JavaScript中部分复制对象数组的最优雅方法是什么 - What is the most elegant way of partly copying object arrays in JavaScript 什么是javascript中将字节转换为可读格式的最优雅方式 - What is the most elegant way in javascript to convert bytes into a readable format Javascript使用加载gif的最简单/最优雅的方式 - Javascript Easiest/Most Elegant way of using a loading gif 插入此条件语句的最优雅方式是什么? - What is the most elegant way to insert this conditional statement? 创建 javascript 弹出窗口的最优雅方式? - most elegant way to create a javascript popup? 使用jQuery过滤表行的更优雅的方法吗? - More elegant way to filter out table rows using jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM