使用选择菜单进行列过滤(DataTables)

时间:2012-10-26 20:24:00

标签: asp.net-mvc-3 datatables filtering

美好的一天!

我正在使用MVC3和jquery DataTables插件。重点是使用选择菜单进行列过滤(进一步多选)。

这是我的JS,它非常类似于DataTables example

 (function ($) {
    $.fn.dataTableExt.oApi.fnGetColumnData = function (oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty) {
        // check that we have a column id
        if (typeof iColumn == "undefined") return new Array();

        // by default we only want unique data
        if (typeof bUnique == "undefined") bUnique = true;

        // by default we do want to only look at filtered data
        if (typeof bFiltered == "undefined") bFiltered = true;

        // by default we do not want to include empty values
        if (typeof bIgnoreEmpty == "undefined") bIgnoreEmpty = true;

        // list of rows which we're going to loop through
        var aiRows;

        // use only filtered rows
        if (bFiltered == true) aiRows = oSettings.aiDisplay;
        // use all rows
        else aiRows = oSettings.aiDisplayMaster; // all row numbers

        // set up data array   
        var asResultData = new Array();

        for (var i = 0, c = aiRows.length; i < c; i++) {
            iRow = aiRows[i];
            var aData = this.fnGetData(iRow);
            var sValue = aData[iColumn];

            // ignore empty values?
            if (bIgnoreEmpty == true && sValue.length == 0) continue;

            // ignore unique values?
            else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;

            // else push the value onto the result data array
            else asResultData.push(sValue);
        }

        return asResultData;
    } 
} (jQuery));

function fnCreateSelect(aData) {
    var r = '<select><option value=""></option>', i, iLen = aData.length;
    for (i = 0; i < iLen; i++) {
        r += '<option value="' + aData[i] + '">' + aData[i] + '</option>';
    }
    return r + '</select>';
}
$(document).ready(function () {
    var oTable = $('#dataTable').dataTable({
        "sDom": 'W<"clear">lfrtip',
        "sAjaxSource": '@Url.Action("ResourcesWorkflowData", "LineManager")',
        "aoColumns": [
  { "sTitle": "User", "mData": "User" },
  { "sTitle": "Region", "mData": "Region" },
    ]
 });
    /* Add a select menu for each TH element in the table footer */
    $("tfoot th").each(function (i) {
        this.innerHTML = fnCreateSelect(oTable.fnGetColumnData(i));
        $('select', this).change(function () {
            oTable.fnFilter($(this).val(), i);
        });
    });
});

问题出在我的aaData中。如果我传递这样的数组数组:

  "aaData": [
  ['User1', 'Central'],
  ['User2', 'Central'],
]

一切都很顺利,但如果我制作这样的对象数组:

       "aaData": [
  {
      "User": "User1",
      "Region": "Central",
  },
  {
      "User": "User2",
      "Region": "Central",
  }
]

我在这行js中得到“无法读取未定义的属性'长度”错误:

       // ignore empty values?
            if (bIgnoreEmpty == true && sValue.length == 0) continue;

为什么会这样?我试图使用像ColumnFilterColumnFilterWidgets这样的添加项,但在这两种情况下我都遇到了一些问题。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

看起来sValue为null,这意味着Json中的一个字段为null。你能粘贴完整的Json吗?

这种问题存在小问题。

为了确保非字段为空,您可以编写如下简单的内容:

 User = SomeObject.User != null ? SomeObject.User : " ";

这应该可以解决您的问题,因为价值不再为空。