简体   繁体   English

如何将已解析的 csv 中的数据放入 select 框中?

[英]How can i put data from parsed csv to select boxes?

I used JS to parse the csv file.我用JS解析csv文件。

Example of csv: csv 示例:

Year,Model,Mileage,Color,Vin,Image
2012,BUICK LACROSSE FWD V6 FFV 4D SEDAN LEATHER,113046,BROWN,1G4GC5E34CF177752,https:imagelink

And the code of parser以及解析器的代码

$(document).ready(function(){
     $('#load_data').click(function(){
      $.ajax({
       url:"Cars.csv",
       dataType:"text",
       success:function(data)
       {
        var cars_data = data.split(/\r?\n|\r/);
        var table_data = '<table class="table table-bordered table-striped">';
        for(var count = 0; count<cars_data.length; count++)
        {
         var cell_data = cars_data[count].split(",");
         table_data += '<tr>';
         for(var cell_count=0; cell_count<cell_data.length; cell_count++)
         {
          if(count === 0)
          {
           table_data += '<th>'+cell_data[cell_count]+'</th>';
          }
          else
          {
           table_data += '<td>'+cell_data[cell_count]+'</td>';
          }
         }
         table_data += '</tr>';
        }
        table_data += '</table>';
        $('#cars_table').html(table_data);
       }
      });
     });

    });

Example of select boxes select 盒子示例

<select>
                  <option value="0">Model</option>
                  <option value="1">HERE COMES THE MODEL FROM PARSED CSV</option>
                </select>

So how can i put data from this parsed csv to select boxes on the html?那么我怎样才能把这个解析的 csv 中的数据放到 html 上的 select 框中?

Since your question wasn't clear to me, I added two solutions.由于我不清楚您的问题,因此我添加了两个解决方案。 The first solution one takes the Model column and offers a selectbox for this column.第一个解决方案采用Model列并为此列提供选择框。 You can change the column to use as a select box if you change the select_column_index (to use the Year for example, set select_column_index = 0 ).如果您更改select_column_index (例如,使用Year ,请设置select_column_index = 0 ),您可以更改该列以用作 select 框。

I created the addTable(data) function.我创建了addTable(data) function。 This is equal to your success:function(){...} , but for testing I cannot use ajax.这等于您的success:function(){...} ,但对于测试,我不能使用 ajax。 Your code is then你的代码是

$(document).ready(function(){
    $('#load_data').click(function(){
        $.ajax({
            url:"Cars.csv",
            dataType:"text",
            success:addTable,
        });
    );
});

The code takes the column with the select_column_index and uses the first cell (in the first row in the select_column_index -th column) as the label.该代码采用带有select_column_index的列,并使用第一个单元格(在select_column_index -th 列的第一行中)作为 label。 The following cells (in each row in the select_column_index -th column) are the option s for the select .以下单元格(在select_column_index -th 列的每一行中)是selectoption

Note that one should not use the label as the first option from the select box as you did in your example.请注意,不应像在示例中那样使用 label 作为 select 框中的第一个选项。 This makes it possible for users to select the mode Model which makes no sense.这使得用户可以使用 select 模式Model没有意义。 This is more complicated for users and leads to mistakes.这对用户来说更加复杂并导致错误。 Also you have to check the result on the server side which is more unnecessary code.您还必须检查服务器端的结果,这是更多不必要的代码。

You can check the output in the following snippet.您可以在以下代码段中查看 output。 The second solution is added below.下面添加第二种解决方案。

 // for demo only let csv = "Year,Model,Mileage,Color,Vin,Image\n" + "2012,BUICK LACROSSE FWD V6 FFV 4D SEDAN LEATHER,113046,BROWN,1G4GC5E34CF177752,\n" + "2013,Lincoln MKZ 3.7L AWD,113046,Lincoln,,\n"; // for demo only $(document).ready(function(){ addTable(csv); }); function addTable(data){ var cars_data = data.split(/\r?\n|\r/); var table_data = '<table class="table table-bordered table-striped">'; // use model for the select var select_column_index = 1; var label_data = "<label for='csv-select'>"; var select_data = "<select id='csv-select'>"; for(var count = 0; count<cars_data.length; count++){ var cell_data = cars_data[count].split(","); table_data += '<tr>'; for(var cell_count=0; cell_count<cell_data.length; cell_count++){ if(count === 0){ table_data += '<th>'+cell_data[cell_count]+'</th>'; } else{ table_data += '<td>'+cell_data[cell_count]+'</td>'; } if(cell_count == select_column_index){ if(count == 0){ label_data += cell_data[cell_count]; } else{ select_data += "<option value='" + cell_data[cell_count] + "'>" + cell_data[cell_count] + "</option>"; } } } table_data += '</tr>'; } table_data += '</table>'; label_data += "</label>"; select_data += "</select>"; $('#cars_table').html(table_data); $('#select-wrapper').html(label_data + " " + select_data); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="cars_table"></div> <div id="select-wrapper"></div>

The second solution takes all the columns and adds a select box for each column.第二种解决方案采用所有列并为每列添加一个 select 框。 Note that I did not add the table, you can just use your code.请注意,我没有添加表格,您可以使用您的代码。

This is basically the same code but I am saving the options to another variable.这基本上是相同的代码,但我将选项保存到另一个变量。 After all options of all columns are present, they are pasted (replace the %options% ) in the select boxes.在所有列的所有选项都存在后,它们被粘贴(替换%options% )在 select 框中。

 // for demo only let csv = "Year,Model,Mileage,Color,Vin,Image\n" + "2012,BUICK LACROSSE FWD V6 FFV 4D SEDAN LEATHER,113046,BROWN,1G4GC5E34CF177752,\n" + "2013,Lincoln MKZ 3.7L AWD,113046,Lincoln,,\n"; // for demo only $(document).ready(function(){ addSelects(csv); }); function addSelects(data){ var cars_data = data.split(/\r?\n|\r/); var select_options = []; var select_html = ""; for(var count = 0; count < cars_data.length; count++){ var cell_data = cars_data[count].split(","); for(var cell_count = 0; cell_count < cell_data.length; cell_count++){ if(count == 0){ select_html += "<div class='select-line'>" + "<label for='select-" + cell_count + "'>" + cell_data[cell_count] + "</label>" + "<select id='select-" + cell_count + "' name='" + cell_data[cell_count] + "'>" + "%options%" + "</select>" + "</div>"; } else{ if(select_options.length < cell_count){ select_options.push(""); } select_options[cell_count] += "<option value='" + cell_data[cell_count] + "'>" + cell_data[cell_count] + "</option>"; } } } for(var i = 0; i < select_options.length; i++){ select_html = select_html.replace("%options%", select_options[i]); } $('#selects').html(select_html); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="selects"></div>

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

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