按名称、价格筛选JQuery列表..json数组,JQuery.grep()

Filter JQuery list by name, price... json array, JQuery.grep()

本文关键字:JQuery 数组 json grep 列表 筛选      更新时间:2023-09-26

我刚刚收到了上一个问题的惊人反馈,但现在我又陷入了困境。

我有一个JQuery列表视图,显示我的json数组。它运行良好。但现在,我希望能够按名称或价格过滤我的结果。我查找了jQuery.grep()方法,但无法使其正常工作。

如果有人能给我一些建议,告诉我如何让它发挥作用,我将再次不胜感激。

这是我目前掌握的代码:

js文件:

//Json阵列var productList={"products":[{"description":"Product 1","price":"9.99$"},{"description":"Product 2","price":"9.97$"},{"描述":"产品3","价格":"8.52$"},{"描述":"产品4","价格":"5.24$"},{"描述":"产品5","价格":"4.21$"}]};
function loadList() {
//  var list = document.getElementById('productList');
    var list = $("#productList").listview();
    list.sort();
   $(productList.products).each(function(index){
    $(list).append('<li id="listitem">' + this.description + "  " +
            "    :     " +  this.price + '</li>');
   });
   $(list).listview("refresh");
}<code>

HTML文件:

<html> <head> <title>Product List</title> &meta; <script src="@=site.cfg.resources.url@/test.js"></script> </head> <body onLoad="loadList()"> <div data-role="page"> <div data-role="header" id="header"> <h1>Product List</h1> </div> <div data-role="content" id="content"> <ul id="productList" data-role="listview" data-inset="true" data-theme="c" data-dividertheme="b"> </ul> </div> </div> </body> </html>
我认为,

.filter()应该能满足您的需求。

$.grep()

值得一看。