【问题标题】:searching a jquery datatables搜索 jquery 数据表
【发布时间】:2014-08-30 18:35:32
【问题描述】:

我正在使用 jquery datatables 1.10 并尝试搜索和过滤表。我想使用一个搜索两列的搜索文本框和一个复选框来过滤第三列的结果。这是我的数据表:

var url = '@Url.Action("SupportClass1Search", "SupportClass1")';
$('#SupportClass1DataTable').DataTable({
    "serverSide": true,
    "processing": true,
    "ajax": url,
    "ordering": true,
    "dom": '<"top"prl<"clear">>t<"bottom">pi<"clear">',
    "pageLength": 10,
    "autoWidth": false,
    "columns": [
        { // create a link column using the value of the column as the link text
            "data": "SupportClass1Id",
            "width": "20%",
            "render": function (oObj) { return "<a href='#' onclick='editItem(\"" + oObj + "\")'>" + oObj + "</a>"; },
        },
        { "data": "SupportClass1Name", "sWidth": "70%" },
        { // convert boolean values to Yes/No
            "data": "Active",
            "width": "7%",
            "render": function (data, type, full) {
                if (data == true)
                { return 'Yes'; }
                else
                { return 'No'; }
            }
        }
    ]
})

我想根据复选框值过滤第三列(活动)。下面的 JS 可以过滤表格,但是当我输入“是”或“否”时,它没有选择 Active 列:

// use an outside search input
oTable = $('#SupportClass1DataTable').DataTable();

$('#btnSearch').click(function () {
    oTable.search($('#txtSearch').val()).draw();
})

另外,我更喜欢单独搜索 Active 列,有点像这样:

oTable
    .column(2).search('Yes')
    .columns([0,1]).search($('#txtSearch').val())
    .draw();

但这不起作用。任何帮助表示赞赏

【问题讨论】:

标签: jquery search datatables


【解决方案1】:

您可能想要使用 columnfilter 插件 http://jquery-datatables-column-filter.googlecode.com/svn/trunk/index.html(一个 jQuery Datatables 插件),因为它可以完成您所寻求的大部分工作。这是jsFiddle Demo here 中的一个示例。在此示例中,我使用 2 个字段进行文本过滤,第三个是下拉列表

    oTable = $("#myTable").dataTable({
    bInfo: false,
    bSort: false,
    bSortable: false,
        "data": arrayData,
        "columns": [{
        "data": "Emp"
    }, {
        "data": "Name"
    }, {
        "data": "Role"
    }, {
        "data": "Notes"
    }]
}).columnFilter({
    sPlaceHolder : "head:before",
    aoColumns : [{
        type : "text"
    }, {
        type : "text"
    }, {
        type : "select",
        values : arrayRoles
    }]
});

【讨论】:

  • 谢谢。这适用于 DataTables 1.10 版吗?看起来你的语法是 1.9
  • 是的,适用于最新版本。数据表是 1.10.2,它是 jsFiddle 中的引用。 1.10 既有较旧的结构(对象)也有较新的结构 (API)(小“d”与大“D”)
  • 这是一个使用他们网站datatables.net/examples/api/multi_filter.html 上的 API 版本的 URL 示例,可能也有帮助
  • 奇怪的是我什至不能让列搜索在一个列上工作:$('#btnSearch').click(function () { oTable.columns(1).search($ ('#txtSearch').val()).draw(); })
【解决方案2】:

我想通了。使用 1.10 版,您必须使用 ajax.data:

https://datatables.net/reference/option/ajax.data

在我的初始化中,我使用以下内容为我的 ajax 调用添加了一个额外的参数:

"ajax": {
    "url": url,
    "data": function (d) {
        d.activeOnly = $('#activeOnly').is(':checked');
    }
},

这是我的完整初始化:

$(document).ready(function () {
    // initialize the data table
    var url = '@Url.Action("SupportClass1Search", "SupportClass1")';
    $('#SupportClass1DataTable').DataTable({
        "serverSide": true,
        "processing": true,
        "ajax": url,
        "ordering": true,
        "dom": '<"top"prl<"clear">>t<"bottom">pi<"clear">',
        "pageLength": 10,
        "autoWidth": false,
        "ajax": {
            "url": url,
            "data": function (d) {
                d.activeOnly = $('#activeOnly').is(':checked');
            }
        },
        "columns": [
            { // create a link column using the value of the column as the link text
                "data": "SupportClass1Id",
                "width": "20%",
                "render": function (oObj) { return "<a href='#' onclick='editItem(\"" + oObj + "\")'>" + oObj + "</a>"; },
            },
            { "data": "SupportClass1Name", "sWidth": "70%" },
            { // convert boolean values to Yes/No
                "data": "Active",
                "width": "7%",
                "render": function (data, type, full) {
                    if (data == true)
                    { return 'Yes'; }
                    else
                    { return 'No'; }
                }
            }
        ]
    })

    oTable = $('#SupportClass1DataTable').DataTable();

    // this is a checkbox outside the datatable 
    // whose value I wanted to pass back to my controller

    $('#activeOnly').click(function () {
        oTable.search($('#txtSearch').val()).draw();
    })

    $('#btnSearch').click(function () {
        oTable.search($('#txtSearch').val()).draw();
    })
});

我正在使用一个类作为 DataTable 的模型。我还在这里添加了 activeOnly 参数/属性:

/// <summary>
/// this class provides a model to use with JQuery DataTables plugin
/// </summary>
public class jQueryDataTableParamModel
{
    #region DataTable specific properties
    /// <summary>
    /// Request sequence number sent by DataTable,
    /// same value must be returned in response
    /// </summary>       
    public string draw { get; set; }

    /// <summary>
    /// Number of records that should be shown in table
    /// </summary>
    public int length { get; set; }

    /// <summary>
    /// First record that should be shown(used for paging)
    /// </summary>
    public int start { get; set; }
    #endregion

    #region Custom properties

    public bool activeOnly { get; set; }

    #endregion
}

这是我的控制器:

public ActionResult SupportClass1Search(jQueryDataTableParamModel param)
{
    // initialize the datatable from the HTTP request
    var searchString = Request["search[value]"];
    var sortColumnIndex = Convert.ToInt32(Request["order[0][column]"]);
    var sortDirection = Request["order[0][dir]"]; // asc or desc

    // query the database and output to a viewmodel
    var lvm = new SupportClass1SearchViewModel { };
    if (String.IsNullOrEmpty(searchString))
    {
        lvm.SupportClass1List = supportClass1Service.GetAll();
    }
    else
    {
        lvm.SupportClass1List = supportClass1Service.FindBy(t => (t.SupportClass1Name.Contains(searchString))
            && (t.Active.Equals(param.activeOnly) || param.activeOnly == false));
    }

    // do a bunch of stuff and retunr a json string of the data
    return MyJson;
}

现在,当我点击 activeOnly 复选框并重新绘制表格时,将 true 或 false 传递给控制器​​。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-16
    • 2014-09-17
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 2016-07-08
    • 1970-01-01
    相关资源
    最近更新 更多