2017-11-10 2 views
3

複数の列を持つ表があります(この質問では2つの列しか作成しませんでした)、ユーザーが選択できるオプションに応じてインデックスを使用して列を検索できるようにします。私は作業コードを持っていますが、検索オプションを変更するためには、各入力の関数をコピーしなければなりませんでした。JavaScriptでインデックスを検索する

インデックスで検索するにはどうすればよいですか?ユーザーが(name)のようなオプションを選択した場合、javascript関数は検索しているインデックスを[0]に変更します。ユーザーが(場所)を選択すると、関数は索引を[1]に変更し、その列を検索します。

これは可能ですか?どんな助けもありがとう。

は、あなたが必要なものを検索するオブジェクトの配列を使用し、各研究でそれを再編成し、HTMLを再作成:

const searchName = document.getElementById('searchName'); 
 
const searchLoc = document.getElementById('searchLoc'); 
 
custSelect.addEventListener('click',() => { 
 
    if (custSelect.value == 'custname') { 
 
    searchName.style.display = 'block'; 
 
    searchLoc.style.display = 'none'; 
 
    } else { 
 
    searchName.style.display = 'none'; 
 
    searchLoc.style.display = 'block'; 
 
    } 
 
}); 
 

 

 
// Search for customer, or search for location, index will change based on option selected. 
 
function tableSearch(id, index) { 
 
    // Declare variables 
 
    var filter, input, table, tr, td, i; 
 
    input = document.getElementById(id); 
 
    filter = input.value.toUpperCase(); 
 
    table = document.getElementById(id); 
 
    tr = table.getElementsByTagName('tr'); 
 

 
    // Loop through all table rows, and hide those who don't match the search query 
 
    for (i = 0; i < tr.length; i++) { 
 
    td = tr[i].getElementsByTagName("td")[0]; // index will change based on option selected. 
 
    if (td) { 
 
     if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { 
 
     tr[i].style.display = ""; 
 
     } else { 
 
     tr[i].style.display = "none"; 
 
     } 
 
    } 
 
    } 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>Test</title> 
 
</head> 
 

 
<body> 
 
    <div id="custDiv"> 
 
    <div class="addBtns"> 
 
     <input id="searchName" onkeyup="tableSearch('searchName','custList'[0])" type="text" placeholder="search name" /> 
 
     <!-- this searches through the first index or [0] --> 
 
     <input id="searchLoc" onkeyup="tableSearch('searchLoc','custList'[1])" style="display: none;" type="text" placeholder="search location" /> 
 
     <!-- this searches through the second index or [1] --> 
 
     <div id="custSelectDiv" style="width: 175px; height: 35px; max-height: 35px; margin: 0 auto;"> 
 
     <select id="custSelect" style="position: absolute;"> 
 
      <option value="custname">name</option> <!-- if user selects this, the corresponding input is displayed, which changes the index to search through --> 
 
      <option value="location">location</option> <!-- if user selects this, the corresponding input is displayed, which changes the index to search through --> 
 
      </select> 
 
     </div> 
 
    </div> 
 
    <table id="custListTop" contenteditable="false"> 
 
     <tr> 
 
     <td style="border-top-left-radius: 5px;">Customers</td> 
 
     <td style="border-top-right-radius: 5px;">Main Location</td> 
 
     </tr> 
 
    </table> 
 
    <table id="custList" contenteditable="true"> 
 
     <tr> 
 
     <td>josh</td> 
 
     <td>hawkins</td> 
 
     </tr> 
 
     <tr> 
 
     <td>hanna</td> 
 
     <td>big sandy</td> 
 
     </tr> 
 
     <tr> 
 
     <td>bonne</td> 
 
     <td>big sandy</td> 
 
     </tr> 
 
     <tr> 
 
     <td>thomas</td> 
 
     <td>big sandy</td> 
 
     </tr> 
 
    </table> 
 
    </div> 
 

 
<script src="test.js"></script> 
 
</body> 
 

 
</html>

+3

であなたを取得する必要があります多分私はオフトピックだが、なぜあなたは、オブジェクトの配列に検索し、テーブルを再描画しませんか?なぜあなたはDOMで検索する必要がありますか?それはただのUIです! – PhilMaGeo

答えて

2

これはトラック

var table = document.getElementById('tab'), 
 
    col = document.getElementById('col'), 
 
    val = document.getElementById('val'); 
 
col.max = table.rows[0].cells.length - 1; 
 

 
function search() { 
 
    var regex = new RegExp(val.value || '', 'i'); 
 
    for (var i = table.rows.length; i-- > 1;) { 
 
    if (regex.test(table.rows[i].cells[+col.value].innerHTML)) { 
 
     table.rows[i].style.display = 'table-row'; 
 
    } else 
 
     table.rows[i].style.display = 'none'; 
 
    } 
 
}
table { 
 
    border-collapse: collapse; 
 
} 
 

 
table, 
 
th, 
 
td { 
 
    border: 1px solid; 
 
}
<label for="col">Column :</label> 
 
<input type="number" id="col" placeholder="column" onkeyup="search()" min="0" step="1" /> 
 
<br> 
 
<label for="val">Find :</label> 
 
<input type="text" id="val" placeholder="cell" onkeyup="search()" /> 
 
<table id="tab"> 
 
    <tr> 
 
    <th>Customers</th> 
 
    <th>Main Location</th> 
 
    </tr> 
 
    <tr> 
 
    <td>josh</td> 
 
    <td>hawkins</td> 
 
    </tr> 
 
    <tr> 
 
    <td>hanna</td> 
 
    <td>big sandy</td> 
 
    </tr> 
 
    <tr> 
 
    <td>bonne</td> 
 
    <td>big sandy</td> 
 
    </tr> 
 
    <tr> 
 
    <td>thomas</td> 
 
    <td>big sandy</td> 
 
    </tr> 
 
</table>

+0

私はこれが好きですが、if(col.value == '1'){label.innerHTML = 'Search Name:';}というif文を使用しようとしています。 }これはinnerHTMLを変更しません – hannacreed

0

は、私は、彼らがその研究を行うには、2つのsimplierの方法があると思いますテーブルが変更されるたびに

dataTableを使用してください。これは、テーブルを並べ替える非常に簡単なツールです。

関連する問題