再会!
我正在使用 MVC3 和 jquery DataTables 插件。关键是使用选择菜单(进一步多选)进行列过滤。
这是我的 JS,它与这个DataTables 示例非常相似:
(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;
为什么会这样?我试图使用一些附加组件,如ColumnFilter或ColumnFilterWidgets但在这两种情况下我都遇到了一些问题。请问有什么建议吗?