简体   繁体   English

获取每个选定行的表格的单元格值?

[英]Get a cell value of table for each selected row?

I'm trying to get a single value of cell per row on a table when a checkbox of this row is selected and get all values of all rows when the "Select all" checkbox is selected too. 我试图在选中该行的复选框时获取表上每行单元格的单个值,并且也选中了“全选”复选框时获取所有行的所有值。

I try to do this: A table with sub-table inside of each rows. 我尝试执行以下操作:在每行的内部都有一个子表。 So, I try to when the checkbox "Select All" y clicked, all the checkbox inside of sub-table the row that i clicked be marked and get the values of each rows. 因此,我尝试在单击“全选”复选框时,标记我单击的行的子表内的所有复选框,并获取每行的值。

All this in Javascript or TypeScript 所有这些都使用JavascriptTypeScript

this is my html: 这是我的HTML:

<div class="table-responsive row col-md-12" style="margin-top: 3.5%; margin-bottom:1%;  border-bottom: solid; border-bottom-width: 1px;">
<table class="table table-hover tablaReclamaciones"  > 
  <thead>
    <tr>
      <th></th>
      <th>No.</th>
      <th>Alias</th>
      <th>Exp. Date</th>
      <th>Past Deu</th>
      <th>Current Balance</th>
      <th>Mount to pay</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <label class="containerCheck"> Select All
          <input type="checkbox" id="selectAll">
        </label>
      </td>
      <td>001</td>
      <td>My Home</td>
      <td>10/12/2018</td>
      <td>2500</td>
      <td>5000</td>
      <td>7500</td>
    </tr>

    <tr>
      <td colspan="7">
        <div class=" row col-md-12" style="background: rgb(243, 243, 243); margin-left: 0.15%;">
          <div class="row col-md-12 " style="color: rgb(253, 120, 43);">
            <h5 class="pull-left" >Details</h5> 
            <h5 class="pull-right arrowDown"  id="2222" data-toggle="collapse" [attr.data-target]="'#' + r.nocontracto"  ><i class="test fa fa-chevron-down "></i></h5>
          </div>

          <div id={{ r.nocontracto }} class="table-responsive row col-md-12  "  style=" margin-top: 1%;" >
            <table class="table table-hover" style="background: rgb(243, 243, 243)">
                <thead>
                  <th></th>
                  <th>No. Reference</th>
                  <th>Invoice Date</th>
                  <th>Exp Date</th>
                  <th>Balance</th>
                  <th></th>
                </thead>
                <tbody>
                  <tr *ngFor="let b of r.billing" id="billDetails">
                    <td>
                      <input type="checkbox" class="checkInside" name="invoiceCheck" onclick="singleRow()">
                    </td>
                    <td>123456</td>
                    <td>22/12/2018</td>
                    <td>12/01/2018</td>
                    <td>5250.00</td>
                    <td>
                      RD$ <input type="text" id="amountInput">
                    </td>
                    <td></td>
                  </tr>
                  <tr *ngFor="let b of r.billing" id="billDetails">
                    <td>
                      <input type="checkbox" class="checkInside" name="invoiceCheck" onclick="singleRow()">
                    </td>
                    <td>123456</td>
                    <td>22/01/2018</td>
                    <td>12/02/2018</td>
                    <td>400.00</td>
                    <td>
                      RD$ <input type="text" id="amountInput">
                    </td>
                    <td></td>
                  </tr>
                  <tr *ngFor="let b of r.billing" id="billDetails">
                    <td>
                      <input type="checkbox" class="checkInside" name="invoiceCheck" onclick="singleRow()">
                    </td>
                    <td>123456</td>
                    <td>22/02/2018</td>
                    <td>12/03/2018</td>
                    <td>2600.00</td>
                    <td>
                      RD$ <input type="text" id="amountInput">
                    </td>
                    <td></td>
                  </tr>
                </tbody>
            </table>
          </div>
        </div>
      </td>
    </tr> 
  </tbody>
</table>

Total: 总:

Javacript code (For get the value of a single row, but don't work): Javacript代码(用于获取单行的值,但不起作用):

function singleRow() {
let row = document.getElementById('billDetails');
let cells = row.getElementsByTagName('td');

let inputVal = document.getElementById('amountInput').value;

if(!inputVal) {

} else {
    console.log('Columna de Input: ' + inputVal);
}
}

You can try the following way in vanilla JavaScript: 您可以在原始JavaScript中尝试以下方式:

 document.querySelectorAll('input[type=checkbox]').forEach(function(el){ el.addEventListener('change', function(){ if(this.id == 'selectAll'){ var trList = document.querySelectorAll('tbody tbody tr'); console.clear(); //clear the console trList.forEach(function(tr){ tr.querySelectorAll('td').forEach(function(td){ console.log(td.textContent.trim()) }); }); } else{ console.clear(); //clear the console var tdList = this.parentNode.parentNode.childNodes; tdList.forEach(function(td){ console.log(td.textContent.trim()) }); } }); }); 
 <div class="table-responsive row col-md-12" style="margin-top: 3.5%; margin-bottom:1%; border-bottom: solid; border-bottom-width: 1px;"> <table class="table table-hover tablaReclamaciones" > <thead> <tr> <th></th> <th>No.</th> <th>Alias</th> <th>Exp. Date</th> <th>Past Deu</th> <th>Current Balance</th> <th>Mount to pay</th> </tr> </thead> <tbody> <tr> <td> <label class="containerCheck"> Select All <input type="checkbox" id="selectAll"> </label> </td> <td>001</td> <td>My Home</td> <td>10/12/2018</td> <td>2500</td> <td>5000</td> <td>7500</td> </tr> <tr> <td colspan="7"> <div class=" row col-md-12" style="background: rgb(243, 243, 243); margin-left: 0.15%;"> <div class="row col-md-12 " style="color: rgb(253, 120, 43);"> <h5 class="pull-left" >Details</h5> <h5 class="pull-right arrowDown" id="2222" data-toggle="collapse" [attr.data-target]="'#' + r.nocontracto" ><i class="test fa fa-chevron-down "></i></h5> </div> <div id={{ r.nocontracto }} class="table-responsive row col-md-12 " style=" margin-top: 1%;" > <table class="table table-hover" style="background: rgb(243, 243, 243)"> <thead> <th></th> <th>No. Reference</th> <th>Invoice Date</th> <th>Exp Date</th> <th>Balance</th> <th></th> </thead> <tbody> <tr *ngFor="let b of r.billing" id="billDetails"> <td> <input type="checkbox" class="checkInside" name="invoiceCheck" onclick=""> </td> <td>123456</td> <td>22/12/2018</td> <td>12/01/2018</td> <td>5250.00</td> <td> RD$ <input type="text" id="amountInput"> </td> <td></td> </tr> <tr *ngFor="let b of r.billing" id="billDetails"> <td> <input type="checkbox" class="checkInside" name="invoiceCheck" onclick=""> </td> <td>123456</td> <td>22/01/2018</td> <td>12/02/2018</td> <td>400.00</td> <td> RD$ <input type="text" id="amountInput"> </td> <td></td> </tr> <tr *ngFor="let b of r.billing" id="billDetails"> <td> <input type="checkbox" class="checkInside" name="invoiceCheck" onclick=""> </td> <td>123456</td> <td>22/02/2018</td> <td>12/03/2018</td> <td>2600.00</td> <td> RD$ <input type="text" id="amountInput"> </td> <td></td> </tr> </tbody> </table> </div> </div> </td> </tr> </tbody> </table> 

A sample solution in javascript: javascript中的示例解决方案:

 <label>A small sample code</label> <br> <input type="checkbox" name="all" onchange="allChecked(this)"/> <label>check all</label> <br> <input type="checkbox" name="child" onchange="showVal(this)"/> <label>Strongly disagree</label> <input type="checkbox" name="child" onchange="showVal(this)"/> <label>Somewhat agree</label> <input type="checkbox" name="child" onchange="showVal(this)"/> <label>Strongly agree</label> <script> function showVal(chk) { if(chk.checked){ alert(chk.checked); // traverse the dom elements and get values of the cell you need } }; function allChecked(chk){ var childChks = document.getElementsByName("child"); for(var i =0 ;i < childChks.length; i++){ childChks[i].checked =chk.checked; } } </script> 

Here's an ES6 version (useful for destructuring the event targets efficiently). 这是ES6版本(用于有效地破坏事件目标)。 Instead of attaching change listeners to each checkbox element it uses "event propagation" to capture events as they bubble up the DOM. 它没有将更改侦听器附加到每个复选框元素,而是使用“事件传播”来捕获事件,这些事件使DOM冒泡。

Note: that I've reduced your HTML example down to the core parts, and updated your ids to classes (ids have to be unique, classes do not). 注意:我已经将您的HTML示例简化为核心部分,并将您的ID更新为类(ID必须是唯一的,类不是唯一的)。

 // Grab the selectAll checkbox and add an eventListener to it const allCheckbox = document.getElementById('selectAll'); allCheckbox.addEventListener('change', selectAll, false); // Grab the amountInput elements const amountInputs = document.querySelectorAll('.amountInput'); // Grab the "inner" table and add an event listener to it // The function attached to this listener will capture events as they // bubble up from the checkboxes const inner = document.querySelector('.inner'); inner.addEventListener('change', selectSingle, false); function selectSingle(e) { // Grab the type and checked property from the element attached to the event, // and also the parentNode of its parentNode (the row element) const { type, checked, parentNode: { parentNode } } = e.target; // If the element is a checkbox and the checkbox is checked // (we don't want to return a value if the user has unchecked the box...) if (type === 'checkbox' && checked) { // Find the amountInput child element in that row, // and grab its value const { value } = parentNode.querySelector('.amountInput'); console.log(value); } } function selectAll(e) { const { type, checked } = e.target; // If it is checked `map` over the amountInput elements // and return an array of their values if (checked) { const values = [...amountInputs].map(input => input.value); console.log(values); } } 
 <label class="containerCheck"> Select All <input type="checkbox" id="selectAll"> </label> <table class="inner"> <tbody> <tr> <td><input type="checkbox" class="checkInside" /></td> <td>RD$ <input type="text" class="amountInput" value="432" /></td> </tr> <tr> <td><input type="checkbox" class="checkInside" /></td> <td>RD$ <input type="text" class="amountInput" value="12222" /></td> </tr> <tr> <td><input type="checkbox" class="checkInside" /></td> <td>RD$ <input type="text" class="amountInput" value="324" /></td> </tr> </tbody> </table> <div id="out"></div> 

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

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