简体   繁体   English

如何在 asp.net 内核的 Datatable 中搜索动态列

[英]How to search dynamic columns in Datatable in asp.net core

Following the example, on https://www.datatables.net/release-datatables/examples/api/regex.html I want to filer the table by columns, The global search us working fine but search the columns is showing an error that.按照示例,在https://www.datatables.net/release-datatables/examples/api/regex.html我想按列归档表格,全局搜索我们工作正常,但搜索列显示错误. I think I have follwed the example very well but I guess I am still missing something ther我想我已经很好地遵循了这个例子,但我想我仍然错过了一些东西

Uncaught TypeError: $(...).DataTable(...).column(...).search(...).draw is not a function
    at filterColumn (Index:667)
    at HTMLInputElement.<anonymous> (Index:637)
    at HTMLInputElement.dispatch (jquery.min.js:2)
    at HTMLInputElement.v.handle (jquery.min.js:2)

This is my code这是我的代码


@section Styles {
    <!-- DataTables -->
    <link rel="stylesheet" href="~/libs/datatables/css/dataTables.bootstrap.min.css">
    <link href="~/css/listedviews.css" rel="stylesheet" />

}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
        integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
        crossorigin="anonymous"></script>

@section Scripts {
    <!-- DataTables -->
    <script src="~/libs/datatables/js/jquery.dataTables.min.js"></script>
    <script src="~/libs/datatables/js/dataTable110.21.js"></script>

}
<script>
    $(document).ready(function () {
        $('#dailyload').DataTable({
            pageLength: 10,
            ajax: {
                url: '/MonthlyInterest/MonthlyDetails',
                dataSrc: ''
            },
            columns: [
                {title: 'Account No', data: 'loanAccountNo'},
                {title: 'Amount Written off',data: 'amountWrittenOff'}
            ]
        });

        $('input.global_filter').on('keyup click', function () {
            filterGlobal();
        });

        $('input.column_filter').on('keyup click', function () {
            filterColumn($(this).parents('tr').attr('data-column'));
        });
    });

    function filterGlobal() {
        $('#dailyload').DataTable().search(
            $('#global_filter').val(),
        ).draw();
    }

    function filterColumn(i) {
        $('#dailyload').DataTable().column(i).search(
            $('#col' + i + '_filter').val()
        ).draw();
    }
</script>

<div class="container">
    <div class="row">
        <div class="box">
            <div class="box-header">
            </div>

            <div class="box-body" style="padding-right: 80px">
                <div style="background-color:#f5f5f5; padding:20px">
                    <h2>Search Panel</h2>
                    <table>
                        <tbody>
                            <tr>
                                <td>Employee Name</td>
                                <td align="center"><input type="text" class="global_filter" id="global_filter"></td>
                                <td align="center"><input type="text" class="column_filter" id="col0_filter"></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <table id="dailyload" class="table table-striped">
                </table>
            </div>
        </div>
    </div>
</div>

Actually you were pretty close.其实你已经很亲近了。

You forgot to include your second filter on its own row and include the data-column attribute on its tr tag.您忘记在自己的行中包含第二个过滤器,并在其tr标签中包含data-column属性。 This is your original code:这是您的原始代码:

<tr>
    <td>Employee Name</td>
    <td align="center"><input type="text" class="global_filter" id="global_filter"></td>
    <td align="center"><input type="text" class="column_filter" id="col0_filter"></td>
</tr>

And this is how you should do it:这就是你应该这样做的方式:

<tr>
    <td>Employee Name</td>
    <td align="center"><input type="text" class="global_filter" id="global_filter"></td>
</tr>
<tr data-column="0">
    <td>Another Filter</td>
    <td align="center"><input type="text" class="column_filter" id="col0_filter"></td>
</tr>

Look at my example:看我的例子:

 var jsonData = [ { "loanAccountNo": "500507082020", "amountWrittenOff": "$320,800" }, { "loanAccountNo": "308205105020", "amountWrittenOff": "$170,750" }, { "loanAccountNo": "120205205070", "amountWrittenOff": "$186,800" } ]; $(document).ready(function () { $('#dailyload').DataTable({ pageLength: 10, /*ajax: { url: '/MonthlyInterest/MonthlyDetails', dataSrc: '' },*/ data: jsonData, columns: [ {title: 'Account No', data: 'loanAccountNo'}, {title: 'Amount Written off',data: 'amountWrittenOff'} ] }); $('input.global_filter').on('keyup click', function () { filterGlobal(); }); $('input.column_filter').on('keyup click', function () { filterColumn($(this).parents('tr').attr('data-column')); }); }); function filterGlobal() { $('#dailyload').DataTable().search( $('#global_filter').val(), ).draw(); } function filterColumn(i) { $('#dailyload').DataTable().column(i).search( $('#col' + i + '_filter').val() ).draw(); } /*function filterColumn2(i) { $('#dailyload').DataTable().columns(i).search( $('#col' + i + '_filter').val() ).draw(); }*/
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <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> <div class="container"> <div class="row"> <div class="box"> <div class="box-header"> </div> <div class="box-body" style="padding-right: 80px"> <div style="background-color:#f5f5f5; padding:20px"> <h2>Search Panel</h2> <table> <tbody> <tr> <td>Employee Name</td> <td align="center"><input type="text" class="global_filter" id="global_filter"></td> </tr> <tr data-column="0"> <td>Another Filter</td> <td align="center"><input type="text" class="column_filter" id="col0_filter"></td> </tr> </tbody> </table> </div> <br> <table id="dailyload" class="table table-striped"> </table> </div> </div> </div> </div>

By the way, in my first attempt I was having the same issue (same error message), and I changed this line:顺便说一句,在我的第一次尝试中,我遇到了同样的问题(同样的错误消息),我改变了这一行:

 $('#dailyload').DataTable().column(i).search(

For this:为了这:

 $('#dailyload').DataTable().columns(i).search(

And finally it was working as expected.最后它按预期工作。 Notice the extra s in columns .注意columns中多余的 s

But then I tried again removing the s (with column ), and suddenly, to my surprise, it was working.但后来我再次尝试删除s (与column ),突然,令我惊讶的是,它正在工作。 Strange glitch, maybe?奇怪的故障,也许?

Happy Coding!快乐编码!

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

相关问题 在ASP.NET Core中发布jQuery Datatable导致400错误 - Posting jQuery Datatable in ASP.NET Core causes 400 error 如何在“a”标签中定义与 asp.net 核心视图中的行 ID 相关的详细数据表 - How to show detail Datatable relate to row id in asp.net core view define in "a" tag 带有动态搜索的 ASP.NET Core MVC 选择 - ASP.NET Core MVC Select with dynamic searchig editfor 内部 editorfor,用于 asp.net core 中的动态列表 - editfor inside editorfor, for dynamic list in asp.net core 如何在动态创建的表单中使用 JQuery Ajax 将表单数据提交到 ASP.NET 核心 MVC 操作 - How to submit formdata to ASP.NET Core MVC action using JQuery Ajax in a dynamic created form 使用 ajax 和部分视图在 asp.net 核心中进行分页和搜索 - paging and search with ajax and partial view in asp.net core 如何将以下使用复选框的数据表脚本代码全部调整到我的 asp.net 核心应用程序? - How can I adapt the following datatable script code which uses checkbox all to my asp.net core app? ASP.NET如何打印动态表 - asp.net how to print dynamic table 具有DataTable.net数据表的ASP.Net Core 2.1不显示任何数据 - ASP.Net Core 2.1 with DataTable.net datatables is not displaying any data 如何在asp.net GridView上应用Datatable jQuery? - how can I apply Datatable jquery on asp.net GridView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM