简体   繁体   English

基于表格单元格纯Javascript隐藏表格行

[英]Hide table row based on table cell pure Javascript

I have an form which add the input to an HTML table.我有一个将输入添加到 HTML 表的表单。 I want to do a dropdown where the user can filter for specific table cell elements only in pure Javascript and a filter method.我想做一个下拉列表,用户只能在纯 Javascript 和过滤方法中过滤特定的表格单元格元素。

Let's say I have an table like this:假设我有一张这样的桌子:

Name姓名 Age年龄 ID ID
Anna安娜 14 14 3 3
Herb草本植物 34 34 4 4
John约翰 14 14 6 6

And a dropdown like this:和这样的下拉列表:

Select Name: Select 名称:
Anna安娜
Herb草本植物
John约翰

In the case the user selects Anna only the following table should showed:在用户选择 Anna 的情况下,仅应显示下表:

Name姓名 Age年龄 ID ID
Anna安娜 14 14 3 3

The tricky part is that every row is created through the input from an form what means that I can't talk to an specific table row per id or class.棘手的部分是每一行都是通过来自表单的输入创建的,这意味着我无法与每个 id 或 class 的特定表行交谈。

Here is what I tried:这是我尝试过的:

For the Javascript part I tried to get all the elements from the table cell which should contain the value from the select button and if this element is.= the value from the select button I hide the table row.对于 Javascript 部分,我尝试从表格单元格中获取所有元素,这些元素应该包含来自 select 按钮的值,如果这个元素是。=来自 select 按钮的值。 I don't know how to get the table row through a table cell element.我不知道如何通过表格单元格元素获取表格行。

 function changeDisplayTable() { //here i get all the values from the table cell departments in an array as text var dataCellsDepartments = document.getElementsByClassName(" dep"); //if does not work try " dep" var listWithTextContentDepartments = []; var i = 0; len = dataCellsDepartments.length; while (i < len) { listWithTextContentDepartments.push(dataCellsDepartments[i].textContent) i++ } console.log(listWithTextContentDepartments); //array filtern und dann durch jede row gehen und checken if row contains one of the elem from array const result = listWithTextContentDepartments.filter(checkDepartment); function checkDepartment(department) { return department } //wenn selected elem != elem from table row --> hide table row }
 <table id="table" class="tableZebra" border="1"> <tr> <th>Student_Id</th> <th>First_Name</th> <th>Last_Name</th> <th>DOB</th> <th>Gender</th> <th>Department</th> <th>Email_Id</th> <th>Joining Date</th> </tr> </table> <form> <p>*Staff_Id:</p> <div><input type="text" id="Staff_Id" placeholder="Staff_Id"></div> <p>*First_Name:</p> <div><input type="text" id="First_Name_staff" placeholder="First_Name"></div> <p>Last_Name:</p> <div><input type="text" id="Last_Name_staff" placeholder="Last_Name"></div> <p>DOB:</p> <div><input type="text" id="DOB_staff" placeholder="DOB"></div> <p>Gender:</p> <div><input type="radio" id="GenderFemale_staff" placeholder="Gender" name="test"></div> <label for="html">Female</label> <div><input type="radio" id="GenderMale_staff" placeholder="Gender" name="test"></div> <label for="html">Male</label> <p>*Email_Id:</p> <div><input type="text" id="Email_Id_staff" placeholder="Email_Id"></div> <div class="distance-submit"><input class="submit" type="submit" value="Submit" onclick="onClickCheckFormStaff()"></div> <div class="error-staff" id="error-staff">*Fill out all mandatory fields</div> </form> <p>*Department:</p> <select name="departments" id="Departments"> <option value="empty">------</option> <option value="Department_1">Department_1</option> <option value="Department_2">Department_2</option> <option value="Department_3">Department_3</option> <option value="Department_4">Department_4</option> </select>

You can get the table row from a cell using closest() :您可以使用closest()从单元格中获取表格行:

var tableRow = tableCell.closest("tr");

Note that this method is not supported on internet explorer.请注意,Internet Explorer 不支持此方法。

From what I can see, you want to filter based on department and you're trying to display the data based on the selected department.据我所知,您希望根据部门进行过滤,并且您正在尝试根据所选部门显示数据。

Here what you need to do is keep your data stored in a variable and render the table based on another variable which changes when the filter information is selected.在这里,您需要做的是将数据存储在一个变量中,并根据另一个变量呈现表格,该变量在选择过滤器信息时会发生变化。

For example:-例如:-

var data = [
   {Student_Id : 123
    First_Name: John
    Last_Name: doe
    ...
    Department: ABC
    ...}
    {Student_Id :3456
    First_Name:Jane 
    Last_Name: Doe
    .....
    Department:DEF
    ....}
]

Use another variable for populating the table data say "studentTableData"使用另一个变量来填充表格数据,比如“studentTableData”

Now in default condition when no filter is applied do现在在没有应用过滤器时处于默认状态

var studentTableData = data

When a filter is selected instead of hiding the table rows,filter the data of "studentTableData" based on the selected department and render the table again(in simpler words replace the table) When a filter is selected instead of hiding the table rows,filter the data of "studentTableData" based on the selected department and render the table again(in simpler words replace the table)

var department_selected = "ABC"

var studentTableData = data.filter(function(el){
return el.Department == department_selected 
})
//var studentTableData will now have an array of data where department name matches the selected name

more simple with .cells[ x ] row property使用.cells[ x ]行属性更简单

sample code:示例代码:

 const data = [ { name: 'Anna', age:14, id: '6' }, { name: 'Herb', age:34, id: '4' }, { name: 'John', age:14, id: '6' } ], theSelect = document.querySelector('#my-select'), tableBody = document.querySelector('#my-table tbody'), myForm = document.querySelector('#my-form'); theSelect.onchange = e => // ********** here it is. ********** { if (theSelect.value==='*') tableBody.querySelectorAll('tr').forEach(tr=>tr.className = '') else tableBody.querySelectorAll('tr').forEach(tr=> { tr.classList,toggle('noDisplay'. tr.cells[0].textContent.==theSelect.value ) }) } // just adding code for testing... function addRow(obj) { let nRow = tableBody.insertRow() nRow.insertCell().textContent = obj.name nRow.insertCell().textContent = obj.age nRow.insertCell().textContent = obj.id theSelect,add( new Option(obj.name.obj.name)) } data.forEach(addRow) myForm.onsubmit = e => { addRow( Object.fromEntries(new FormData(myForm).entries())) e.preventDefault() myForm.reset() theSelect.selectedIndex = 0 tableBody.querySelectorAll('tr').forEach(tr=>tr.className='') }
 html { font-family: Arial, Helvetica, sans-serif; font-size: 14px; } table { border-collapse: collapse; margin: 2em 1em; } td { padding: .2em.8em; border: 1px solid darkblue; } thead { background-color: #9bbad8; } form { padding: 0 3em; } label { line-height: 2em; } input { display: block; margin-bottom: .6em; } button { margin-top: 1em; width: 5em; }.noDisplay { /* display or not display, this is your question...*/ display: none; }
 <select id="my-select"> <option value="" disabled selected> Please pick one...</option> <option value="*"> All </option> </select> <table id="my-table"> <thead> <tr><td>name</td><td>age</td><td>id</td></tr> </thead> <tbody></tbody> </table> <form id="my-form"> <label > name <input type="text" name="name" value="" required> </label> <label > age <input type="number" name="age" value="" min="1" required> </label> <label > id <input type="text" name="id" value="" required> </label> <button type="submit">submit</button> <button type="reset">reset</button> </form>

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

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