如何在表格上方显示搜索过滤器而不是在表格标题下显示?

问题描述 投票:0回答:1

我从事 asp.net MVC 项目。我面临无法在标题上方显示搜索过滤器的问题

意思是先显示搜索过滤器,然后显示表头,然后显示数据。

因此搜索过滤器将位于带有标题的显示表格之后的第一行。

那么我可以做哪些更改来在表标题上方显示搜索过滤器?

我的代码详细信息

 new DataTable('#dtbl', {
            "dom": 'rtip',
            "order": [[0, 'desc'], [5, 'asc']],
            initComplete: function () {
                $('#dtbl tfoot tr').insertAfter($('#dtbl thead tr'))
                this.api()
                    .columns()
                    .every(function () {
                        let column = this;
                        let title = column.footer().textContent;
                        let input = document.createElement('input');
                        input.placeholder = title;
                        column.footer().replaceChildren(input);
                        input.addEventListener('keyup', () => {
                            if (column.search() !== this.value) {
                                column.search(input.value).draw();
                            }
                        });
                    });
            }
        });

 <table id="dtbl" class="table table-bordered table-hover table-striped" style="width:100%;padding-left:5px;
padding-right:7px;">
        <thead>
            <tr style="background-color: #f2f2f2;">

                <th style="border: 1px solid black;">
                    Request No
                </th>
                <th style="border: 1px solid black;">
                    Employee No
                </th>
                <th style="border: 1px solid black;">
                    Employee Name
                </th>
               

            </tr>
        </thead>

        <tbody>
            @foreach (var item in Model)
            {
                <tr style="background-color: #f2f2f2;">

                    <td style="border: 1px solid black;">
                        @Html.DisplayFor(modelItem => item.RequestNo)
                    </td>
                    <td style="border: 1px solid black;">
                        @Html.DisplayFor(modelItem => item.EmpID)
                    </td>
                    <td style="border: 1px solid black;">
                        @Html.DisplayFor(modelItem => item.EmpName)
                    </td>
                   

                </tr>
            }
        </tbody>
        <tfoot>
                <tr>
                <th>Request No</th>
                <th>Employee No</th>
                <th>Employee Name</th>
            </tr>

            </tfoot>
    </table>

预期结果如下:

javascript jquery ajax asp.net-mvc datatable
1个回答
0
投票

腾出空间:

 <thead>
    <tr id="searchFiltersRow"></tr>
    <tr style="background-color: #f2f2f2;">
        <th style="border: 1px solid black;">Request No</th>
        <th style="border: 1px solid black;">Employee No</th>
        <th style="border: 1px solid black;">Employee Name</th>
    </tr>
</thead>

并在你的 every 中添加 th

.every(function () {
....

   searchFiltersRow.append($('<th>').append(input));
© www.soinside.com 2019 - 2024. All rights reserved.