Splitting up array/object to filter values

走远了吗. 提交于 2019-12-25 07:40:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!