动态输入字段的搜索功能

时间:2018-11-26 05:29:59

标签: javascript php jquery codeigniter

我有动态的父类,在每个父类中,我有100多个子测试。.我具有可折叠的可折叠菜单,单击该按钮即可显示或隐藏内容。我有一个动态类,名称为ep-dp- dt如果我通过ep-dp-dt[0],我将在哪里搜索输入字段,如果我通过ep-dp-dt[1],我将只能搜索第几类子测试,如果我通过getElementsByClassName('ep-dp-dt')[dynamic_value],我将只能搜索第一类。 ..所以我如何在function myFunction() { var input, filter, table, tr, td, i; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); tr = document.getElementsByClassName('ep-dp-dt')[0].getElementsByTagName('tr'); var i; for(var i = 0, length = tr.length; i < length; i++) { td = tr[i].getElementsByTagName("td")[1]; if (td) { if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } ???

中传递动态值
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="" title="" class="form-control pull-right">

<table id="collapse1" role="tabpanel" aria-labelledby="heading1" class="table table-bordered table-striped collapse1 collapse in" cellspacing="0" width="100%" aria-describedby="example_info" style="width: 100%; height: auto;">

    <thead>
      <tr class="patientinfo " role="row">
      <th></th>
       <th></th>
      </tr>
   </thead>
   <tbody class="ep-dp-dt">
        <td></td>
        <td></td>
    </tbody>
</table>


<table id="collapse2" role="tabpanel" aria-labelledby="heading2" class="table table-bordered table-striped collapse1 collapse in" cellspacing="0" width="100%" aria-describedby="example_info" style="width: 100%; height: auto;">

    <thead>
      <tr class="patientinfo " role="row">
      <th></th>
       <th></th>
      </tr>
   </thead>
   <tbody class="ep-dp-dt">
        <td></td>
        <td></td>
    </tbody>
</table>

表结构类似于:

<h5>edit startup</h5>
 <%= link_to 'Edit', edit_booking_url(@booking)%>

Messages Mailer
class MessagesMailer < ApplicationMailer
 def send_email(booking_users)
  @booking_users = User.includes(:roles).where(roles: {:id=> 2})
  mail(to: "penny12@gmail.com")
 end
end

To get the booking id I have called this mailer in the booking model
  def booking_email
    @booking = Booking.find(self.id)
    BookingMailer.send_email.deliver
  end

... 如此

1 个答案:

答案 0 :(得分:1)

您可以使用两个 input 元素,一个用于基于索引的隐藏/显示表,另一个用于表中的实际搜索字符串。

function myFunctionIndex(idx){
  document.querySelectorAll('.ep-dp-dt').forEach(function(el, i){
    if(idx.trim() == "")
      el.parentNode.style.display = "block";
    else if(i != idx)
      el.parentNode.style.display = "none";
    else
      el.parentNode.style.display = "block";
  });
}

function myFunction(el) {
  var input, filter, table, tr, td, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  var idxVal = document.getElementById("index");
  tr = document.querySelectorAll('.ep-dp-dt tr');
  for(let i = 0, length = tr.length; i < length; i++) {
    td = tr[i].querySelectorAll("td")[1];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }  
  } 
  
}
Table Index: <input type="text" id="index" oninput="myFunctionIndex(this.value)" placeholder="" title="" class="form-control pull-right">
Search By: <input type="text" id="myInput" oninput="myFunction()" placeholder="" title="" class="form-control pull-right">

<table id="collapse1" role="tabpanel" aria-labelledby="heading1" class="table table-bordered table-striped collapse1 collapse in" cellspacing="0" width="100%" aria-describedby="example_info" style="width: 100%; height: auto;">

  <thead>
    <tr class="patientinfo " role="row">
    <th></th>
     <th></th>
    </tr>
  </thead>
  <tbody class="ep-dp-dt">
       <tr>
        <td>first table data 1</td>
        <td>first table data 2</td>
      </tr>
  </tbody>
</table>


<table id="collapse2" role="tabpanel" aria-labelledby="heading2" class="table table-bordered table-striped collapse1 collapse in" cellspacing="0" width="100%" aria-describedby="example_info" style="width: 100%; height: auto;">

  <thead>
    <tr class="patientinfo " role="row">
    <th></th>
     <th></th>
    </tr>
  </thead>
  <tbody class="ep-dp-dt">
      <tr>
        <td>second table data 1</td>
        <td>second table data 2</td>
      </tr>
  </tbody>
</table>