简体   繁体   English

Javascript以字母顺序显示具有多列的表数据

[英]Javascript display table data in alphabetical order with multiple columns

I was wondering how to achieve a table with order like this: 我想知道如何使用这样的顺序来创建表:

在此处输入图片说明

I tried this code in my JS : 我在JS中尝试了以下代码:

var streetsPerPage = 5; 
var numNodes = parseInt(data.StreetList.length);
var noOfColumns=4;
var noOflist = numNodes / noOfColumns;
var rem = numNodes % noOfColumns;
var cellCount = 0;
var streetRowTemplate='<table width="100%"><tr><td>';
$.each(data.StreetList, function(index, streets){
    $('.streetListTemplate').removeClass('hide'); cellCount++;
    streetRowTemplate+='<div class="paddingTd" style="white-space: nowrap;"><a href="">'+streets.StreetName+'</a></div>';
    if(rem == 0) {
      if(cellCount >= noOflist) {
        streetRowTemplate+='</td>';
        streetRowTemplate+='<td>';
        cellCount = 0;
      }
    } else {
      if(cellCount == (noOflist + 1)) {
        streetRowTemplate+='</td>';
        cellCount = 0;
        streetRowTemplate+='<td>';
      }
    }
});
streetRowTemplate +='</td></tr></table>';
$('#streetListFilter').html(streetRowTemplate);

Here's the output: 这是输出:

在此处输入图片说明

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance! 提前致谢! :) :)

I needed something similar and I came across this question, so the answer is I am sure too late for the original poster. 我需要类似的内容,因此遇到了这个问题,因此答案对于原始海报来说我相信已经太迟了。 I wish they has posted a little more information about the dataset used, but from the code I managed to use something similar. 我希望他们发布了有关使用的数据集的更多信息,但是从代码中我设法使用了类似的东西。 jsfiddle link jsfiddle 链接

The issue with the code as posted is the reminder in noOflist after 所发布代码的问题是在noOflist之后的提醒

var noOflist = numNodes / noOfColumns;

The data used in the example doesn't have an even division with the desired columns and will result in a remainder which will break when handled in later the line 该示例中使用的数据与所需的列没有均匀的划分,并且会导致余数,该余数在以后的行中处理时会中断

if(cellCount == (noOflist + 1)) {

The working solution is to use 'Math.floor' to get the integer of 'noOflist'. 可行的解决方案是使用“ Math.floor”获得“ noOflist”的整数。 The complete working code is below, 完整的工作代码如下,

       var streetsPerPage = 5;
       var numNodes = parseInt(StreetList.length);
       var noOfColumns=3;
       var noOflist = numNodes / noOfColumns;
       noOflist = Math.floor(noOflist)
       var rem = numNodes % noOfColumns;
       var cellCount = 0;
       var streetRowTemplate='<table width="100%"><tr><td>';
       $.each(StreetList, function(index, streets){
         console.log(streets.StreetName);
           $('.streetListTemplate').removeClass('hide'); cellCount++;
           streetRowTemplate+='<div class="paddingTd" style="white-space: nowrap;"><a href="">'+streets.address1+'</a></div>';
           if(rem == 0) {
             if(cellCount >= noOflist) {
               streetRowTemplate+='</td>';
               streetRowTemplate+='<td>';
               cellCount = 0;
             }
           } else {
             if(cellCount == (noOflist + 1)) {
               streetRowTemplate+='</td>';
               cellCount = 0;
               streetRowTemplate+='<td>';
             }
           }
       });
       streetRowTemplate +='</td></tr></table>';
       $('#streetListFilter').html(streetRowTemplate);

This is an example of the dataset I used from github 这是我从github使用的数据集的示例

       var StreetList = [

            {
                "address1": "1745 T Street Southeast",
                "address2": "",
                "city": "Washington",
                "state": "DC",
                "postalCode": "20020",
                "coordinates": {
                    "lat": 38.867033,
                    "lng": -76.979235
                }
            },
            {
                "address1": "6007 Applegate Lane",
                "address2": "",
                "city": "Louisville",
                "state": "KY",
                "postalCode": "40219",
                "coordinates": {
                    "lat": 38.1343013,
                    "lng": -85.6498512
                }
            },


     ]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM