如何让DataTables搜索匹配折叠<tr>内容时自动显示主行?
问题描述
使用DataTables管理表格时,部分内容放在默认隐藏(display: none)的折叠<tr>元素中,仅点击对应Details按钮才会显示。希望DataTables搜索过滤器在匹配隐藏<tr>内容时,无需手动展开,自动显示对应的主行。
当前设置
- 每个折叠行有唯一ID
- 将每行内容存储在字典中,结构为
{ 'id_of_row': ['content1', 'content2', ...] } - 目标:用户搜索时在字典中匹配,若命中则显示对应ID的主行
问题示例
搜索'requ'时无结果,因为对应内容在折叠<tr>中;手动展开该行后,匹配内容正常显示。
已尝试方案
通过字典映射主行ID到内容数组,期望搜索匹配时显示对应主行,但未达预期,仅手动展开后才会显示匹配行。
简化版代码
@using Newtonsoft.Json @{// Sample machine names var MachineNames = new List<string> { "Machine A", "Machine B", "Machine C" }; // Sample requests using anonymous objects var requests = new[] { new { Id = 1, ContactName = "Client A", CompanyName = "Machine A", Status = "Pending", Msg = "Request 1" }, new { Id = 2, ContactName = "Client B", CompanyName = "Machine B", Status = "In Progress", Msg = "Request 2" }, new { Id = 3, ContactName = "Client C", CompanyName = "Machine C", Status = "Completed", Msg = "Request 3" } }; // Sample dictionary data for matching the behavior of your original example var requestDictionary = new Dictionary<int, string[]> { { 1, new[] { "client a", "machine a", "pending", "request 1" } }, { 2, new[] { "client b", "machine b", "in progress", "request 2" } }, { 3, new[] { "client c", "machine c", "completed", "request 3" } } }; } <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Collapsible Table with Custom Filter</title> <!-- Include jQuery --> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <!-- Include DataTables CSS & JS --> <link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css"> <script src="https://cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script> <style> table { width: 100%; border-collapse: collapse; } th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; } .show { display: table-row; } .dataTables_filter label { font-weight: bold; font-size: 1.2em; } .dataTables_filter input { margin-left: 10px; padding: 5px; border-radius: 5px; border: 1px solid #ccc; } #search-container { width: auto; padding: 5px; margin-bottom: 10px; } </style> </head> <body> <h1>Collapsible Table with Custom Search Filter</h1> <!-- Search filter --> <div id="search-container"></div> <!-- The table with collapsible rows --> <table id="myTable" class="display table-flexible"> <thead> <tr> <th>Request ID</th> <th>Client</th> <th>Machine</th> <th>Status</th> <th>Description</th> </tr> </thead> <tbody> @foreach (var request in requests) { <tr> <td>@request.Id</td> <td>@request.ContactName</td> <td data-id="@request.CompanyName">@request.CompanyName</td> <td>@request.Status</td> <td> <button class="btn btn-info btn-sm toggle-btn" data-target="#detailsCollapse_@request.Id">Details</button> </td> </tr> <tr id="detailsCollapse_@request.Id" class="details-row" style="display: none;"> <td colspan="6"> <div class="card custom-card-body details-card"> <div class="detail-item"> <strong>Message:</strong> <p class="tw-font-bold">@request.Msg</p> </div> </div> </td> </tr> } </tbody> </table> @section Scripts { <script src="https://cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script> <script> $(document).ready(function () { var requestDictionary = @Html.Raw(JsonConvert.SerializeObject(requestDictionary)); console.log(requestDictionary); // Debugging purposes // Initialize DataTable var table = $('#myTable').DataTable({ columnDefs: [{ "defaultContent": "-", "targets": "_all" }], paging: false, ordering: false, info: false, dom: 'ft' }); // Move search box to custom container $('#search-container').html($('.dataTables_filter')); // Custom search functionality using the requestDictionary function filterTable(searchTerm) { var rows = table.rows().nodes(); // Fetch all rows $(rows).each(function () { var $row = $(this); // The `tr` element // Extract requestId from the first column (adjust if necessary) var requestId = $row.find('td').first().text().trim(); // Ensure requestId is a valid number requestId = parseInt(requestId, 10); if (isNaN(requestId)) { return; // Skip rows without a valid requestId } // Check if requestId exists in the dictionary if (!requestDictionary.hasOwnProperty(requestId)) { return; } var requestDetails = requestDictionary[requestId]; console.log(requestDetails); // Debugging purposes // Check if any of the details contain the search term var match = requestDetails.some(function (detail) { return detail.toLowerCase().includes(searchTerm.toLowerCase()); }); // Show or hide the row based on the search match if (match) { console.log("HIT - Showing row for requestId:", requestId); $row.css("display", "table-row"); // Set display to table-row } else { console.log("MISS - Hiding row for requestId:", requestId); $row.css("display", "none"); // Hide row } }); } // Handle search input $('.dataTables_filter input[type="search"]').on('input', function () { var searchTerm = $(this).val().trim(); filterTable(searchTerm); }); // Attach event listeners for toggling rows $('.toggle-btn').on('click', function () { var target = $(this).data('target'); $(target).fadeToggle(500); }); }); </script> } </body>
更新需求
搜索时希望显示红色标记的主行,而非仅显示点击按钮后才会出现的绿色折叠行。
内容的提问来源于stack exchange,提问作者Duarte Neves
相关产品推荐
相关产品推荐

