0

DropDownList items

a
b
c

GridView

X | B | C | D | E
a | 1 | 2 | 3 | 4
b | 2 | 2 | 2 | 2
c | 3 | 3 | 3 | 3

When DropDownList.SelectedItem = a

Hide GridView.Rows = b & c


When DropDownList.SelectedItem = b

Hide GridView.Rows = a & c


and so on


Anyone know the javascript for doing this client-side?

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
Pod Mays
  • 2,563
  • 7
  • 31
  • 44

2 Answers2

2

Assuming the dropdownlist's id is alpha and the gridview's tbl, you can do it this way:

$(document).ready(function(){
  $("#alpha").change(function(){
    var selVal = $(this).find(":selected").text();   
   var rows =   $("#tbl tr:gt(0)");    
    if (selVal == "ALL") {           
       $("#tbl tr").show();          
    }
    else {        
       var rowToShow = rows.find("td:eq(0)").filter(":contains(" + selVal + ")").closest("tr");
    rows.show().not( rowToShow ).hide();
    }
  });   
});

Here is an example in JS BIN

Sang Suantak
  • 5,213
  • 1
  • 27
  • 46
  • @jsang how do you make it show all the rows again? like if there's a dropdownlist.item = "ALL", then reset to show all rows. – Pod Mays May 20 '11 at 07:56
  • @jsang for some reason it does not work on my project. – Pod Mays May 20 '11 at 08:12
  • @jsang it still doesn't work. i think jquery doesn't work on my end. is this the only line i have to add in the markup? `` – Pod Mays May 20 '11 at 08:25
  • it's too long to be pasted in the comments, i posted here thanks http://stackoverflow.com/questions/6069435/jquery-not-working-on-my-asp-page/6069536#6069536 – Pod Mays May 20 '11 at 08:59
0

Sharing a sub-pseudo code below:

Array listFromGridView = [a,b,c];

// get parent dropdownbox and save for future reference
var drop=$("#dropdownBoxID);

drop.bind('click',function(e){ 

// get all element in an array
Array tempList= listFromGridView;

// remove the element which was clicked
// $(this) = the dropdownbox, $(this).val() = selected value
tempList.remove( $(this).val() );

// iterate and hide rows
foreach( element el in tempListView)
// code for hide goes here
});

// Done :-)
seoul
  • 864
  • 1
  • 12
  • 32