[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 74
  • Last Modified:

Make table rows selectable (radio)?

Hi,

I got a table with data pulled from a database.
My user then select a row matching his requirements and click the submit button.
Entry in database is updated with his selection.

How can I make a row in a table selectable (like a input radio)?
See attached image.

Untitled-1.png
0
kgp43
Asked:
kgp43
  • 7
  • 7
1 Solution
 
gplanaCommented:
You can do this by using JQuery:

$(document).ready(function(){
    $('table tbody tr').click(function(){
        alert($(this).text());
    });
});

Open in new window


Please note that if you have more than one table you should change  table.tbody.tr by #your_table_id.tbody.tr

And also just change the alert by the code you want to execute (probably

And also you should improve the visual part by adding this CSS:

table tbody tr:hover {
    background-color: orange;
    cursor: pointer;
}

Open in new window


Hope it helps. Regards.
0
 
kgp43Author Commented:
Having problems getting that to work :(

mysql query.... while...

<input id="mount_tube_<?php echo $mount_fetch['id']; ?>" name="mount_tube_id" value="<?php echo $mount_fetch['id']; ?>" type="radio">
<label for="mount_tube_<?php echo $mount_fetch['id']; ?>">
	<tr>
		<td><?php echo $mount_fetch['id']; ?></td>
		<td><?php echo $mount_fetch['oP']; ?></td>
		<td><?php echo $mount_fetch['Z']; ?></td>
		<td><?php echo $mount_fetch['S']; ?></td>
		<td><?php echo $mount_fetch['oD']; ?></td>
		<td><?php echo $mount_fetch['S1']; ?></td>
		<td><?php echo $mount_fetch['L']; ?></td>
		<td><?php echo $mount_fetch['FC']; ?></td>
		<td><?php echo $mount_fetch['weight']; ?></td>
		<script>
			$(document).ready(function(){
				$('#mount_tube.tbody.tr').click(function(){
					alert($(this).text());
				});
			});
		</script>
	</tr>
</label>

Open in new window

0
 
gplanaCommented:
What error are you getting?

Have you included jquery library?
If not, please have a look at http://learn.jquery.com/about-jquery/how-jquery-works/
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gplanaCommented:
Also the script should probably be placed on the head html section instead of inside body section. But the most important thing is to include the JQuery library (download and include jquery.js file, as explained on the link I provided.

Hope it helps.
0
 
kgp43Author Commented:
No error, but the radio button is not selected upon table-row click.

jQuery is included in <head>
I moved the script to <head> section as well.
0
 
gplanaCommented:
Is not a radio button, but row can be selected.
You can add a column on the table with an <input type="radio"> if you want...
0
 
kgp43Author Commented:
Think we got misunderstood each other.

How can I make a row in a table selectable (like a input radio)?
- That I meant to say was "How can I check a radion button on a table row click?

I figured it out, but it does not highlight the checked/active table row (td's).
Is it possible to make all "td-labels" change color when checked?

I normally use this to change background color at checked radio buttons/labels, but it does not work here:
input[type=radio]:checked + label {
	background-color:#E5020D;

	color: #fff;
}

Open in new window


.overview_table label {
	width: 100%;
	height: 100%;
	display: block;
}

Open in new window

.
<label for="mount_tube_123"><input id="mount_tube_123" name="mount_tube_id" value="<?php echo $mount_fetch['id']; ?>" type="radio"></label>
<tr>
	<td><label for="mount_tube_123"><div class="standard">standard</div></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['oP']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['Z']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['S']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['oD']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['S1']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['L']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['FC']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['weight']; ?></label></td>
</tr>

Open in new window

0
 
gplanaCommented:
You should do it by code inside JQuery function. So change the alert sentence by this:

$("#mount_tube_123").attr('checked', true);

Where mount_tube_123 is the id of the radio button you want to check ( as I saw on your code).

However, I can see that radio button is outside the row of the table... so I guess you want to add a cell for this radiobutton:

<label for="mount_tube_123"><input id="mount_tube_123" name="mount_tube_id" value="<?php echo $mount_fetch['id']; ?>" type="radio"></label>
<tr>
	<td>
		<label for="mount_tube_123"><input id="mount_tube_123" name="mount_tube_id" value="<?php echo $mount_fetch['id']; ?>" type="radio"></label>
	</td>
	<td><label for="mount_tube_123"><div class="standard">standard</div></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['oP']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['Z']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['S']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['oD']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['S1']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['L']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['FC']; ?></label></td>
	<td><label for="mount_tube_123"><?php echo $mount_fetch['weight']; ?></label></td>
</tr>

Open in new window

0
 
kgp43Author Commented:
I was planning to add 'type="hidden"' to that radio button.
I will try what you said again, see if it works :)

Also, please read my previous reply (I updated it a sec. before you posted your reply).
0
 
kgp43Author Commented:
Hmm... not sure how to change that jQuery.
mount_tube_123 is not "static", but actually this (I just modified it to make the code more "user-friendly":

mount_tube_<?php echo $mount_fetch['id']; ?>

Open in new window

0
 
gplanaCommented:
Yes, that should do the trick, as this id would be generated by php.
0
 
kgp43Author Commented:
Any idea how to make the "input[type=radio]:checked" to work?
It does not highlight the checked row and the jQuery does not work.

input[type=radio]:checked + label {
	background-color:#E5020D;

	color: #fff;
}

Open in new window


<script type="text/javascript">
$(document).ready(function(){
    $('#mount_tube.tbody.tr').click(function(){
        alert($(this).text());
    });
});
</script>

Open in new window


<tr>
	<td><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><input id="mount_tube_<?php echo $mount_fetch['id']; ?>" name="mount_tube_id" value="<?php echo $mount_fetch['id']; ?>" <?php echo $checked; ?> type="radio"></label></td>
	<td style="position:relative;"><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><?php echo $mount_fetch['id']; ?>
												<?php if(isset($checked) && !empty($checked)) echo '<div class="standard">standard</div>';?></label></td>
	<td><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><?php echo $mount_fetch['oP']; ?></label></td>
	<td><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><?php echo $mount_fetch['Z']; ?></label></td>
	<td><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><?php echo $mount_fetch['S']; ?></label></td>
	<td><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><?php echo $mount_fetch['oD']; ?></label></td>
	<td><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><?php echo $mount_fetch['S1']; ?></label></td>
	<td><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><?php echo $mount_fetch['L']; ?></label></td>
	<td><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><?php echo $mount_fetch['FC']; ?></label></td>
	<td><label for="mount_tube_<?php echo $mount_fetch['id']; ?>"><?php echo $mount_fetch['weight']; ?></label></td>
</tr>

Open in new window

0
 
gplanaCommented:
If you put the alert code, does it appear when you click over a row of a table?
If you install firebug and look at the console, does any javascript error appear?
0
 
kgp43Author Commented:
No, nothing happens.

Firebug -> Console -> Errors:
No error when reloading and clicking a table row.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now