简体   繁体   English

使用javascript / jquery从html表中的csv动态添加值

[英]Dynamically add values from a csv in an html table using javascript/jquery

I have a dynamically generated CSV file from another vendor that I am puling in and need to show in a table on my site. 我从另一个供应商那里动态生成了一个CSV文件,该文件正在我的邮寄中,并且需要显示在我网站的表格中。 The problem is I need to be able to manipulate the data from the CSV so it can show the corrected values in the html table. 问题是我需要能够处理CSV中的数据,以便它可以在html表中显示更正后的值。 In the end I need the HTML table to just display the Products, not the Mixed Sets. 最后,我需要HTML表仅显示产品,而不是混合集。

I am using jquery and the papaparse library to get the data and parse it in a table in html. 我正在使用jquery和papaparse库来获取数据并将其解析为html表。 My codepen is here: 我的代码笔在这里:

https://codepen.io/BIGREDBOOTS/pen/YQojww https://codepen.io/BIGREDBOOTS/pen/YQojww

The javascript pulls the initial csv values and display in a table, but I can't figure out how to to add together the values. javascript拉出初始csv值并显示在表格中,但我不知道如何将这些值加在一起。 If there is a better way of going about this, like converting the CSV to some other form of data like JSON, That is fine too. 如果有更好的解决方案,例如将CSV转换为其他形式的数据(如JSON),那也很好。

My CSV looks like this: 我的CSV如下所示:

product_title,product_sku,net_quantity
Product 1,PRD1,10
Product 2,PRD2,20
Product 3,PRD3,30
Mixed Set 1,MIX1,100
Mixed Set 2,MIX2,50
Mixed Set 3,MIX3,75

The Javascript I am using is: 我使用的Javascript是:

  function arrayToTable(tableData) { var table = $('<table></table>'); $(tableData).each(function (i, rowData) { var row = $('<tr class="rownum-' + [i] + '"></tr>'); $(rowData).each(function (j, cellData) { row.append($('<td class="' + [i] + '">'+cellData+'</td>')); }); table.append(row); }); return table; } $.ajax({ type: "GET", url: "https://cdn.shopify.com/s/files/1/0453/8489/t/26/assets/sample.csv", success: function (data) { $('body').append(arrayToTable(Papa.parse(data).data)); } }); 

My rules for the mixed set: 我对混合集的规则是:

  • Mixed Set 1 should add 100 to Product 1 and Product 2. 混合组1应将乘积1和乘积2加100。
  • Mixed Set 2 should add 50 to Product 2 and Product 3. 混合组2应将乘积2和乘积3加50。
  • Mixed Set 3 should add 75 to Product 1, Product 2 and Product 3. 混合组3应将产品1,产品2和产品3加75。

I'd like to end up with Just the products output, and the correct numbers added to the formula. 最后,我只想输出乘积,并在公式中添加正确的数字。 The end result would be a table with Product 1 = 185 , Product 2 = 245 , and Product 3 = 155 . 最终结果将是一个表,其中产品1 = 185产品2 = 245产品3 = 155

While it would be even better if the top THEAD elements were in a "th", It's fine if that is too complicated. 如果顶部的THEAD元素在“ th”中会更好,但是如果太复杂则很好。

<table>
    <tbody>
        <tr class="rownum-0">
            <td class="0">product_title</td>
            <td class="0">product_sku</td>
            <td class="0">net_quantity</td>
        </tr>
        <tr class="rownum-1">
            <td class="1">Product 1</td>
            <td class="1">PRD1</td>
            <td class="1">185</td>
        </tr>
        <tr class="rownum-2">
            <td class="2">Product 2</td>
            <td class="2">PRD2</td>
            <td class="2">245</td>
        </tr>
        <tr class="rownum-3">
            <td class="3">Product 3</td>
            <td class="3">PRD3</td>
            <td class="3">155</td>
        </tr>
    </tbody>
</table>

Without knowing the size of the dataset you're working with, I suggest you first iterate through all the CSV dataset in order to populate a list of products with the correct values, and then iterate again on that to populate your HTML table: 不知道要使用的数据集的大小,建议您首先遍历所有CSV数据集,以便使用正确的值填充产品列表,然后再次对其进行迭代以填充HTML表:

function datasetToMap(data) {
    var ret = {};
    //Initialize a map with all the product rows
    $(data).each(function(index, row) {
        if(row[0].startsWith("Product")) {
            ret[row[1]] = row; //Using the SKU as the key to the map
        }
    });

    //Apply your mixed sets rules to the elements in the ret array        
    $(data).each(function(index, row) {
        if(row[1] === "MIX1") {
            ret["PRD1"][2] += 100;
            ret["PRD2"][2] += 100;
        }
        //Do the same for Mixed sets 2 and 3
    });
    return ret;
}

function appendMapToTable(map) {
    var $table = $('#my-table');
    Object.keys(map).forEach(function(key, i) {
        var rowData = map[key];
        var row = $('<tr class="rownum-' + [i] + '"></tr>');
        $(rowData).each(function (j, cellData) {
            row.append($('<td class="' + [j] + '">'+cellData+'</td>'));
        });
        $table.append(row);
    });
}

$.ajax({
    type: "GET",
    url: "https://cdn.shopify.com/s/files/1/0453/8489/t/26/assets/sample.csv",
    success: function (data) {
        appendMapToTable(datasetToMap(Papa.parse(data).data));
    }
});

Note that this expects a table with id my-table to be already present in your HTML: you could manually parse the first row of your CSV data to add the table headings. 请注意,这希望ID为my-table已经存在于您的HTML中:您可以手动解析CSV数据的第一行以添加表标题。

Also note that if your CSV dataset is very big this is definitely not an optimal solution, since it requires iterating through all its lines twice and then iterating again through all the list built with computed values. 还要注意,如果您的CSV数据集非常大,那么这绝对不是最佳解决方案,因为它需要遍历所有行两次,然后再次遍历所有使用计算值构建的列表。

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

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