views:

28

answers:

2

I use a foreach loop inside ASP.NET MVC View page. For each element of the collection that foreach operates on I create two rows - one for display, one for edit. I want to hide the edit row and only display it later depending on user action.

If I hide the edit rows with display: none, then jQuery's show() method cannot redisplay it again - it doesn't work. If I hide it like this

// I put this inside the foreach loop
<script type="text/javascript">
$("#edititem_" + <%: item.Id %>).hide();
</script>

jQuery's show() function can display it later but the page does not validate because this is inside <tbody> tag (this is where I enumerate my collection and create <tr>'s)

I want to be able to show/hide edit rows on demand and still have a XHTML valid page.

How can I achieve that?

A: 

use a CSS file and add a Class to the edit-row and another class to the showing row.

if you write

.editRows {
  display:none;
}

in the CSS file, the .show() of jquery works.

or you could do

$(".editRows").hide();
Stefanvds
hey, thanks, this actually works but if I had an inline style (which I had until now) it didn't..
mare
+1  A: 

To have graceful degredation as well, you could stick with the hiding done via jQuery, just give the edit <tr> rows a class, e.g. <tr class="edit"> then hide all of those at once with jQuery outside the table, like this:

<script type="text/javascript">
  $(function() {
    $("tr.edit").hide();
  });
</script>

This approach, instead of hiding them off the bat with CSS is more friendly to those with JS disabled, if you might have an audience of users like that, go with this .hide() approach.

Nick Craver
The only downside on hiding them with jQuery is if you have a BIG table, the users will see both the displaying and the editing rows while the document is loading. Hiding in CSS will hide it immediately (when the CSS file is cached, what usually is the case)
Stefanvds
@Stefanvds - You could also stick the script not in a `document.ready` immediately after the table if this is a concern.
Nick Craver
how do you mean? how would that look like?
Stefanvds
@Stefanvds - Just put `<script type="text/javascript">$("tr.edit").hide();</script>` immediately after the `</table>` :)
Nick Craver
starred this for future reference...;)
mare
oh, that way, i see. but if the table is like 2000 records of that size that still would not fix it :) For big tables I prefer CSS, It's also cleaner than putting a script under the table, and works even better. why would you not use CSS? :)
Stefanvds
@Stefanvds - For the exact reason I mentioned, you've broken users with JS disabled, your solution does *not* have graceful degradation. Also you don't know the environment the author is in, *typically* a table of 2000+ rows (your example) is an intranet style application, which denotes much more bandwidth and an instant html transfer...don't make assumptions either way, it's a very bad practice :) You can play what-if all day on every question...graceful degradation though is a certainty, and your answer doesn't have or mention it :)
Nick Craver
ok, now i got your point. good thinking. I do guess you get up and go to sleep with jQuery :D when I develop I never really care about people without JS, because in my case there are none. Another day, Another lesson :)
Stefanvds