[英]Filter JQuery list by name, price… json array, JQuery.grep()
我剛剛對我之前的問題得到了驚人的反饋,但現在我又被困住了。
我有一個 JQuery 列表視圖,顯示我的 json 數組。 它工作正常。 但現在,我希望能夠按名稱或價格過濾我的結果。 我查找了 jQuery.grep() 方法,但無法真正讓它正常工作。
如果有人能就我如何讓它工作給出一些指示,我將再次不勝感激。
這是我到目前為止的代碼:
.js 文件:
// Json array var productList = {"products": [ {"description": "Product 1", "price": "9.99$"}, {"description": "Product 2", "price": "9.97$"}, {"description": "Product 3", "price": "8.52$"}, {"description": "Product 4", "price": "5.24$"}, {"description": "Product 5", "price": "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 file :
<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()應該做你正在尋找的,我認為。
值得一試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.