简体   繁体   中英

searching specific html table column

hello can someone help me with this. the search is working but how can i exclude the last column to be searched? i have three columns, first name, lastname and email.

what i want is to search using the two columns only. and exclude the column email when searching. thank you this is my javascript code

<script type="text/javascript">
        function doSearch() {
            var searchText = document.getElementById('searchTerm').value;
            var targetTable = document.getElementById('report');
            var targetTableColCount;

            //Loop through table rows
            for (var rowIndex = 0; rowIndex < targetTable.rows.length; rowIndex++ ) {
                var rowData = '';

                //Get column count from header row
                if (rowIndex == 0) {
                    targetTableColCount = targetTable.rows.item(rowIndex).cells.length;
                    continue; //do not execute further code for header row.
                }

                //Process data rows. (rowIndex >= 1)
                for (var colIndex = 0; colIndex < targetTableColCount; colIndex++) {
                    var cellText = '';

                    if (navigator.appName == 'Microsoft Internet Explorer')
                        cellText = targetTable.rows.item(rowIndex).cells.item(colIndex).innerText;
                    else
                        cellText = targetTable.rows.item(rowIndex).cells.item(colIndex).textContent;

                    rowData += cellText;
                }

                // Make search case insensitive.
                rowData = rowData.toLowerCase();
                searchText = searchText.toLowerCase();

                //If search term is not found in row data
                //then hide the row, else show
                if (rowData.indexOf(searchText) == -1)
                    targetTable.rows.item(rowIndex).style.display = 'none';
                else
                    targetTable.rows.item(rowIndex).style.display = 'table-row';
            }
        }
    </script>

and this is my html code

<input id="searchTerm" class="form-control" placeholder="Enter text" onkeyup="doSearch()">  
  <table id="report" class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
    </tbody>
  </table>

i have another question. i added an expandable row on my html table and now the search doesnt give the desired output. example when i search for a value that are not on the html table it just remove the first row and show the rest of the row. which is not the correct output.

<table id="report" class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>

        <th>Email</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
        <td>
        <div class="arrow"><a id="menu-toggle" href="#" ><i class="glyphicon glyphicon-plus-sign"></i></a></div>
        </td>
      </tr>
      <tr><td colspan="4">
        <button type="button" class="btnview btn btn-xs btn-warning"  >
            <span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>View
        </button>
      </td>
      </tr>

      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
        <td>
        <div class="arrow"><a id="menu-toggle" href="#" ><i class="glyphicon glyphicon-plus-sign"></i></a></div>
        </td>
      </tr>
      <tr><td colspan="4">
        <button type="button" class="btnview btn btn-xs btn-warning"  >
            <span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>View
        </button>
      </td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
        <td>
        <div class="arrow"><a id="menu-toggle" href="#" ><i class="glyphicon glyphicon-plus-sign"></i></a></div>
        </td>
      </tr>
      <tr><td colspan="4">
        <button type="button" class="btnview btn btn-xs btn-warning"  >
            <span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>View
        </button>
      </td>
      </tr>
    </tbody>
  </table>

sir roman after integrating you're code to mine. the search is now working as expected. but when the searchterm input is empty and i press backspace on it. it became like this

https://jsfiddle.net/t6xg97uo/ 在此处输入图片说明

I believe this should answer your issue. I am simply adjusting how many columns you are searching:

            for (var colIndex = 0; colIndex < (targetTableColCount-1); colIndex++) {

Here is an example:

http://codepen.io/anon/pen/PNWNmL

Update

Ok, I am not sure this is the right fix for what you are looking for, but I just commented out the code that is causing that button to be revealed when backspacing. Here is what I did:

<tbody>
  <tr>
    <td>John</td>
    <td>Doe</td>
    <td>john@example.com</td>
    <td>
    <div class="arrow"><a id="menu-toggle" href="#" ><i class="glyphicon glyphicon-plus-sign"></i></a></div>
    </td>
  </tr>
  <tr>
    <!-- <td colspan="4">
            <button type="button" class="btnview btn btn-xs btn-warning"  >
        <span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>View
            </button>
          </td> -->
  </tr>

  <tr>
    <td>Mary</td>
    <td>Moe</td>
    <td>mary@example.com</td>
    <td>
    <div class="arrow"><a id="menu-toggle" href="#" ><i class="glyphicon glyphicon-plus-sign"></i></a></div>
    </td>
  </tr>
  <tr>
    <!-- <td colspan="4">
            <button type="button" class="btnview btn btn-xs btn-warning"  >
        <span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>View
            </button>
          </td> -->
  </tr>
  <tr>
    <td>July</td>
    <td>Dooley</td>
    <td>july@example.com</td>
    <td>
    <div class="arrow"><a id="menu-toggle" href="#" ><i class="glyphicon glyphicon-plus-sign"></i></a></div>
    </td>
  </tr>
  <tr>
    <!-- <td colspan="4">
            <button type="button" class="btnview btn btn-xs btn-warning"  >
        <span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>View
            </button>
          </td> -->
  </tr>
</tbody>

And here is a jsfiddle:

https://jsfiddle.net/t6xg97uo/1/

Solution including you additional requirement( with an expandable row within a table ):
- replace your nested for loop(through row columns) as shown below:
(you should also skip rows which have only one cell(td) from processing)

...
var rowCells = targetTable.rows.item(rowIndex).cells, cellsLen = rowCells.length;
if (cellsLen > 1) {
    for (var colIndex = 0; colIndex < 2; colIndex++) {
        var cellText = '';

        if (targetTable.rows.item(rowIndex).cells.item(colIndex)) {                      
            cellText = rowCells.item(colIndex)[(navigator.appName == 'Microsoft Internet Explorer')? "innerText" : "textContent"];
        }
        rowData += cellText;
    }
}

// Make search case insensitive.
rowData = rowData.toLowerCase().trim();
searchText = searchText.toLowerCase();

//If search term is not found in row data
//then hide the row, else show
if (cellsLen > 1) {
    if (searchText && rowData.indexOf(searchText) === -1) {
        targetTable.rows.item(rowIndex).style.display = 'none';
    } else {
        targetTable.rows.item(rowIndex).style.display = 'table-row';
    }
}
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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