无法将行动态添加到 Jquery 数据表

我在 jquery 数据表中执行 2 个函数。

  1. 根据选择标签搜索和过滤表格。

  2. 通过 ajax 将行动态添加到数据表。

当我添加行时,虽然行被添加到表中并且行数在分页中增加但是表由于搜索功能而显示空行。刷新表格后,我就能看到所有行。我发现主要问题出在$.fn.dataTable.ext.search.push()函数中,因为table.rows.add($trHTML).draw(); 不管用。由于我无法解决的搜索功能,正在创建一些依赖项。请提供一些建议来解决它。


qq_遁去的一_1
浏览 99回答 1
1回答

青春有我

我用我的一个小存储库创建了一个小示例,希望它能帮助您解决问题:var table = $('#example').DataTable({&nbsp; "bLengthChange": false,&nbsp; //searching: false,&nbsp; pageLength: 3,&nbsp; dom: 'tip'});&nbsp;&nbsp;$.fn.dataTable.ext.search.push(function( settings, data, dataIndex ) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var filterCategory= $("#cato option:selected").text().toUpperCase();&nbsp; &nbsp; var filterSubCategory= $("#subo option:selected").text().toUpperCase();&nbsp; &nbsp; var subCategory = String(data[2]).toUpperCase();&nbsp; &nbsp; var category = String(data[3]).toUpperCase();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; //console.log(filterSubCategory);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(filterSubCategory != "-SELECT SUBCATEGORY-") {&nbsp; &nbsp; &nbsp; &nbsp; if ( filterCategory == category && filterSubCategory == subCategory)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(filterCategory != "-SELECT CATEGORY-") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( filterCategory == category)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; });$('#cato').on('change', function() {&nbsp; $('#subo').val("");&nbsp; table.draw();});$('#subo').on('change', function() {&nbsp; table.draw();});function getInfo() {&nbsp; &nbsp; &nbsp; &nbsp; var $subCategory = $("#subo option:selected").text()&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; type:'GET',&nbsp; &nbsp; &nbsp; url: "https://my-json-server.typicode.com/SagnalracSO/repo/items?subcategory=" + $subCategory,&nbsp; &nbsp; &nbsp; /*data:{&nbsp; &nbsp; &nbsp; &nbsp; name:$('#name').val(),&nbsp; &nbsp; &nbsp; &nbsp; desc:$('#desc').val(),&nbsp; &nbsp; &nbsp; &nbsp; category:$("#catoo option:selected").text(),&nbsp; &nbsp; &nbsp; &nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; 'csrfmiddlewaretoken': '{{ csrf_token }}'&nbsp; &nbsp; &nbsp; },*/&nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp; &nbsp; beforeSend: function(jqXHR, settings) {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($subCategory.toUpperCase() == '-SELECT SUBCATEGORY-') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('Select a SubCategory');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jqXHR.abort();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var item = data[0];&nbsp; &nbsp; &nbsp; &nbsp; var jRow = $("<tr>").append("<td>" + item.id + "</td><td>" + item.product + "</td><td>" + item.subcategory + "</td><td>" + item.category + "</td>").append("</tr>");&nbsp; &nbsp; &nbsp; &nbsp; table.row.add(jRow).draw();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;});}<link href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css" rel="stylesheet"/><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script><select id="cato" class="form-control" >&nbsp; <option value="" disabled selected="true">-Select Category-</option>&nbsp; <option>Electronics</option>&nbsp; <option>Sports</option></select><select id="subo" class="form-control">&nbsp; &nbsp;<option value="" disabled selected="true">-Select Subcategory-</option>&nbsp; &nbsp;<option>Laptop</option>&nbsp; &nbsp;<option>Mobile</option></select><table id="example" class="table display">&nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th>Id</th>&nbsp; &nbsp; &nbsp; <th>Product</th>&nbsp; &nbsp; &nbsp; <th>Subcategory</th>&nbsp; &nbsp; &nbsp; <th>Category</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody id="r">&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>1</td>&nbsp; &nbsp; &nbsp; <td>Samsung</td>&nbsp; &nbsp; &nbsp; <td>Mobile</td>&nbsp; &nbsp; &nbsp; <td>Electronics</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2</td>&nbsp; &nbsp; &nbsp; <td>Racket</td>&nbsp; &nbsp; &nbsp; <td>Tennis</td>&nbsp; &nbsp; &nbsp; <td>Sports</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>3</td>&nbsp; &nbsp; &nbsp; <td>Bat</td>&nbsp; &nbsp; &nbsp; <td>Cricket</td>&nbsp; &nbsp; &nbsp; <td>Sports</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>4</td>&nbsp; &nbsp; &nbsp; <td>Dell</td>&nbsp; &nbsp; &nbsp; <td>Laptop</td>&nbsp; &nbsp; &nbsp; <td>Electronics</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>5</td>&nbsp; &nbsp; &nbsp; <td>Iphone</td>&nbsp; &nbsp; &nbsp; <td>Mobile</td>&nbsp; &nbsp; &nbsp; <td>Electronics</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>6</td>&nbsp; &nbsp; &nbsp; <td>Soccer Ball</td>&nbsp; &nbsp; &nbsp; <td>Soccer</td>&nbsp; &nbsp; &nbsp; <td>Sports</td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table><br><br><input type="button" value="ADD ROWS" onClick="getInfo()" />如果您的 Ajax 请求返回多条记录,那么您可以替换为:var item = data[0];&nbsp; &nbsp; var jRow = $("<tr>").append("<td>" + item.id + "</td><td>" + item.product + "</td><td>" + item.subcategory + "</td><td>" + item.category + "</td>").append("</tr>");&nbsp; &nbsp; table.row.add(jRow).draw();有了这个:for (var item in data) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var item = data[item];&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var jRow = $("<tr>").append("<td>" + item.id + "</td><td>" + item.product + "</td><td>" + item.subcategory + "</td><td>" + item.category + "</td>").append("</tr>");&nbsp; &nbsp; &nbsp; &nbsp; table.row.add(jRow).draw();&nbsp; &nbsp; }顺便说一句,如果将来你想创建涉及调用 API(通过 Ajax 请求)的示例,就像我在这个示例中所做的那样,我想向你推荐这个网站JSONPlaceholder
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript