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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

@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.
@jsang for some reason it does not work on my project. <script type="text/javascript" src="ajax.googleapis.com/ajax/libs/jQuery/1.2.6/jQuery.min.js"> </script> <script type="text/javascript"> $(document).ready(function () { $("#ddmodel").change(function () { var selVal = $(this).text(); alert(selVal); var rows = $("#gvtop tr:gt(0)"); var rowToShow = rows.find("td:eq(3)").filter(":contains(" + selVal + ")").closest("tr"); rows.show().not(rowToShow).hide(); }); });</script>
@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? <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
it's too long to be pasted in the comments, i posted here thanks stackoverflow.com/questions/6069435/…
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 :-)

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.