筛选后的数据求和

huangapple go评论66阅读模式
英文:

Sum data after filters

问题

以下是您提供的代码的翻译部分:

function searchTable() {
  var filter, found, table, tr, td, i, j;
  //input = document.getElementById("search");
  //input2 = document.getElementById("search2");
  filter = input.value.toUpperCase();
  table = document.getElementById("displaytable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td");
    for (j = 0; j < td.length; j++) {
      if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
        found = true;
      }
    }
    if (found) {
      tr[i].style.display = "";
      found = false;
    } else {
      tr[i].style.display = "none";
    }
  }
  var cls = document.getElementById("displaytable").getElementsByTagName("td");
  var sum = 0;
  for (var i = 0; i < cls.length; i++) {
    if (cls[i].className == "countable" && cls[i].closest("tr").style.display != "none") {
      sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
    }
  }
  document.getElementById('total').innerHTML = sum;
}

var cls = document.getElementById("displaytable").getElementsByTagName("td");
var sum = 0;
for (var i = 0; i < cls.length; i++) {
  if (cls[i].className == "countable") {
    sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
  }
}
document.getElementById('total').innerHTML += sum;

希望这能对您有所帮助。如果您需要更多翻译或其他帮助,请随时提问。

英文:

I have this html table which will later be replaced by a for loop. I want to sum the data I get in Number table, after applying lookup filters.

Currently the sum remains fixed for all values ​​and does not consider the filter.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function searchTable() {
var filter, found, table, tr, td, i, j;
//input = document.getElementById(&quot;search&quot;);
//input2 = document.getElementById(&quot;search2&quot;);
filter = input.value.toUpperCase();
table = document.getElementById(&quot;displaytable&quot;);
tr = table.getElementsByTagName(&quot;tr&quot;);
for (i = 0; i &lt; tr.length; i++) {
td = tr[i].getElementsByTagName(&quot;td&quot;);
for (j = 0; j &lt; td.length; j++) {
if (td[j].innerHTML.toUpperCase().indexOf(filter) &gt; -1) {
found = true;
}
}
if (found) {
tr[i].style.display = &quot;&quot;;
found = false;
} else {
tr[i].style.display = &quot;none&quot;;
}
}
var cls = document.getElementById(&quot;displaytable&quot;).getElementsByTagName(&quot;td&quot;);
var sum = 0;
for (var i = 0; i &lt; cls.length; i++) {
if (cls[i].className == &quot;countable&quot; &amp;&amp; cls[i].closest(&quot;tr&quot;).style.display != &quot;none&quot;) {
sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
}
}
document.getElementById(&#39;total&#39;).innerHTML = sum;
}
var cls = document.getElementById(&quot;displaytable&quot;).getElementsByTagName(&quot;td&quot;);
var sum = 0;
for (var i = 0; i &lt; cls.length; i++) {
if (cls[i].className == &quot;countable&quot;) {
sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
}
}
document.getElementById(&#39;total&#39;).innerHTML += sum;

<!-- language: lang-html -->

&lt;head&gt;
&lt;!--  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;//cdn.datatables.net/1.11.1/css/jquery.dataTables.min.css&quot;&gt; --&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;//cdn.datatables.net/1.11.1/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;link href=&quot;https://unpkg.com/bootstrap-table@1.21.2/dist/bootstrap-table.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;script src=&quot;https://unpkg.com/bootstrap-table@1.21.2/dist/bootstrap-table.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/bootstrap-table@1.21.2/dist/extensions/filter-control/bootstrap-table-filter-control.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;!-- &lt;input type=&quot;text&quot; id=&quot;search&quot; onkeyup=&#39;searchTable()&#39; placeholder=&quot;Type to search&quot;&gt;
&lt;input type=&quot;text&quot; id=&quot;search2&quot; onkeyup=&#39;searchTable()&#39; placeholder=&quot;Type to search&quot;&gt; --&gt;
&lt;!-- &lt;input class=&quot;form-control&quot; onkeyup=&#39;searchTable()&#39; id=&quot;search&quot; type=&quot;text&quot; placeholder=&quot;Search..&quot;&gt; --&gt;
&lt;table class=&quot;table responsive table table-striped&quot; data-toggle=&quot;table&quot; data-filter-control=&quot;true&quot; data-show-jump-to=&quot;true&quot; data-pagination=&quot;true&quot; data-show-columns=&quot;true&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; data-field=&quot;name&quot; data-filter-control=&quot;input&quot;&gt;Name&lt;/th&gt;
&lt;th scope=&quot;col&quot; data-field=&quot;surname&quot; data-filter-control=&quot;input&quot;&gt;Surname&lt;/th&gt;
&lt;th scope=&quot;col&quot; data-field=&quot;number&quot; data-filter-control=&quot;input&quot;&gt;Number&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody id=&quot;displaytable&quot;&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;Tidor&lt;/td&gt;
&lt;td class=&quot;countable&quot;&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Marc&lt;/td&gt;
&lt;td&gt;Tidor&lt;/td&gt;
&lt;td class=&quot;countable&quot;&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tom&lt;/td&gt;
&lt;td&gt;Cruise&lt;/td&gt;
&lt;td class=&quot;countable&quot;&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jordan&lt;/td&gt;
&lt;td&gt;Michael&lt;/td&gt;
&lt;td class=&quot;countable&quot;&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Marc&lt;/td&gt;
&lt;td&gt;Michael&lt;/td&gt;
&lt;td class=&quot;countable&quot;&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Marc&lt;/td&gt;
&lt;td&gt;Michael&lt;/td&gt;
&lt;td class=&quot;countable&quot;&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;!-- &lt;tfoot class=&quot;shown&quot;&gt;
&lt;tr&gt;
&lt;td colspan=&quot;5&quot;&gt;Total:&lt;/td&gt;
&lt;td id=&quot;total&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tfoot&gt; --&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tfoot class=&quot;shown&quot;&gt;
&lt;tr&gt;
&lt;td colspan=&quot;5&quot;&gt;Total:&lt;/td&gt;
&lt;td id=&quot;total&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

答案1

得分: 1

这不能工作,因为您的函数只有一个过滤器。

并且移除onclick,最好使用事件监听器。

所以,在这里,一开始您的列表为空,因为没有任何输入字段被填充。

编辑 1:而且,请避免使用 var!您可以看一下第一个代码片段,但第二个是更新版本。

编辑 2:请注意第二个代码片段,因为在第一个代码片段中,如果您键入 "M","Tom" 也会出现,因为它包含 "m"!!!

编辑 3:添加了第三个代码片段,以避免同时在多个字段中进行搜索,这样您就不会在前两个代码片段中遇到的问题。

通过保留这三种解决方案,您可以了解前两个提案的不便之处。

以下是翻译的代码部分:

第一个代码片段:

let filter1, filter2;
document.addEventListener("DOMContentLoaded", init);

function init() {
  filter1 = document.getElementById("search");
  filter2 = document.getElementById("search2");
  filter1.addEventListener("keyup", searchTable);
  filter2.addEventListener("keyup", searchTable);
  searchTable();
}

function searchTable() {
  let found, table, tr, td, i;
  filter1 = document.getElementById("search").value.toUpperCase();
  filter2 = document.getElementById("search2").value.toUpperCase();
  table = document.getElementById("displaytable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td");
    found = false;
    if (
      (filter1 && td[0].innerHTML.toUpperCase().indexOf(filter1) > -1) ||
      (filter2 && td[1].innerHTML.toUpperCase().indexOf(filter2) > -1)
    ) {
      found = true;
    }
    tr[i].style.display = found ? "" : "none";
  }
  updateTotal();
}

function updateTotal() {
  let cls = document.getElementById("displaytable").getElementsByTagName("td");
  let sum = 0;
  let originalList = false;
  for (i = 0; i < cls.length; i++) {
    if (
      cls[i].className == "countable" &&
      cls[i].closest("tr").style.display != "none"
    ) {
      sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
      originalList = true;
    }
  }
  document.getElementById("total").innerHTML = originalList ? sum : "";
}

第二个代码片段:

let filter1, filter2, filter3;

document.addEventListener("DOMContentLoaded", init);

function init() {
  filter1 = document.getElementById("search").value.toUpperCase();
  filter2 = document.getElementById("search2").value.toUpperCase();
  filter3 = document.getElementById("search3").value.toUpperCase();

  document.getElementById("search").addEventListener("keyup", searchTable);
  document.getElementById("search2").addEventListener("keyup", searchTable);
  document.getElementById("search3").addEventListener("keyup", searchTable);

  searchTable();
}

function searchTable() {
  let found, table, tr, td, i;
  filter1 = document.getElementById("search").value.toUpperCase();
  filter2 = document.getElementById("search2").value.toUpperCase();
  filter3 = document.getElementById("search3").value.toUpperCase();

  table = document.getElementById("displaytable");
  tr = table.getElementsByTagName("tr");

  for (i = 0; i < tr.length; i++) {
    if (tr[i].getElementsByTagName("th").length > 0) {
      continue;
    }
    td = tr[i].getElementsByTagName("td");
    found = false;

    if (
      (filter1 && td[0].textContent.toUpperCase().startsWith(filter1.trim())) ||
      (filter2 && td[1].textContent.toUpperCase().includes(filter2.trim())) ||
      (filter3 && td[2].textContent.trim().toUpperCase() === filter3.trim())
    ) {
      found = true;
    }

    tr[i].style.display = found ? "" : "none";
  }

  updateTotal();
}

function updateTotal() {
  let cls = document.getElementById("displaytable").getElementsByTagName("td");
  let sum = 0;
  let originalList = false;

  for (var i = 0; i < cls.length; i++) {
    if (
      cls[i].className == "countable" &&
      cls[i].closest("tr").style.display != "none"
    ) {
      sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
      originalList = true;
    }
  }

  document.getElementById("total").innerHTML = originalList ? sum : "";
}

第三个代码片段:

let filter1, filter2, filter3;
let searchInput1;
let searchInput2;
let searchInput3;
let filters;

document.addEventListener("DOMContentLoaded", init);

function init() {
  filter1 = document.getElementById("search");
  filter2 = document.getElementById("search2");
  filter3 = document.getElementById("search3");

  filters = [filter1, filter2, filter3];

  searchInput1 = document.getElementById("search");
  searchInput2 = document.getElementById("search2");
  searchInput3 = document.getElementById("search3");

  filter1.addEventListener("focus", () => resetOtherFields(filter1));
  filter2.addEventListener("focus", () => resetOtherFields(filter2));
  filter3.addEventListener("focus", () => resetOtherFields(filter3));

  filter1.addEventListener("keyup", searchTable);
  filter2.addEventListener("keyup", searchTable);
  filter3.addEventListener("keyup", searchTable);

  searchTable();
}

function resetOtherFields(currentField) {
  filters.forEach((field) => {
    if (field !== currentField) {
      field.value = "";
    }
  });

  searchTable();
}

function searchTable() {
  let found, table, tr, td, i;
  filter1 = searchInput1.value.toUpperCase();
  filter2 = searchInput2.value.toUpperCase();
  filter3 = searchInput3.value.toUpperCase();

  table = document.getElementById("displaytable");
  tr = table.getElementsByTagName("tr");

  for (i = 0; i < tr.length; i++) {
    if (tr[i].getElementsByTagName("th").length > 0) {
      continue;
    }
    td = tr[i].getElementsByTagName("td");
    found = false;

    if (
      (filter1 && td[0].textContent.toUpperCase().startsWith(filter1.trim())) ||
      (filter2 && td[1].textContent.toUpperCase().startsWith(filter2.trim())) ||
      (filter3 && td[2].textContent.trim().toUpperCase() === filter3.trim())
    ) {
      found = true;
    }

    tr[i].style.display = found ? "" : "none";
  }

  updateTotal();
}

function updateTotal() {
  let cls = document.getElementById("displaytable").getElementsByTagName("td");
  let sum = 0;
  let originalList =

<details>
<summary>英文:</summary>

This cannot work because you have just one filter in your function.
And remove the onclick, it&#39;s better to use EventListeners.

So, here at start You start with an empty list because none of the input fields are filled.

**EDIT 1** : And yes, avoid var! You may take a look on the first snippet, but the second is an update.

**EDIT 2** : Check at the second snippet because in the first one if you type &quot;M&quot; Tom will also appear because it contains a &quot;m&quot;!!!

**EDIT 3** : Added a third snippet to avoid searches in multiple fields at the same time, so you don&#39;t have problems that may occur in the two first snippets.

By keeping the three solutions, you can have an idea of the inconveniences of the two first proposals.

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    		let filter1, filter2;
    		document.addEventListener(&quot;DOMContentLoaded&quot;, init);
    		
    		function init(){
    		  filter1 = document.getElementById(&quot;search&quot;);
    		  filter2 = document.getElementById(&quot;search2&quot;);
    		  filter1.addEventListener(&quot;keyup&quot;, searchTable);
    		  filter2.addEventListener(&quot;keyup&quot;, searchTable);
    		  searchTable();
    		}

    		function searchTable() {
    		  let found, table, tr, td, i;
    		  filter1 = document.getElementById(&quot;search&quot;).value.toUpperCase();
    		  filter2 = document.getElementById(&quot;search2&quot;).value.toUpperCase();
    		  table = document.getElementById(&quot;displaytable&quot;);
    		  tr = table.getElementsByTagName(&quot;tr&quot;);
    		  for (i = 0; i &lt; tr.length; i++) {
    			td = tr[i].getElementsByTagName(&quot;td&quot;);
    			found = false;
    			if (
    			  (filter1 &amp;&amp; td[0].innerHTML.toUpperCase().indexOf(filter1) &gt; -1) ||
    			  (filter2 &amp;&amp; td[1].innerHTML.toUpperCase().indexOf(filter2) &gt; -1)
    			) {
    			  found = true;
    			}
    			tr[i].style.display = found ? &quot;&quot; : &quot;none&quot;;
    		  }
    		  updateTotal();
    		}

    		function updateTotal() {
    		  let cls = document.getElementById(&quot;displaytable&quot;).getElementsByTagName(&quot;td&quot;);
    		  let sum = 0;
    		  let originalList = false;
    		  for (i = 0; i &lt; cls.length; i++) {
    			if (
    			  cls[i].className == &quot;countable&quot; &amp;&amp;
    			  cls[i].closest(&quot;tr&quot;).style.display != &quot;none&quot;
    			) {
    			  sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
    			  originalList = true;
    			}
    		  }
    		  document.getElementById(&quot;total&quot;).innerHTML = originalList ? sum : &quot;&quot;;
    		}

&lt;!-- language: lang-html --&gt;


    	&lt;input type=&quot;text&quot; id=&quot;search&quot; placeholder=&quot;Search Name&quot;&gt;
    	&lt;input type=&quot;text&quot; id=&quot;search2&quot; placeholder=&quot;Search Surname&quot;&gt;
    	&lt;!-- &lt;input class=&quot;form-control&quot; onkeyup=&#39;searchTable()&#39; id=&quot;search&quot; type=&quot;text&quot; placeholder=&quot;Search..&quot;&gt; --&gt;
    	&lt;table class=&quot;table responsive table table-striped&quot; data-toggle=&quot;table&quot; data-filter-control=&quot;true&quot; data-show-jump-to=&quot;true&quot; data-pagination=&quot;true&quot; data-show-columns=&quot;true&quot;&gt;
    	  &lt;thead&gt;
    		&lt;tr&gt;
    		  &lt;th scope=&quot;col&quot; data-field=&quot;name&quot; data-filter-control=&quot;input&quot;&gt;Search by Name&lt;/th&gt;
    		  &lt;th scope=&quot;col&quot; data-field=&quot;surname&quot; data-filter-control=&quot;input&quot;&gt;Search by Surname&lt;/th&gt;
    		  &lt;th scope=&quot;col&quot; data-field=&quot;number&quot; data-filter-control=&quot;input&quot;&gt;Number&lt;/th&gt;
    		&lt;/tr&gt;
    	  &lt;/thead&gt;
    	  &lt;tbody id=&quot;displaytable&quot;&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;John&lt;/td&gt;
    		  &lt;td&gt;Tidor&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;5&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Marc&lt;/td&gt;
    		  &lt;td&gt;Tidor&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;5&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Tom&lt;/td&gt;
    		  &lt;td&gt;Cruise&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;4&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Jordan&lt;/td&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;7&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Marc&lt;/td&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;3&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Marc&lt;/td&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;1&lt;/td&gt;
    		&lt;/tr&gt;
    	  &lt;/tbody&gt;

    	&lt;/table&gt;

    	&lt;table&gt;
    	  &lt;tfoot class=&quot;shown&quot;&gt;
    		&lt;tr&gt;
    		  &lt;td colspan=&quot;5&quot;&gt;Total:&lt;/td&gt;
    		  &lt;td id=&quot;total&quot;&gt;&lt;/td&gt;
    		&lt;/tr&gt;
    	  &lt;/tfoot&gt;
    	&lt;/table&gt;

&lt;!-- end snippet --&gt;

**Here&#39;s an edit that is more accurate as I say in my EDIT 2 :**
So :
&quot;M&quot; will display all the names that begins with &quot;M&quot;.
&quot;Mi&quot; will display &quot;Michael&quot;, &quot;Michel&quot;, &quot;Miranda&quot;.
and &quot;MIC&quot; only &quot;Michael&quot; and &quot;Michel&quot;

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;


    		let filter1, filter2, filter3;

    		document.addEventListener(&quot;DOMContentLoaded&quot;, init);

    		function init() {
    		  filter1 = document.getElementById(&quot;search&quot;).value.toUpperCase();
    		  filter2 = document.getElementById(&quot;search2&quot;).value.toUpperCase();
    		  filter3 = document.getElementById(&quot;search3&quot;).value.toUpperCase();

    		  document.getElementById(&quot;search&quot;).addEventListener(&quot;keyup&quot;, searchTable);
    		  document.getElementById(&quot;search2&quot;).addEventListener(&quot;keyup&quot;, searchTable);
    		  document.getElementById(&quot;search3&quot;).addEventListener(&quot;keyup&quot;, searchTable);

    		  searchTable();
    		}

    		function searchTable() {
    		  let found, table, tr, td, i;
    		  filter1 = document.getElementById(&quot;search&quot;).value.toUpperCase();
    		  filter2 = document.getElementById(&quot;search2&quot;).value.toUpperCase();
    		  filter3 = document.getElementById(&quot;search3&quot;).value.toUpperCase();

    		  table = document.getElementById(&quot;displaytable&quot;);
    		  tr = table.getElementsByTagName(&quot;tr&quot;);

    		  for (i = 0; i &lt; tr.length; i++) {
    			if (tr[i].getElementsByTagName(&quot;th&quot;).length &gt; 0) {
    			  continue;
    			}
    			td = tr[i].getElementsByTagName(&quot;td&quot;);
    			found = false;

    			if (
    			  (filter1 &amp;&amp; td[0].textContent.toUpperCase().startsWith(filter1.trim())) ||
    			  (filter2 &amp;&amp; td[1].textContent.toUpperCase().includes(filter2.trim())) ||
    			  (filter3 &amp;&amp; td[2].textContent.trim().toUpperCase() === filter3.trim())
    			) {
    			  found = true;
    			}

    			tr[i].style.display = found ? &quot;&quot; : &quot;none&quot;;
    		  }

    		  updateTotal();
    		}

    		function updateTotal() {
    		  let cls = document.getElementById(&quot;displaytable&quot;).getElementsByTagName(&quot;td&quot;);
    		  let sum = 0;
    		  let originalList = false;

    		  for (var i = 0; i &lt; cls.length; i++) {
    			if (cls[i].className == &quot;countable&quot; &amp;&amp; cls[i].closest(&quot;tr&quot;).style.display != &quot;none&quot;) {
    			  sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
    			  originalList = true;
    			}
    		  }

    		  document.getElementById(&quot;total&quot;).innerHTML = originalList ? sum : &quot;&quot;;
    		}

&lt;!-- language: lang-html --&gt;


    	&lt;input type=&quot;text&quot; id=&quot;search&quot; placeholder=&quot;Search Name&quot;&gt;
    	&lt;input type=&quot;text&quot; id=&quot;search2&quot; placeholder=&quot;Search Surname&quot;&gt;
    	&lt;input type=&quot;text&quot; id=&quot;search3&quot; placeholder=&quot;Search Value&quot;&gt;
    	&lt;table class=&quot;table responsive table table-striped&quot; data-toggle=&quot;table&quot; data-filter-control=&quot;true&quot; data-show-jump-to=&quot;true&quot; data-pagination=&quot;true&quot; data-show-columns=&quot;true&quot;&gt;
    	  &lt;thead&gt;
    		&lt;tr&gt;
    		  &lt;th scope=&quot;col&quot; data-field=&quot;name&quot; data-filter-control=&quot;input&quot;&gt;Search by Name&lt;/th&gt;
    		  &lt;th scope=&quot;col&quot; data-field=&quot;surname&quot; data-filter-control=&quot;input&quot;&gt;Search by Surname&lt;/th&gt;
    		  &lt;th scope=&quot;col&quot; data-field=&quot;number&quot; data-filter-control=&quot;input&quot;&gt;Search by value&lt;/th&gt;
    		&lt;/tr&gt;
    	  &lt;/thead&gt;
    	  &lt;tbody id=&quot;displaytable&quot;&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;John&lt;/td&gt;
    		  &lt;td&gt;Tidor&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;5&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Marc&lt;/td&gt;
    		  &lt;td&gt;Tidor&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;5&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Tom&lt;/td&gt;
    		  &lt;td&gt;Cruise&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;4&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Jordan&lt;/td&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;7&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Marc&lt;/td&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;3&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Marc&lt;/td&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;1&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Miranda&lt;/td&gt;
    		  &lt;td&gt;Mimi&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;10&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Michel&lt;/td&gt;
    		  &lt;td&gt;Mitch&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;1&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;John&lt;/td&gt;
    		  &lt;td&gt;Doe&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;0&lt;/td&gt;
    		&lt;/tr&gt;
    	  &lt;/tbody&gt;

    	&lt;/table&gt;

    	&lt;table&gt;
    	  &lt;tfoot class=&quot;shown&quot;&gt;
    		&lt;tr&gt;
    		  &lt;td colspan=&quot;5&quot;&gt;Total:&lt;/td&gt;
    		  &lt;td id=&quot;total&quot;&gt;&lt;/td&gt;
    		&lt;/tr&gt;
    	  &lt;/tfoot&gt;
    	&lt;/table&gt;

&lt;!-- end snippet --&gt;

Searches in the different fields at the same time may not working as expected!
So I suggest you to reset all the fields to &quot;&quot; when you select a field to fill like in this **third EDIT**

**Third snippet :**

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;


    		let filter1, filter2, filter3;
    		let searchInput1;
    		let searchInput2;
    		let searchInput3;
    		let filters;

    		document.addEventListener(&quot;DOMContentLoaded&quot;, init);

    		function init() {
    		  filter1 = document.getElementById(&quot;search&quot;);
    		  filter2 = document.getElementById(&quot;search2&quot;);
    		  filter3 = document.getElementById(&quot;search3&quot;);
    			
    		  filters = [filter1, filter2, filter3];

    		  searchInput1 = document.getElementById(&quot;search&quot;);
    		  searchInput2 = document.getElementById(&quot;search2&quot;);
    		  searchInput3 = document.getElementById(&quot;search3&quot;);

    		  filter1.addEventListener(&quot;focus&quot;, () =&gt; resetOtherFields(filter1));
    		  filter2.addEventListener(&quot;focus&quot;, () =&gt; resetOtherFields(filter2));
    		  filter3.addEventListener(&quot;focus&quot;, () =&gt; resetOtherFields(filter3));

    		  filter1.addEventListener(&quot;keyup&quot;, searchTable);
    		  filter2.addEventListener(&quot;keyup&quot;, searchTable);
    		  filter3.addEventListener(&quot;keyup&quot;, searchTable);

    		  searchTable();
    		}

    		function resetOtherFields(currentField) {
    		  filters.forEach((field) =&gt; {
    			if (field !== currentField) {
    			  field.value = &quot;&quot;;
    			}
    		  });

    		  searchTable();
    		}

    		function searchTable() {
    		  let found, table, tr, td, i;
    		  filter1 = searchInput1.value.toUpperCase();
    		  filter2 = searchInput2.value.toUpperCase();
    		  filter3 = searchInput3.value.toUpperCase();

    		  table = document.getElementById(&quot;displaytable&quot;);
    		  tr = table.getElementsByTagName(&quot;tr&quot;);

    		  for (i = 0; i &lt; tr.length; i++) {
    			if (tr[i].getElementsByTagName(&quot;th&quot;).length &gt; 0) {
    			  continue;
    			}
    			td = tr[i].getElementsByTagName(&quot;td&quot;);
    			found = false;

    			if (
    			  (filter1 &amp;&amp; td[0].textContent.toUpperCase().startsWith(filter1.trim())) ||
    			  (filter2 &amp;&amp; td[1].textContent.toUpperCase().startsWith(filter2.trim())) ||
    			  (filter3 &amp;&amp; td[2].textContent.trim().toUpperCase() === filter3.trim())
    			) {
    			  found = true;
    			}

    			tr[i].style.display = found ? &quot;&quot; : &quot;none&quot;;
    		  }

    		  updateTotal();
    		}
    		
    		function updateTotal() {
    		  let cls = document.getElementById(&quot;displaytable&quot;).getElementsByTagName(&quot;td&quot;);
    		  let sum = 0;
    		  let originalList = false;

    		  for (var i = 0; i &lt; cls.length; i++) {
    			if (cls[i].className == &quot;countable&quot; &amp;&amp; cls[i].closest(&quot;tr&quot;).style.display != &quot;none&quot;) {
    			  sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
    			  originalList = true;
    			}
    		  }

    		  document.getElementById(&quot;total&quot;).innerHTML = originalList ? sum : &quot;&quot;;
    		}


&lt;!-- language: lang-html --&gt;

    	&lt;input type=&quot;text&quot; id=&quot;search&quot; placeholder=&quot;Search Name&quot;&gt;
    	&lt;input type=&quot;text&quot; id=&quot;search2&quot; placeholder=&quot;Search Surname&quot;&gt;
    	&lt;input type=&quot;text&quot; id=&quot;search3&quot; placeholder=&quot;Search Value&quot;&gt;
    	&lt;table class=&quot;table responsive table table-striped&quot; data-toggle=&quot;table&quot; data-filter-control=&quot;true&quot; data-show-jump-to=&quot;true&quot; data-pagination=&quot;true&quot; data-show-columns=&quot;true&quot;&gt;
    	  &lt;thead&gt;
    		&lt;tr&gt;
    		  &lt;th scope=&quot;col&quot; data-field=&quot;name&quot; data-filter-control=&quot;input&quot;&gt;Search by Name&lt;/th&gt;
    		  &lt;th scope=&quot;col&quot; data-field=&quot;surname&quot; data-filter-control=&quot;input&quot;&gt;Search by Surname&lt;/th&gt;
    		  &lt;th scope=&quot;col&quot; data-field=&quot;number&quot; data-filter-control=&quot;input&quot;&gt;Search by value&lt;/th&gt;
    		&lt;/tr&gt;
    	  &lt;/thead&gt;
    	  &lt;tbody id=&quot;displaytable&quot;&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;John&lt;/td&gt;
    		  &lt;td&gt;Tidor&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;5&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Marc&lt;/td&gt;
    		  &lt;td&gt;Tidor&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;5&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Tom&lt;/td&gt;
    		  &lt;td&gt;Cruise&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;4&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Jordan&lt;/td&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;7&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Marc&lt;/td&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;3&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Marc&lt;/td&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;1&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Miranda&lt;/td&gt;
    		  &lt;td&gt;Mimi&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;10&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Michel&lt;/td&gt;
    		  &lt;td&gt;Mitch&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;1&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;John&lt;/td&gt;
    		  &lt;td&gt;Doe&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;0&lt;/td&gt;
    		&lt;/tr&gt;
    		&lt;tr&gt;
    		  &lt;td&gt;Michael&lt;/td&gt;
    		  &lt;td&gt;Jackson&lt;/td&gt;
    		  &lt;td class=&quot;countable&quot;&gt;4&lt;/td&gt;
    		&lt;/tr&gt;
    	  &lt;/tbody&gt;

    	&lt;/table&gt;

    	&lt;table&gt;
    	  &lt;tfoot class=&quot;shown&quot;&gt;
    		&lt;tr&gt;
    		  &lt;td colspan=&quot;5&quot;&gt;Total:&lt;/td&gt;
    		  &lt;td id=&quot;total&quot;&gt;&lt;/td&gt;
    		&lt;/tr&gt;
    	  &lt;/tfoot&gt;
    	&lt;/table&gt;

&lt;!-- end snippet --&gt;

So, now the reset is done when you change the field into you want to check a value.

</details>



huangapple
  • 本文由 发表于 2023年6月9日 01:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定