简体   繁体   English

如何将相同的选项附加到多个选择下拉框?

[英]How to append the same options to multiple select drop down boxes?

I am trying to add the same options to each drop down box listed but the options are only being added to the first drop down. 我正在尝试将相同的选项添加到列出的每个下拉框中,但这些选项仅被添加到第一个下拉列表中。

function loadIgorMap() {
    $.getJSON('./iGorMap.json', function (data) {
        theData = data;
        for (var i = 0; i < data.length; i++) {
            $('.tableBody').append(`<tr>
                <td>${data[i].FiduciaryOutsourcingField}</td>
                <td>
                    <select class="browser-default custom-select" id="dropdown">
                        <option value="NA" class="N/A" hidden> -- Select --</option>
                    </select>
                </td>`)

        }
    })
}

window.onload = function () {


            for (let j = 0; j < theData.length; j++) {

                console.log('I am here')
                console.log(theData)

                let option;
                for (let i = 0; i < jsonKeys.length; i++) {
                    option = document.createElement('option');
                    option.text = jsonKeys[i];
                    // console.log(jsonKeys[i])
                    // option.value = jsonKeys[i].abbreviation;
                    dropdown.add(option);




                }
            }
        };

This should display the same options for all select boxes on the page. 这将为页面上的所有选择框显示相同的选项。 Here is a link to a live page on repl.it https://repl.it/@MattEubanks/Vanilla-Mapping-Tool . 这是repl.it https://repl.it/@MattEubanks/Vanilla-Mapping-Tool上的实时页面的链接。 I added a CSV that you should be able to save in an excel file and then load into the system. 我添加了一个CSV文件,您应该可以将其保存在excel文件中,然后加载到系统中。

It took me a moment to understand what you're looking to do. 我花了一点时间来了解您要做什么。 It seems you plan to import a CSV file and read the Header line. 看来您打算导入CSV文件并阅读标题行。 You then want to be able to Map the Column Headers in your App. 然后,您希望能够在应用程序中映射列标题。

Consider the following code. 考虑下面的代码。 I populated variables, assuming all the data has been imported from the CSV and JSON already/ 我已填充变量,假设所有数据已从CSV和JSON中导入/

 $(function() { // Field Data read in from CSV var fields = ["First Name", "Middle Name", "Last Name", "Suffix", "Address 1", "Address 2", "City", "State", "Zip Code", , , , , , , , , , , , ]; // JSON Data for selections var myData = [{ "FiduciaryOutsourcingField": "EIN", "YourField": "" }, { "FiduciaryOutsourcingField": "Location", "YourField": "" }, { "FiduciaryOutsourcingField": "TaxId", "YourField": "" }, { "FiduciaryOutsourcingField": "FirstName", "YourField": "First Name" }, { "FiduciaryOutsourcingField": "MiddleName", "YourField": "Middle Name" }, { "FiduciaryOutsourcingField": "LastName", "YourField": "Last Name" } ]; function readData(dObj, tObj) { $.each(dObj, function(key, row) { var newRow = $("<tr>").appendTo(tObj); var f1 = $("<td>").html(row.FiduciaryOutsourcingField).appendTo(newRow); var f2 = $("<td>").appendTo(newRow); var s1 = $("<select>").appendTo(f2); $("<option>").html("-- Select --").appendTo(s1); $.each(fields, function(i, v) { $("<option>", { selected: (row.YourField == v ? true : false), "data-col-it": i }).html((v != undefined ? v : "(Col-" + i + ")")).appendTo(s1); }); }); } readData(myData, $(".tableBody")); }); 
 .tableHeader { width: 300px; } 
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container col-md-"> <div> <table class="table table-bordered table-striped"> <thead class="thead-dark"> <tr> <th scope="col">Fiduciary Outsourcing Field</th> <th scope="col">Your Fields</th> </tr> </thead> <tbody class="tableBody"></tbody> </table> </div> </div> 

As you can see, I iterate over each row of JSON data and make the Table Row from it. 如您所见,我遍历了JSON数据的每一行,并从中制作了Table Row。 Since we want fields for each Select, we iterate over our field data each pass, creating new <option> elements. 由于我们需要每个Select的字段,因此我们每次遍历字段数据,创建新的<option>元素。 We can also set the selected property of each if there is a match in the JSON data already. 如果JSON数据中已经存在匹配项,我们还可以设置每个属性的selected属性。

I included a data-col-id in case you need to use that index versus the column name for mapping. 我包括一个data-col-id ,以防您需要使用该索引和列名进行映射。

Hope this helps. 希望这可以帮助。

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

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