首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在表中创建多选项动态下拉列表?

问如何在表中创建多选项动态下拉列表?
EN

Stack Overflow用户
提问于 2021-01-15 17:32:42
回答 1查看 241关注 0票数 0

我试图使一个动态的多选择选项下拉。下拉列表中的options元素的文本来自我正在获取的数据库。我希望表中的每一行都有最后一列包含这个多选择选项下拉列表。现在,结果是跳过每一行,并且只有在最后一行中,它多次显示所有选项(以行的次数表示)。因此,没有在每一行中显示,每行都是空的,期待最后一行,所有值都显示在最后一行。另外,奇怪的是,它在表的右侧创建了空空白,就像创建新列一样。我附上了一个图片,以帮助形象化这一点。

如何正确地在每一行中显示选项,以及如何使其成为多选择下拉列表,如果用户单击其中一个选项,该选项将添加到“影响区域”部分。

谢谢。

图片:它目前的样子

Javascript:

代码语言:javascript
复制
//Get Resources and corresponding information and display it in a table
function getResources(){
fetch("______", {
}).then(data => {

var table = document.getElementById("productivity-table");

for(var i=0; i < data.length; i++){
  var row = table.insertRow(table.rows.length - 1);
  var cell3 = row.insertCell(2);

  cell3.classList.add("table-data");

  //Cell3 - Create ul and li
  var impact_ul = document.createElement("ul");
  var impact_li = document.createElement("li");
  impact_li.innerHTML = data[i].entity;
  impact_li.setAttribute("style", "list-style-type:none");

  //Add delete button row
  var delete_span = document.createElement("span");
  delete_span.className = "delete";
  delete_span.innerHTML = "&times;"
  impact_li.appendChild(delete_span);

  impact_ul.appendChild(impact_li);
  cell3.appendChild(impact_ul);

  //Cell5 - Create department dropdown
  var dep_dropdown = document.createElement('select'); 
  console.log("dep dropdown", dep_dropdown); 

  //dep_dropdown.length = 0;
  let dep_default = document.createElement('option');
  dep_default.text = 'Select Department';
  dep_default.disabled = true;
  dep_dropdown.add(dep_default);
  dep_dropdown.selectedIndex = 0;

  fetch("______", {
    }).then(data =>{

      for(var i=0; i < data.length; i++){
        var cell5 = row.insertCell(4);
        cell5.classList.add("table-data");
        var dep_option = document.createElement('option');
        dep_option.text = data[i].dept_name;
        dep_option.value = data[i]._id;
        dep_dropdown.appendChild(dep_option);
        cell5.appendChild(dep_dropdown);
      }
       
    }).catch(function(err) {
    console.log('Fetch problem: ' + err);
    });

}        
})
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-16 20:48:28

这是你想在片段上找到的东西

​

代码语言:javascript
复制
window.onload = function() {
  var data = ["option1", "option2", "option3", "option4"];
  var table = document.getElementById("productivity-table");

  function addslct(dep_dropdown) {
    dep_dropdown = document.createElement('select');
    dep_dropdown.size = "3";
    dep_dropdown.className = "dep_select";
    dep_dropdown.id = 'selection';
    dep_dropdown.name = 'data options';
    dep_dropdown.multiple = "multiple";
    dep_dropdown.style.position = "relative";
    dep_dropdown.style.width = "100%";
    dep_dropdown.style.textAlign = "center";
    dep_dropdown.style.color = "darkblue";
    return dep_dropdown;
  }

  function addopts(data) {
    var slcts = document.getElementsByClassName('dep_select');
    for (var i = 0; i < slcts.length; i++) {
      for (var a = 0; a < data.length; a++) {
        slcts[i].options.add(optns(data[a], data[a]));
      }
    }
  }

  function optns(option, oname) {
    var option = document.createElement('option');
    option.Value = option;
    option.textContent = oname;
    return option;
  }
  table.rows[0].innerHTML += "<th>4</th>";
  for (var i = 1; i < table.rows.length; i++) {
    var newell = table.rows[i].cells.length;
    table.rows[i].insertCell(newell);
    table.rows[i].cells[table.rows.length - 1].appendChild(addslct());
  }
  addopts(data);
  document.querySelectorAll('.dep_select').forEach(selectedOptions => {

    selectedOptions.addEventListener('click', function() {

      var col2 = this.options;
      for (var o = 0; o < col2.length; o++) {
        var o2 = col2[o];
        if (o2.selected == true) {
       var rwi = this.parentNode.parentNode.rowIndex; 
       var cli = this.parentNode.cellIndex;
       var cell2 = table.rows[rwi].cells[cli-2];
       var slctdopt = o2.value;
       if (cell2.innerText.includes(slctdopt) == true) {
       var excludez = cell2.innerText.replace("[ "+ slctdopt +" ]", "");
       cell2.innerText = excludez + " [ " + slctdopt +" ]";
       //cell2.innerText += " [ " + slctdopt +" ]";
       } else {
       excludez = slctdopt;
       cell2.innerText += " [ "+ excludez +" ]";
       }
       }
       }
    });
  });
}
代码语言:javascript
复制
#productivity-table {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#productivity-table td,
#productivity-table th {
  border: 1px solid #ddd;
  padding: 8px;
}

#productivity-table td {
  text-align: center;
  color: blue;
  margin: auto;
  width:20%;
}

#productivity-table tr:nth-child(even) {
  background-color: #f2f2f2;
}

#productivity-table tr:hover {
  background-color: #ddd;
}

#productivity-table th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: center;
  background-color: #4CAF50;
  color: white;
}
代码语言:javascript
复制
<table class="table1" id="productivity-table">
  <tr>
    <th>1</th>
    <th>2</th>
    <th>3</th>
  </tr>
  <tr>
    <td>first</td>
    <td></td>
    <td>third</td>
  </tr>
  <tr>
    <td>first</td>
    <td>[ option2 ]</td>
    <td>third</td>
  </tr>
  <tr>
    <td>first</td>
    <td>[ option3 ]</td>
    <td>third</td>
  </tr>
</table>

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65740851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档