问题
Here is how I am generating a list of part numbers (from a json response):
// Any given part could be used more than once. Only want each part # to show first occurance.
$.each(data, function(key, val) {
if ($.inArray(val.name, partArray) === -1) {
partArray.push(val.name);
}
});
return partArray;
I am using jQuery DataTables to render my parts list. I have 3 rows that are rendering like this from my loop above:
["1", "2", "3"] // First Row
["4", "5", "6"] // Second Row
["7", "8", "9"] // Third Row
I would like to filter my parts list (show/hide rows based on selected value). To do that, I need to generate an object/array that matches the above. However, If I console.log "partArray", in my sorting method, I get this:
["1", "2", "3", "4", "5", "6", "7", "8", "9"] // Object
["1", "2", "3", "4", "5", "6", "7", "8", "9"] // Object
["1", "2", "3", "4", "5", "6", "7", "8", "9"] // Object
I am running out of ideas on how I can split up the array to match what my table output looks like - so I can filter based on value. Currently, any value will return true because well, all values are available for each row. I am confused as to how I am getting the 3 rows correctly from the each loop, but when I log out the same array, I get 3 rows of every part number.
Thank you for any suggestions!
回答1:
Well, I don't think this is the end-all-be-all answer, but this is working. Here is the js for anyone who comes across this with a similar problem.
Table column def:
{
'aTargets': [9],
'bSortable': true,
'bVisible': true,
'mData': 'partlist',
'mRender': '[, ].number'
}
Filter method:
$.fn.dataTableExt.afnFiltering.push(
function(oSettings, aData, iDataIndex) {
if ($('#showHideByPartNumber').val() === "" || $('#showHideByPartNumber').val() === 'undefined')
{
return true;
}
else
{
if (aData[9].indexOf($('#showHideByPartNumber').val()) > -1) // aData[9] is the column (zero-based).
{
return true;
}
}
return false;
}
);
$('#partsTbl').dataTable().fnDraw();
来源:https://stackoverflow.com/questions/22151897/splitting-up-array-object-to-filter-values