简体   繁体   English

在MVC中使用WebGrid的jQuery DataTable实现用于多列搜索

[英]jQuery DataTable implementation with WebGrid in mvc for multi-column search

I am using WebGrid and jQuery DataTable, but the problem is that not all the functionalities of jQuery DataTable work well. 我正在使用WebGrid和jQuery DataTable,但是问题是,并非jQuery DataTable的所有功能都能正常工作。

  • The search functionality is limited to the current page 搜索功能仅限于当前页面
  • It shows only one page containing 10 records (total no of records : 50) 它仅显示一页包含10条记录的记录(总记录数:50)
  • Multi-column search doesn't work or not even visible. 多列搜索不起作用甚至不可见。

Please help. 请帮忙。 Thanks :) 谢谢 :)

The following is the script for DataTable: 以下是DataTable的脚本:

$(document).ready(function () {
    var table = $('#webgrid').DataTable({
    "dom": "lfrti",         //to disable paging dropdown
    "bPaginate": false,     //to disable pagination
    "bInfo": false,         //to disable showing entries
    });

    $('#webgrid tfoot th').each(function () {
        var title = $(this).text();
        $(this).html('<input type="text" placeholder="Search ' + title + '" />');
    });

    // DataTable
    var table = $('#webgrid').DataTable();

    // Apply the search
    table.columns().every(function () {
        var that = this;

        $('input', this.footer()).on('keyup change', function () {
            if (that.search() !== this.value) {
                that
                    .search(this.value)
                    .draw();
            }
        });
    });});

The below code is for the WebGrid: 以下代码用于WebGrid:

    @using (Html.BeginForm("Persons", "Welcome", FormMethod.Get, new {@class = "Search-form"}))
    {
    <div id="DivGrid">
    @{
    var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 10,
        defaultSort: "Employee ID", columnNames: new[] { "Employee_ID", 
"First_Name", "Last_Name", "Date_Of_Birth" });
    if (Model.Count() > 0)
    {
        <div class="moveRight"><strong> @ViewBag.SearchParameter</strong> | @grid.TotalRowCount @Html.Label("Record(s) found")</div>

        @grid.GetHtml(tableStyle: "PGrid", headerStyle: "Header", alternatingRowStyle: "altRow", htmlAttributes: new { id = "webgrid" }, columns: grid.Columns(
                 grid.Column("Employee_ID", "Employee ID " + MyFormAppln.Models.helpers.SortDirection(null, ref grid, "Employee ID"),
            format: @<text>  <span class="display-mode grid-filter-btn">@item.Employee_ID </span>
            <label id="EmployeeID" class="edit-mode grid-filter-btn">@item.Employee_ID</label> </text>, style: "col2Width"),

            grid.Column("First_Name", "First Name " + MyFormAppln.Models.helpers.SortDirection(null, ref grid, "First_Name"), format: @<text>  <span class="display-mode">
                <label id="lblFirstName">@item.First_Name</label>
            </span> <input type="text" id="FirstName" value="@item.First_Name" class="edit-mode" name="firstname" data-col="firstname" /></text>, style: "col2Width"),

            grid.Column("Last_Name", "Last Name " + MyFormAppln.Models.helpers.SortDirection(null, ref grid, "Last_Name"), format: @<text> <span class="display-mode">
                <label id="lblLastName">@item.Last_Name</label>
            </span>  <input type="text" id="LastName" value="@item.Last_Name" class="edit-mode" name="lastname" data-col="lastname" /> </text>, style: "col2Width"),

            grid.Column("Date_Of_Birth", "Date Of Birth", format: item => ((item.Date_Of_Birth == null) ? "" : item.Date_Of_Birth.ToString("MM/dd/yyyy")), style: "DateOfBirth"),
            //grid.Column("Date_Of_Birth", "Date Of Birth", format: item => ((item.Date_Of_Birth == null) ? "" : item.Date_Of_Birth.ToString("MM/dd/yyyy")) ),
            grid.Column(header: "Action", canSort: false, style: "action", format: @<text>
                <button class="edit-user display-mode glyphicon glyphicon-edit"> Edit</button>
                <button class="display-mode delete-item glyphicon glyphicon-trash"> Delete</button>
                <button class="save-user edit-mode glyphicon glyphicon-save"> Save</button>
                <button class="cancel-user edit-mode glyphicon glyphicon-remove-sign"> Cancel</button></text>)));
    }
    else
    {
        <hr />@Html.Label("No, Record(s) not found")<<hr />
    }
        }
    </div>}

The search functionality is limited to the current page Your webgrid has paginated your data table It shows only one page containing 10 records (total no of records : 50) Your webgrid has a 10 rows per page attribute Multi-column search doesn't work or not even visible. 搜索功能仅限于当前页面您的Webgrid已对数据表进行分页它仅显示一个包含10条记录的页面(记录总数:50)您的Webgrid每页具有10行属性多列搜索无效或甚至看不见 https://datatables.net/examples/api/multi_filter.html https://datatables.net/examples/api/multi_filter.html

Try 尝试

var grid = new WebGrid(source: Model, canPage: false,

DataTable script code : DataTable脚本代码:

var oTable;
var asInitVals = new Array();

$(document).ready(function () {
    oTable = $('#webgrid').dataTable({
        "oLanguage": {
            "sSearch": "Search all columns:"
        }
    });

    $("tfoot input").keyup(function () {
        /* Filter on the column (the index) of this element */
        oTable.fnFilter(this.value, $("tfoot input").index(this));
    });
    /*
     * Support functions to provide a little bit of 'user friendlyness' to the textboxes in
     * the footer
     */
    $("tfoot input").each(function (i) {
        asInitVals[i] = this.value;
    });
});

WebGrid code : WebGrid代码:

 @grid.Table(tableStyle: "PGrid", headerStyle: "Header", footerStyle: "Footer", alternatingRowStyle: "altRow", htmlAttributes: new { id = "webgrid" }, columns: grid.Columns(
                 grid.Column("Employee_ID", "Employee ID",
            format: @<text>  <span class="display-mode grid-filter-btn">@item.Employee_ID </span>
            <label id="EmployeeID" class="edit-mode grid-filter-btn">@item.Employee_ID</label> </text>, style: "col2Width"),

            grid.Column("First_Name", "First Name", format: @<text>  <span class="display-mode">
                <label id="lblFirstName">@item.First_Name</label>
            </span> <input type="text" id="FirstName" value="@item.First_Name" class="edit-mode" name="firstname" data-col="firstname" /></text>, style: "col2Width"),

            grid.Column("Last_Name", "Last Name", format: @<text> <span class="display-mode">
                <label id="lblLastName">@item.Last_Name</label>
            </span>  <input type="text" id="LastName" value="@item.Last_Name" class="edit-mode" name="lastname" data-col="lastname" /> </text>, style: "col2Width"),

            grid.Column("Date_Of_Birth", "Date Of Birth", format: item => ((item.Date_Of_Birth == null) ? "" : item.Date_Of_Birth.ToString("MM/dd/yyyy")), style: "DateOfBirth"),
            grid.Column(header: "Action", canSort: false, style: "action", format: @<text>
                <button class="edit-user display-mode glyphicon glyphicon-edit"> Edit</button>
                <button class="display-mode delete-item glyphicon glyphicon-trash"> Delete</button>
                <button class="save-user edit-mode glyphicon glyphicon-save"> Save</button>
                <button class="cancel-user edit-mode glyphicon glyphicon-remove-sign"> Cancel</button></text>)));
        <table class='container'>
            <tfoot class='filters multipleSearch col-md-12'>
                <tr>
                    <th class="txtBoxWidth">
                        <input class='txtBox1 ' placeholder='Employee Id' />
                    </th>
                    <th class="txtBoxWidth">
                        <input class='txtBox2 ' placeholder='First Name' />
                    </th>
                    <th class="txtBoxWidth">
                        <input class='txtBox3 ' placeholder='Last Name' />
                    </th>
                    <th class="txtBoxWidth">
                        <input class='txtBox4 ' placeholder='Date of Birth' />
                    </th>
                    <th>

                    </th>
                </tr>
            </tfoot>
        </table>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM