Anshu
Anshu

Reputation: 616

Filter json with select box option value using jquery

How can i filter the "Make" values according to the year. For eg: When i select "Year" as "2012" the related data i.e; only "def" should be displayed in the drop down box. Another problem is the previous data in the table was shown again when i select next values. See the demo for better understanding.

I tried this.

$.each(g_Vehicle, function(index) {
 var sMake = g_Vehicle[index].Make;
  if($('#DropDown_Year').val())=='2011'){  /* for testing purpose i give year as 2011*/
   if ($.inArray(sMake, g_MakesArray) == -1) {
    g_MakesArray.push(sMake);
        } 
 }
});

DEMO

Upvotes: 1

Views: 1156

Answers (3)

James
James

Reputation: 117

I would use grep for this

e.g.

var newArray = jQuery.grep(g_Vehicle , function(element, index){
  return element.Make == "abc" ; 
});

Upvotes: 0

BalaKrishnan웃
BalaKrishnan웃

Reputation: 4557

To filter a collection/array use the $.grep, to know more about this take a look at the jqfaq.com article, this will explain more on this.

Upvotes: 3

rock-ass
rock-ass

Reputation: 475

function filter() {
    switch($(this).text()) {
    case "2012":
        //filter logic
        $("#dropdown2").html("<select>....</select><select>...</select>");
        break;
    ....
    }
}

$("#dropdown").change(filter);

You can make the second drop box empty when start and fill it with appropriate selects after someone selects year.

Upvotes: 0

Related Questions