Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Make table rows selectable (radio)?

Posted on 2015-02-08
14
Medium Priority
?
68 Views
Last Modified: 2015-08-03
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
Comment
Question by:kgp43
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 40597026
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
 

Author Comment

by:kgp43
ID: 40597036
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
 
LVL 15

Expert Comment

by:gplana
ID: 40597055
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:gplana
ID: 40597059
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
 

Author Comment

by:kgp43
ID: 40597102
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
 
LVL 15

Expert Comment

by:gplana
ID: 40597182
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
 

Author Comment

by:kgp43
ID: 40597211
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
 
LVL 15

Expert Comment

by:gplana
ID: 40597230
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
 

Author Comment

by:kgp43
ID: 40597234
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
 

Author Comment

by:kgp43
ID: 40597238
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
 
LVL 15

Expert Comment

by:gplana
ID: 40597269
Yes, that should do the trick, as this id would be generated by php.
0
 

Author Comment

by:kgp43
ID: 40597373
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
 
LVL 15

Accepted Solution

by:
gplana earned 2000 total points
ID: 40597445
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
 

Author Comment

by:kgp43
ID: 40597770
No, nothing happens.

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

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question