Solved

Make table rows selectable (radio)?

Posted on 2015-02-08
14
43 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
  • 7
  • 7
14 Comments
 
LVL 15

Expert Comment

by:gplana
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 15

Expert Comment

by:gplana
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 15

Expert Comment

by:gplana
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes, that should do the trick, as this id would be generated by php.
0
 

Author Comment

by:kgp43
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
No, nothing happens.

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

As a result of several questions about how to use Bootstrap I thought it would be a good idea to write down the development aspect of creating a Bootstrapped website in as little time as possible. Part 1 of this article will only concentrate on g…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now