Solved

Make table rows selectable (radio)?

Posted on 2015-02-08
14
44 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

864 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

24 Experts available now in Live!

Get 1:1 Help Now