搜索特定的html表列

时间:2016-03-16 20:15:45

标签: javascript html

你好,有人可以帮助我。搜索工作正常 但是如何排除要搜索的最后一列? 我有三列,名字,姓氏和电子邮件。

我想要的是仅使用两列进行搜索。并在搜索时排除列电子邮件。谢谢 这是我的javascript代码

<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>

这是我的HTML代码

<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>

我有另一个问题。我在我的html表上添加了一个可扩展的行 现在搜索没有给出所需的输出。例如,当我搜索不在html表上的值时,它只删除第一行并显示行的其余部分。这不是正确的输出。

<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>
将您的代码整合到我的代码之后,罗马先生。搜索现在按预期工作。但当searchterm输入为空时我按下退格键。它变得像这样

https://jsfiddle.net/t6xg97uo/ enter image description here

2 个答案:

答案 0 :(得分:1)

我相信这应该可以回答你的问题。我只是调整您要搜索的列数:

 $this->db->join('table1name pr1','pr1.id = u.tb1_id','right');
 $this->db->join('table2name tb2','tb2.id = u.tb2_id','right');

以下是一个例子:

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

<强> 更新

好的,我不确定这是否适合您正在寻找的内容,但我只是注释掉了导致该按钮在退回时显示的代码。这是我做的:

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

这是一个jsfiddle:

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

答案 1 :(得分:1)

包含额外要求的解决方案(表格中有可扩展的行):
- 替换嵌套的for循环(通过行列),如下所示:
(您还应该跳过处理中只有一个单元格(td)的行)

...
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';
    }
}
...