简体   繁体   English

使用Asp.Net MVC实体框架数据禁用对数据表的特定列搜索

[英]Disable specific columns search on datatable with Asp.Net MVC Entity Framework data

I'm getting some data into my jQuery datatable from a database created with entity framework code first, using Asp.Net MVC. 我首先使用Asp.Net MVC从使用实体框架代码创建的数据库中获取一些数据到jQuery数据表中。 So for every record in the database there is also the "delete, detail, edit" links generated by the scaffolder. 因此,对于数据库中的每个记录,脚手架还会生成“删除,详细信息,编辑”链接。 The search works also for those links. 搜索也适用于那些链接。 So, how can I allow search only for a specific column, or/and disable it for other specific columns? 因此,如何允许仅搜索特定列,或/和禁用其他特定列? And in this case, how can I also disable sorting for this specific column (delete, details, edit)? 在这种情况下,如何禁用该特定列的排序(删除,详细信息,编辑)?

I am using Asp.Net Core, just for extra information. 我正在使用Asp.Net Core,仅供参考。

The code: 编码:

div class="row">
<div class="col-sm-12">
    <p>
        <a asp-action="Create">Create New</a>
    </p>
    <div class="panel panel-default">
        <div class="panel panel-heading">
            Reports
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-hover table-responsive" id="dataTables">
                    <thead>
                        <tr>
                            <th>
                                @Html.DisplayNameFor(model => model.Description)
                            </th>

                            <th>
                                @Html.DisplayNameFor(model => model.RepFile)
                            </th>                                

                            <th class="col-sm-2"></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td>
                                    @Html.DisplayFor(modelItem => item.Description)
                                </td>

                                <td>
                                    @Html.DisplayFor(modelItem => item.RepFile)
                                </td>

                                <td>
                                    <a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
                                    <a asp-action="Details" asp-route-id="@item.ID">Details</a> |
                                    <a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    </div>
</div>

@section scripts{ @section脚本{

<script>
    $(document).ready(function () {
        $('#dataTables').DataTable({
            responsive: true
        });
    });
</script>

} }

In standard jQuery Datatables , the filtering is done on the client side and the search works on all rows and columns. 标准jQuery Datatables中 ,过滤是在客户端进行的,并且搜索适用于所有行和列。

You can customise the configuration by defining proprieties (like searchable and orderable ) for each columns: 您可以通过定义每列的专有性(例如searchableorderable )来自定义配置:

 $('#example').dataTable({ "columns": [{ "searchable": true, "orderable": true }, { "searchable": true, "orderable": true }, { "searchable": true, "orderable": true }, { "searchable": false, "orderable": false }, { "searchable": false, "orderable": false }, { "searchable": false, "orderable": false }, ] }) 
 <script src="https://cdn.datatables.net/u/bs/jq-2.2.3,dt-1.10.12/datatables.min.js"></script> <script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> <link href="https://cdn.datatables.net/u/bs/jq-2.2.3,dt-1.10.12/datatables.min.css" rel="stylesheet" /> <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" /> <table id="example" class="display" cellspacing="0" width="100%"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </thead> <tfoot> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </tfoot> <tbody> <tr> <td>Tiger Nixon</td> <td>System Architect</td> <td>Edinburgh</td> <td>61</td> <td>2011/04/25</td> <td>$320,800</td> </tr> <tr> <td>Garrett Winters</td> <td>Accountant</td> <td>Tokyo</td> <td>63</td> <td>2011/07/25</td> <td>$170,750</td> </tr> <tr> <td>Ashton Cox</td> <td>Junior Technical Author</td> <td>San Francisco</td> <td>66</td> <td>2009/01/12</td> <td>$86,000</td> </tr> <tr> <td>Cedric Kelly</td> <td>Senior Javascript Developer</td> <td>Edinburgh</td> <td>22</td> <td>2012/03/29</td> <td>$433,060</td> </tr> <tr> <td>Airi Satou</td> <td>Accountant</td> <td>Tokyo</td> <td>33</td> <td>2008/11/28</td> <td>$162,700</td> </tr> <tr> <td>Brielle Williamson</td> <td>Integration Specialist</td> <td>New York</td> <td>61</td> <td>2012/12/02</td> <td>$372,000</td> </tr> <tr> <td>Herrod Chandler</td> <td>Sales Assistant</td> <td>San Francisco</td> <td>59</td> <td>2012/08/06</td> <td>$137,500</td> </tr> </tbody> </table> 

You can archieve that by using optional parametters when configuring the datatable. 您可以在配置数据表时通过使用可选参数来存档。

For order : columns.orderable . 对于订单: columns.orderable

From DataTables 数据表

Description Using this parameter, you can remove the end user's ability to order upon a column. 说明使用此参数,可以删除最终用户对列进行排序的能力。 This might be useful for generated content columns, for example if you have 'Edit' or 'Delete' buttons in the table. 这对于生成的内容列可能很有用,例如,如果表中有“编辑”或“删除”按钮。

Another example thread for searching here 此处搜索的另一个示例线程

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

相关问题 想要在ASP.NET MVC实体框架应用程序中使用存储过程创建自动完成搜索 - Want to create autocomplete Search using stored procedure in ASP.NET MVC Entity Framework application 如何在 asp.net 内核的 Datatable 中搜索动态列 - How to search dynamic columns in Datatable in asp.net core Ajax &amp; ASP.NET MVC:在不使用实体框架的情况下通过 ID 获取数据 - Ajax & ASP.NET MVC : get data by ID without using Entity Framework Ajax &amp; ASP.NET Core MVC 6..0:在不使用实体框架的情况下通过 ID 获取数据 - Ajax & ASP.NET Core MVC 6..0 : Get data by ID without using Entity Framework 在 ASP.NET MVC 中通过 jQuery ajax 发送和接收数据并更新实体框架中的数据库 - Send and receive data through jQuery ajax and update database in Entity Framework in ASP.NET MVC 使用实体框架在 ASP.NET MVC 中进行 Ajax 搜索和分页 - Ajax searching and paging in ASP.NET MVC using Entity Framework 使用实体框架在 ASP.NET MVC 中进行 Ajax 分页 - Ajax paging in ASP.NET MVC with Entity Framework ASP.NET MVC中应该为DataTable返回哪种数据? - Which kind of data should be returned for DataTable in ASP.NET MVC? 无法将数据绑定到 ASP.NET MVC 中的数据表 - Not able to bind data to datatable in ASP.NET MVC ASP.NET MVC DataTable sum 列 - ASP.NET MVC DataTable sum column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM