I have a table generated by jQuery, and a function that triggers an event when pressing ENTER.
The table generates correctly, but the function only works in the first wor of the table.
My html for the table is as follows:
<table border="1" id="PlantillaTable" name="PlantillaTable">
<tbody>
<tr>
<th rowspan="2" scope="col">Cargo</th>
<th colspan="12" scope="col">Escenario de Registro</th>
</tr>
<tr>
<td colspan="2">Folio</td>
<td colspan="2">Propietario</td>
<td >Grupo</td>
<td >Edad</td>
<td colspan="2">Folio</td>
<td colspan="2">Suplente</td>
<td >Grupo</td>
<td >Edad</td>
</tr>
<tr id="FilaTable">
<th scope="row" col="1" row="1">Cargo</th>
<td col="2" row="1">LISTA</td>
<td col="3" row="1">
<input type="text" id="AspiranteField" name="AspiranteField" placeholder="FOLIO" />
</td>
<td col="4" row="1">FOTO</td>
<td col="5" row="1">NOMBRE</td>
<td col="6" row="1">GRUPO</td>
<td col="7" row="1">EDAD</td>
<td col="8" row="1">LISTA</td>
<td col="9" row="1">
<input type="text" id="AspiranteField" name="AspiranteField" placeholder="FOLIO" />
</td>
<td col="10" row="1">FOTO</td>
<td col="11" row="1">NOMBRE</td>
<td col="12" row="1">GRUPO</td>
<td col="13" row="1">EDAD</td>
</tr>
</tbody>
And my function is as follows:
$.fn.pressEnter = function(fn) {
return this.each(function() {
$(this).bind('enterPress', fn);
$(this).keyup(function(e){
if(e.keyCode == 13)
{
$(this).trigger("enterPress");
}
})
});
};
$('input').pressEnter(function(){
var trid = ($(this).closest('tr').attr('id'));
var opcion = ($(this).closest('td').index());
var valor = $(this).val();
$.getJSON("php/getAspiranteNombre.php?AspiranteId="+valor,function(data){
$.each(data,function(index,item) {
if(opcion < 4)
{
$("#"+trid+" td").eq(3).html('<p>'+item.NAME+'</p>');
$("#"+trid+" td").eq(4).html('<p>'+item.GRUPO+'</p>');
$.getJSON("php/getFoto.php?AspiranteId="+valor,function(data2){
$("#"+trid+" td").eq(2).html('<img src="aspiranteFoto/thumb_'+data2+'" width="50px" height="50px"/>');
});
$.getJSON("php/getEdad.php?Nacimiento="+item.EDAD,function(data3){
$("#"+trid+" td").eq(5).html('<p>'+data3+'</p>');
});
}
else
{
$("#"+trid+" td").eq(9).html('<p>'+item.NAME+'</p>');
$("#"+trid+" td").eq(10).html('<p>'+item.GRUPO+'</p>');
$.getJSON("php/getFoto.php?AspiranteId="+valor,function(data2){
$("#"+trid+" td").eq(8).html('<img src="aspiranteFoto/thumb_'+data2+'" width="50px" height="50px"/>');
});
$.getJSON("php/getEdad.php?Nacimiento="+item.EDAD,function(data3){
$("#"+trid+" td").eq(11).html('<p>'+data3+'</p>');
});
}
});
});
})
Which basically asks for a value in a database and places it in the spaces. The problem is that, in the first row of the table (dynamically generated) the function works perfectly, but in the subsequent rows it doesn't do anything.
Thanks in advance for your help!
You have to change the way to this one:
$("#PlantillaTable").find("input").keyup(function(ev) {
if (ev.which === 13 ) {
//yout code here...
}
});
Attaches to all input values inside this table the enter button handler.
http://jsfiddle.net/csdtesting/jkfL2v5s/
You first have to loop through each tr separately, then you can target the td's inside.
You can loop through the rows using $.each() like this:
$("table tr").each(function(i,v){
$(this).find("td:eq(3)").html('<p>'+item.NAME+'</p>');
....
});
and it will go through all your rows, not just the first one.
Related
I am looking for information on the best approach to creating a 'more details' section on a table generated by a MySQL query which is something like this:
<tr>
<th>First Name</th>
<th>Surname</td>
<th>Details</th>
</tr>
<tr>
<td>Joe</td>
<td>Blogs</td>
<td><a class="button-1" href="">More Details</td>
</tr>
<tr>
<th class="hidden_details hidden-1" colspan="3">...insert details...</th>
</tr>
Each row has a unique id e.g. button-2,3,4 etc that I can use to create a unique class for each details section and I can hide and show the details using JQuery:
$(document).ready(function() {
$('.button-1').click(function(){
var link = $(this);
$('.hidden-1').slideToggle('slow', function() {
if ($(this).is(':visible')) {
link.text('Hide Details');
} else {
link.text('More Details');
}
});
});
});
But my knowledge on JQuery is limited so the only way I could think to call the code would be to use php to create multiple versions. If anyone could point my in the direction of the best approach I would be grateful
check this code. it will help you. here id="view_1",id="show_1" 1 is your dynamic value from database .
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>First Name</th>
<th>Surname</td>
<th>Details</th>
</tr>
<tr>
<td>Joe</td>
<td>Blogs</td>
<td><a class="button-1" id="view_1" href="javascript:void(0)" onclick="show_details(1)">More Details</td>
</tr>
<tr>
<th class="hidden_details hidden-1" colspan="3" id="show_1">...insert details...</th>
</tr>
<tr>
<td>Joe1</td>
<td>Blogs2</td>
<td><a class="button-1" id="view_2" href="javascript:void(0)" onclick="show_details(2)">More Details</td>
</tr>
<tr>
<th class="hidden_details hidden-1" colspan="3" id="show_2">...insert details...</th>
</tr>
</table>
<script type="text/javascript">
function show_details(id) {
if ($('#show_'+id).is(':visible')) {
$('#view_'+id).text('More Details');
$('#show_'+id).hide();
} else {
$('#view_'+id).text('Hide Details');
$('#show_'+id).show();
}
}
</script>
<style type="text/css">
.hidden_details {
display: none;
}
</style>
I have a table with the image and image code data, show on code below:
<table id="tblForklift" cellpadding="5" cellspacing="0">
<tbody>
<tr id="image_info">
<td id="1W 1111">
<img src="../Client/images/forklift/corpus-christi-forklifts1.jpg" width="210px" height="210px"/>
<p style="text-align: center;">1W 1111</p>
</td>
<td id="2W 2222"></td>
<td id="3W 3333"></td>
</tr>
<tr id="image_info"></tr>
<tr id="image_info"></tr>
<tr id="image_info"></tr>
</tbody>
</table>
I tried using this code to get the html of selected td of the table. But it show "undefined".
$('#tblForklift').on('click', function(e) {
var forkliftCode = $('this').closest('tr').find('td').html();
alert(forkliftCode)
});
Since #tblForklift will match your table. You need to target td elements inside this table instead. Also if your elements has been added dynamically to the DOM, you can use event delegation:
$(document).on('click', '#tblForklift tr td', function(e) {
var forkliftCode = $(this).html();
alert(forkliftCode)
});
or better if your table is not added dynamically:
$('#tblForklift').on('click', 'tr td', function(e) {
var forkliftCode = $(this).html();
alert(forkliftCode)
});
Also some of your td are missing closing </td>
Add event to the td. so in each td click you can get html.
$('#tblForklift td').on('click',function(e) {
alert($( this ).html());
});
demo
id must be unique use class like,
HTML
<table id="tblForklift" cellpadding="5" cellspacing="0">
<tbody>
<tr class="image_info">
<td class="1W 1111 forklift">
<img src="../Client/images/forklift/corpus-christi-forklifts1.jpg" width="210px" height="210px"/>
<p style="text-align: center;">1W 1111</p>
</td>
<td class="2W 2222"></td>
<td class="3W 3333"></td>
</tr>
<tr class="image_info"></tr>
<tr class="image_info"></tr>
<tr class="image_info"></tr>
</tbody>
</table>
SCRIPT
$('.forklift').on('click', function(e) { // using class for multiple tds
var forkliftCode = $(this).find('p').text();
alert(forkliftCode);
});
Also, never give a space in a id, space has a different meaning in jquery selectors
Live Demo
HTML:
<td class="2W 2222">cvbcxbcvb</td>
<td class="3W 3333">bcvbcvbnvnv</td>
</tr>
<tr class="image_info"></tr>
<tr class="image_info"></tr>
<tr class="image_info"></tr>
</tbody>
</table>
jQuery:
$('#tblForklift td').click(function() {
alert($(this).html());
});
I am new with java script. The code I have for deleting table row works fine when table already exists in the page but it doesnt work if I load table from another page by ajax call.. please help me to solve this.. java script is here
$(document).ready(function()
{
$('table#delTable td a.delete').click(function()
{
if (confirm("Are you sure you want to delete this row?"))
{
var id = $(this).parent().parent().attr('id');
var data = 'id=' + id ;
var parent = $(this).parent().parent();
$.ajax(
{
type: "POST",
url: "delete_row.php",
data: data,
cache: false,
success: function()
{
parent.fadeOut('slow', function() {$(this).remove();});
}
});
}
});
// style the table with alternate colors
// sets specified color for every odd row
$('table#delTable tr:odd').css('background',' #FFFFFF');
});
html code
<table id="delTable">
<tr id="ripon">
<td align="left">Ipsum</td>
<td align="center">19</td>
<td align="center">17</td>
<td align="center"><img alt="" align="absmiddle" border="0" src="img/delete.png" /></td>
</tr>
<tr id="rukon">
<td align="left">Dolor</td>
<td align="center">55</td>
<td align="center">12</td>
<td align="center"><img alt="" align="absmiddle" border="0" src="img/delete.png" /></td>
</tr>
<tr id="sumon">
<td align="left">Sit</td>
<td align="center">11</td>
<td align="center">18</td>
<td align="center"><img alt="" align="absmiddle" border="0" src="img/delete.png" /></td>
</tr>
<tr id="mamun">
<td align="left">Amet</td>
<td align="center">29</td>
<td align="center">27</td>
<td align="center"><img alt="" align="absmiddle" border="0" src="img/delete.png" /></td>
</tr>
The easiest solution for this is to use "Delegated Events".
For more information, see jQuery's on docs.
$("body").on("click", "table#delTable td a.delete", function(event){
// insert your code here
});
Note: while I called on on body, you can call it on something "closer" to the table, if it's going to be there when the page loads (or more specifically, when you make the on call).
I was wondering if this is possible to be done with jQuery. Please see following code.
HTML
<table id="order">
<tr>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
</tr>
<tr>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
</tr>
<tr>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
<td class="order_items"></td>
</tr>
</table>
javascript
$(document).ready(function(){
$("td.load_ads").each(function(){
$(this).html("Place a New Ad Free!");
});
var count=$("td.load_ads").length; //18
$.post('/fetch_orders.php',{count:count},function(data){
var data_arr=data.split('/end');
for(index=0;index < data_arr.length;index++){ //for each of the array values got back from fetch orders.php
.
..
... Stuck here. How to make each data_arr(ie: each data_arr[index]) go into each td?
.POST script
$count =$_POST['count'];
$query="SELECT * FROM orders1_manager_orders ORDER BY RAND() LIMIT $count";
$db->setQuery($query);
$ads=$db->loadRowList();
foreach($ads as $ads_to_display){
echo $orders_to_display[9].'/';
echo $orders_to_display[10].'/end';
}
As explained in the title, I need each order to go into each empty td slot in the above html table. But, if I were to put a .each() function after my .post, all of my tds will turn into the first data gotten from the script, meaning data_arr[1].
First, please have a look at:
http://php.net/manual/en/function.json-encode.php
http://www.islandsmooth.com/2010/04/send-and-receive-json-data-using-ajax-jquery-and-php/
Second: better use div insted tables, this would make you independent of client device screen size.
http://csswizardry.com/2011/08/building-better-grid-systems/
Third, this is absolutly horrible code but it should be your solution:
$("td.load_ads").each(function(){
$(this).html("Place a New Ad Free!");
});
var count=$("td.load_ads").length; //18
$.post('/fetch_orders.php',{count:count},function(data){
var data_arr = [];
var rows=data.split('/end');
for(index=0;index < rows.length;index++){
var row = rows[i].split('/');
for (var x in row) {
data_arr.push(row[x]);
}
}
var i = 0;
$("td.load_ads").each(function () {
if (data_arr[i]) {
$(this).html(data_arr[i]);
}
i++;
});
}
$query = Sql_Query("SELECT * FROM Table");
$num_rows = mysql_num_rows($query);
for($i=0; $i < $num_rows; $i=$i+5)
{
$query1 = Sql_Query("SELECT * FROM Table LIMIT ".$i.", 5 ");
print '<td valign="top">';
while ($row=mysql_fetch_assoc($query1))
{
print '<input type="checkbox" name="chk_group1[]" id="chk_group1" value="'.$row['isp_name'].'" /> '.$row['isp_name'].'<br />';
}
print '</td>';
}
I have a functional use website (not for public use, just a website that provides a means to an end) in which I have a table that is being populated/updated every 5 seconds through an AJAX call to my database.
What I want to happen is that when I click on a checkbox (which is in one of the cells of my table), it adds a class to that row. except it just does not like the fact the data is coming from AJAX, I have tried putting a sample static table in, and that works fine, but the same information as an AJAX table just does nothing.
I checked this link and that didn't respond to my actions either, the JS code I have provided below is the one I have been using which worked on the static table
JS/AJAX
<script>
function getMessage(){
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
var res = xmlhttp.responseText;
$('#message tr:first').after(res);
}
}
xmlhttp.open("GET","ajax/message.php",true);
xmlhttp.send();
};
</script>
JS Code I have been using to highlight
$(document).ready(function() {
$('#lectern_message tr')
.filter(':has(:checkbox:checked)')
.addClass('selected')
.end()
.click(function(event) {
$(this).toggleClass('viewing');
if (event.target.type !== 'checkbox') {
$(':checkbox', this).attr('checked', function() {
return !this.checked;
});
}
});
});
Example Table through AJAX
<table id="message" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<th>Speaker</th>
<th>Question</th>
<th>Time</th>
<th>View</th>
</tr>
<td class="name">Mr A</td>
<td class="message">Hi</td>
<td class="date">11:14:12</td>
<td class="view"><input type="checkbox" value="no"></td>
</tr>
<tr>
<td class="name">Mr B</td>
<td class="message">Hello</td>
<td class="date">10:32:36</td>
<td class="view"><input type="checkbox" value="no"></td>
</tr>
<tr>
<td class="name">Mr C</td>
<td class="message">Question</td>
<td class="date">10:32:18</td>
<td class="view"><input type="checkbox" value="no"></td>
</tr>
<tr>
<td class="name">Mr D</td>
<td class="message">Hi</td>
<td class="date">10:30:53</td>
<td class="view"><input type="checkbox" value="no"></td>
</tr>
</tbody>
</table>
Sorry for the massive amounts of code, thought I would provide the key parts, the message.php file that is mentioned is just a call to retrieve all the records from my database, but that part of it works fine. If anyone can give me a hand that would be a massive help, many thanks
click() will be bind to the elements which are all present when the time of loading. and note that if you want to use click() for dynamically added elements, you have to go for live() or on() method of jQuery... so you have to change ur code to
$(document).ready(function() {
$('#lectern_message tr')
.filter(':has(:checkbox:checked)')
.addClass('selected')
.end()
.live('click', function(event) {
$(this).toggleClass('viewing');
if (event.target.type !== 'checkbox') {
$(':checkbox', this).attr('checked', function() {
return !this.checked;
});
}
});
});
for more examples pls see here