2

I tried many different methods but can't seem to work this one out. I have a JSon array which populates my JQuery list. The list displays correctly, but I cant seem to be able to filter it.

I'd like to be able to filter by name or price. I tried the JQuery .Filter method and many others, and they all failed. I'd also like to make it as a link. ( The user clicks sort by name, and it sorts... )

Heres what I have so far, which I was convinced would work.

Any help is greatly appreciated, Thanks !

.js file :

// Json array var productList = {"products": [ {"description": "Product 1", "price": "3.25"}, {"description": "Product 4", "price": "9.97"}, {"description": "Product 3", "price": "4.21"}, {"description": "Product 2", "price": "5.24"}, {"description": "Product 5", "price": "8.52"} ] };

function loadList() {

var list = $("#productList").listview();

// load array into list

$(productList.products).each(function(index) {
    $(list).append('<li id="listitem">' + this.description + "  " +
            "    :     " + this.price + '</li>');

     // sort by price 
     $(productList.products).filter(function ()
     { return parseFloat(this.price) < 11;})

});

$(list).listview("refresh"); }

3 Answers 3

6

How about this for sorting:

var prods = productList.products.sort(function(a, b) {return parseInt(a.price) < parseInt(b.price);});
$.each(prods, function() {
    list.append("<li>" + this.description + " : " + this.price + "</li>");
});

To sort by description, you could use this instead:

var prods = productList.products.sort(function(a, b) {return a.description < b.description;});

If you just want to filter, you could substitute:

var prods = productList.products.filter(function(item) {return parseInt(item.price) < 5;});
Sign up to request clarification or add additional context in comments.

3 Comments

@JFFF - I wouldn't have been quite so blunt if I'd noticed earlier that you said 'this is an assignment I have' :) You should tag such questions with 'homework' and people will help you learn instead of just blurting out the answer.
Drats, I was about to post jsfiddle.net/morrison/CFnry It doesn't use jQuery, so it should be faster, though most likely negligibly.
The fact that you gave me the answer instead of showing me doesn't mean I won't learn. I'm not just gonna sit there and take your answer as my own work. Your answer is setting me in the right direction, so now I can continue working and try to attach the filter to a link or something. Thanks again !
0

This sort of filtering is typically best done on the server-side end of things. Have you tried applying the filtering at the DB or application-levels?

1 Comment

No, and I cant really do so. This is an assigment I have, and I need to filter the lists dynamicaly with a button or a link. But first I'll have to find a way to actually make the filter work...
0

There have been a lot of json path (xpath like) tools popping up for this kind of purpose. Check out http://goessner.net/articles/JsonPath/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.