简体   繁体   English

javascript在表中显示数组结果

[英]javascript displaying array results in a table

I have the following JavaScript loop that outputs the results of a search form to a table 我有以下JavaScript循环,可将搜索表单的结果输出到表中

l = data.length;
for (var i=0; i < l; i++) {
    var row = $('<tr><td>' + data[i].email_address + '</td><td>' 
    + data[i].number_of_orders + '</td><td>' 
    + '£' + data[i].total_order_value + '</td></tr>');
    $('#Reports').append(row);
} 

like this 像这样

显示表

I need to edit this so I have a table view like this 我需要编辑它,所以我有一个像这样的表格视图

期望的输出

with a <th> showing the domain name before the results of that domain 其中<th>在域名结果之前显示域名

I have a variable containing the domain searched for and my array contains a key value containing the domain 我有一个包含要搜索的域的变量,我的数组包含一个包含域的键值

can anyone point me in the right direction? 谁能指出我正确的方向?

my current thinking is that I need to insert the search var into my loop and check after each loop that the domain has not changed 我目前的想法是,我需要将搜索变量插入到我的循环中,并在每个循环后检查域是否未更改

any help would be great 任何帮助都会很棒

Using the data that you have, create a data structure that will help you build the table BEFORE actually building the table. 使用所拥有的数据,创建一个数据结构,该数据结构将在实际构建表之前帮助您构建表。

Suppose we have the following data 假设我们有以下数据

var data = [{
    email_address: 'someone@able.com',
    number_of_ordrs: 4,
    total_order_value: 5.56
}, {
    email_address: 'someone.else@bodge.com',
    number_of_orders: 3,
    total_order_value: 8.97
}, {
    email_address: 'another@able.com',
    number_of_orders: 6,
    total_order_value: 0.93
}, {
    email_address: 'someone@bodge.com',
    number_of_orders: 6,
    total_order_value: 0.93
}];

Let's transform it so that it looks like 让我们对其进行转换,使其看起来像

var newData: {
    'able.com': [{
        email_address: 'someone@able.com',
        number_of_orders: 4,
        total_order_value: 5.56
    }, {
        email_address: 'another@able.com',
        number_of_orders: 6,
        total_order_value: 0.93
    }],
    'bodge.com': [{
        email_address: 'someone.else@bodge.com',
        number_of_orders: 3,
        total_order_value: 8.97
    }, {
        email_address: 'someone@bodge.com',
        number_of_orders: 6,
        total_order_value: 0.93
    }]
};

We'll also need another variable domains , an array, to store and sort the domains. 我们还需要另一个变量domains (数组)来存储和排序域。 In JavaScript, the order of the properties is not guaranteed , so iterating over the object would not be a good idea. 在JavaScript中, 不能保证属性顺序 ,因此在对象上进行迭代不是一个好主意。 Instead, we'll use domains to ensure the order. 相反,我们将使用domains来确保顺序。

 $(function() { var data = [{ email_address: 'someone@able.com', number_of_orders: 4, total_order_value: 5.56 }, { email_address: 'someone.else@bodge.com', number_of_orders: 3, total_order_value: 8.97 }, { email_address: 'another@able.com', number_of_orders: 6, total_order_value: 0.93 }, { email_address: 'someone@bodge.com', number_of_orders: 6, total_order_value: 0.93 }]; var newData = {}; data.forEach(function(d) { var domain = d.email_address.split('@')[1]; // is this a new domain? if (!newData.hasOwnProperty(domain)) { newData[domain] = []; } // add entry newData[domain].push(d); }); // get and sort domains alphabetically var domains = Object.keys(newData).sort(); //console.log(domains, newData); // build table var html = '<table>'; domains.forEach(function(domain) { html += '<tr><td colspan="3">' + domain + '</td></tr>'; newData[domain].forEach(function(entry) { html += '<tr>'; html += '<td>' + entry.email_address + '</td>'; html += '<td>' + entry.number_of_orders + '</td>'; html += '<td>' + entry.total_order_value + '</td>'; html += '</tr>'; }); }); html += '</table>'; $('#Reports').html(html); }); 
 table { border: 1px solid #000; border-collapse: collapse; } td { border: 1px solid #000; padding: 5px; text-align: center; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="Reports"></div> 

You should have a variable with domain name. 您应该有一个域名变量。

Make sure all the data entered are are in order of the domain, if not you should alphabetize it by ending domain. 确保输入的所有数据均按域顺序排列,否则请按字母顺序将域结尾。

Next you would do an if statement, If the domain of this entry is equal to previous entry, else insert row with new domain. 接下来,您将执行if语句,如果该条目的域等于上一个条目,则使用新域插入行。 Then insert new entry. 然后插入新条目。

And update the domain name variable to the domain of the previous entry. 并将域名变量更新为上一个条目的域。 So it will loop through al the values, adding appropriate headers as necessary. 因此它将遍历所有值,并根据需要添加适当的标头。

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

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