簡體   English   中英

使用Asp.Net MVC實體框架數據禁用對數據表的特定列搜索

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

我首先使用Asp.Net MVC從使用實體框架代碼創建的數據庫中獲取一些數據到jQuery數據表中。 因此,對於數據庫中的每個記錄,腳手架還會生成“刪除,詳細信息,編輯”鏈接。 搜索也適用於那些鏈接。 因此,如何允許僅搜索特定列,或/和禁用其他特定列? 在這種情況下,如何禁用該特定列的排序(刪除,詳細信息,編輯)?

我正在使用Asp.Net Core,僅供參考。

編碼:

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腳本{

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

}

標准jQuery Datatables中 ,過濾是在客戶端進行的,並且搜索適用於所有行和列。

您可以通過定義每列的專有性(例如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> 

您可以在配置數據表時通過使用可選參數來存檔。

對於訂單: columns.orderable

數據表

說明使用此參數,可以刪除最終用戶對列進行排序的能力。 這對於生成的內容列可能很有用,例如,如果表中有“編輯”或“刪除”按鈕。

此處搜索的另一個示例線程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM