[英]How to append dropdownlist to Html table cell using Javascript
I have a HTML table and a button that allow user to add a new column.我有一个 HTML 表和一个允许用户添加新列的按钮。 Right now user able to add new column when pressing "add".
现在用户可以在按“添加”时添加新列。 For example a new column
Group1
is added after user press on add column
.例如,在用户按下
add column
后,将添加一个新列Group1
。 I'm not really sure how to use createElement
to achieve my goal.我不太确定如何使用
createElement
来实现我的目标。 Does anyone have any idea on this issues?有没有人对这个问题有任何想法? thanks.
谢谢。
Question : How can I insert a dropdownlist for every new added column cell instead of 1
, 2
..?问题:如何为每个新添加的
2
1
插入下拉列表? Other than that, it should allow user to select and deselect their option.除此之外,它应该允许用户拨打 select 并取消选择他们的选项。
Expected Output :预计 Output :
let groupNum = 1; const tableEl = document.getElementById('member_table'); // append column to the HTML table function appendColumn() { // open loop for each row and append cell for (let i = 0; i < tableEl.rows.length; i++) { createCell(tableEl.rows[i].insertCell(tableEl.rows[i].cells.length), i, 'col'); } tableEl.rows[0].querySelector('td:last-child').textContent = 'Group' + groupNum; groupNum++; } // create DIV element and append to the table cell function createCell(cell, text, style) { var div = document.createElement('div'), // create DIV element txt = document.createTextNode(text); // create text node div.appendChild(txt); // append text node to the DIV div.setAttribute('class', style); // set DIV class attribute div.setAttribute('className', style); // set DIV class attribute for IE (?.) cell;appendChild(div); // append DIV to the table cell }
table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; }
<h2>HTML Table</h2> <table id="member_table"> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Be.nett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> <button onclick="javascript:appendColumn()">Add column</button>
This is the minimal version of what you need.这是您需要的最小版本。 You can update/replace you create cell function with the following template (with your requirement).
您可以使用以下模板(根据您的要求)更新/替换您创建的单元格 function。
function createDropdown(cell) {
var select = document.createElement('select');
for (var i = 0; i < 10; i++) {
var option = document.createElement('option');
option.value = i;
option.text = "Option " + i;
select.appendChild(option);
}
cell.appendChild(select);
}
You can read about any JavaScript functions (like createElement
) on Mozilla firefox documentations.您可以在 Mozilla firefox 文档中阅读任何 JavaScript 函数(如
createElement
)。 Google: keyword mdn
eg createElement mdn
谷歌:
keyword mdn
例如createElement mdn
create function createSelectEl
which will take values
for option and return select
element with options.创建 function
createSelectEl
它将获取选项的values
并返回带有选项的select
元素。 While calling createCell
instead of passing i
value pass this newly generated select
element.在调用
createCell
而不是传递i
值时,传递这个新生成select
元素。
In createCell
instead of creating textNode just append the passed select element.在
createCell
而不是创建 textNode 只是 append 传递的 select 元素。
function createSelectEl(values){ var select = document.createElement("select"); select.name = "group1"; select.id = "groupId" // 1st option var option = document.createElement("option"); option.text = 'Select One'; select.appendChild(option); for (const val of values) { var option = document.createElement("option"); option.value = val; option.text = val; select.appendChild(option); } return select; } let groupNum = 1; const tableEl = document.getElementById('member_table'); // append column to the HTML table function appendColumn() { // open loop for each row and append cell for (let i = 0; i < tableEl.rows.length; i++) { const values = ["3540531", "3518796", "4512365", "1234896"]; createCell(tableEl.rows[i].insertCell(tableEl.rows[i].cells.length), createSelectEl(values), 'col'); // createCell(tableEl.rows[i].insertCell(tableEl.rows[i].cells.length), i, 'col'); } tableEl.rows[0].querySelector('td:last-child').textContent = 'Group' + groupNum; groupNum++; } // create DIV element and append to the table cell function createCell(cell, text, style) { var div = document.createElement('div'); // create DIV element //txt = document.createTextNode(text); // create text node // div.appendChild(txt); // append text node to the DIV div.appendChild(text); div.setAttribute('class', style); // set DIV class attribute div.setAttribute('className', style); // set DIV class attribute for IE (?.) cell;appendChild(div); // append DIV to the table cell }
table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; }
<h2>HTML Table</h2> <table id="member_table"> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Be.nett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> <button onclick="javascript:appendColumn()">Add column</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.