简体   繁体   English

如何从 laravel 刀片访问 JS 中的所有 foreach 循环数据?

[英]How to access all foreach loop data in JS from laravel blade?

I am displaying data in foreach loop in laravel blade where I only display ten records per page and I have used pagination for showing all records.我在 laravel 刀片中的foreach循环中显示数据,其中每页仅显示十条记录,并且我使用分页来显示所有记录。 Now I want to develop a search or filter functionality in the table but the search can only be applied on one page of the pagination.现在我想在表格中开发search or filter功能,但搜索只能应用于分页的一页。 Is there any way to access all the foreach loop data in the script tag?有什么方法可以访问script标签中的所有foreach循环数据?

Here is my foreach loop and links for pagination:这是我的foreach循环和分页links

<tbody>
                         @foreach($books as $book)
                            <tr>
                                <td>{{ $book->isbn }}</td>
                                <td><a href="{{ route('books.show', $book->id) }}">{{ $book->name }}</a></td>
                                <td>{{ $book->sale_price }}</td>
                                <td>{{ $book->stock }}</td>
                                <td>{{ Str::limit($book->description, 10) }}...</td>
                                <td>{{ $book->category->name }}</td>
                                
                                <td class="text-center">
                                    <ul class="icons-list">
                                        <li class="dropdown">
                                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                                <i class="icon-menu9"></i>
                                            </a>

                                            <ul class="dropdown-menu dropdown-menu-right">
                                                <li><a href="{{ route('books.edit', $book->id) }}"><i class="icon-pencil7"></i>Edit</a></li>
                                                
                                                <li><a href="{{ route('books.changePdf', $book->id) }}"><i class="icon-file-pdf"></i>Change PDF File</a></li>
                                                
                                                <li><a href="#" data-toggle="modal" data-target="#modal_theme_danger_{{ $book->id }}">
                                                    <i class="icon-bin"></i>Delete PDF</a>
                                                </li>

                                                
                                            </ul>
                                        </li>
                                    </ul>
                                </td>
                            </tr>
                           @endforeach
</tbody>  
<div class="text-center">{{ $books->links() }}</div>  

Here is the textbox for search functionality:这是搜索功能的textbox

<div class="row">
  <div class="col-sm-6">
    <label for="txtSearch">Search:</label>
    <input type="text" id="txtSearch" name="txtSearch" class="search form-control" onkeyup="myFunction()">  
  </div>
</div>  

Here is my JS code:这是我的JS代码:

<script type="text/javascript">
function myFunction() {
          // Declare variables
          var input, filter, table, tr, td, i, txtValue;
          input = document.getElementById("txtSearch");
          filter = input.value.toUpperCase();
          table = document.getElementById("tblSearch");
          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")[1];
            if (td) {
              txtValue = td.textContent || td.innerText;
              if (txtValue.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
              } else {
                tr[i].style.display = "none";
              }
            }
          }
        }
    </script>  

The script can search for the given data in the current page of the pagination but cannot apply the search on all the data of the pagination.该脚本可以在分页的当前页面中搜索给定的数据,但不能将搜索应用于分页的所有数据。 Any help is appreciated in advance.提前感谢任何帮助。

Unfortunately, unless you want to use the search button to call a search route, I don't think you can.不幸的是,除非您想使用搜索按钮来调用搜索路线,否则我认为您不能。 Javascript has to work with the data that it has. Javascript 必须使用它拥有的数据。 So every time you fetch paginated data, Javascript only has access to this data.所以每次你获取分页数据时,Javascript 只能访问这些数据。 Again I would suggest linking the search button to a function (using Ajax to call the route if you like) and then fetching the new data based on your params.我再次建议将搜索按钮链接到 function(如果您愿意,可以使用 Ajax 调用路线),然后根据您的参数获取新数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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