拆分数组/对象以过滤值

编程入门 行业动态 更新时间:2024-10-06 08:27:40
本文介绍了拆分数组/对象以过滤值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我通过json响应生成零件编号列表的方式:

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;

我正在使用 jQuery DataTables 来呈现我的零件清单.从上面的循环中,我有3行这样渲染:

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

我想过滤我的零件清单(根据选定的值显示/隐藏行).为此,我需要生成一个与上述匹配的对象/数组.但是,如果我用console.log"partArray",在我的排序方法中,我得到的是:

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

我对如何分割数组以使其与表输出看起来匹配的想法不多了-因此我可以根据值进行过滤.当前,任何值都将返回true,因为好吧,每一行都可以使用所有值.我对如何从每个循环中正确获取3行感到困惑,但是当我注销相同的数组时,每个零件编号都会得到3行.

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.

谢谢您的任何建议!

推荐答案

好吧,我不认为这是万事俱备的答案,但这是可行的.这是遇到类似问题的人的js.

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.

表列定义:

{ 'aTargets': [9], 'bSortable': true, 'bVisible': true, 'mData': 'partlist', 'mRender': '[, ].number' }

过滤方法:

$.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();

更多推荐

拆分数组/对象以过滤值

本文发布于:2023-11-30 15:27:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650507.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!