I am trying to ajax delete rows in a table. It is working, except for the fact that it deletes the ENTIRE table... There is a table within the table the code is as follows;
$(document).ready(function() {
$(".delete").bind('click', function(e){
e.preventDefault();
var parent = $(this).parents('tr');
var string = 'id='+ parent.attr('id').replace('record-','') ;
$.ajax({
type: 'POST',
url: 'ajax_delete.php',
data: string,
beforeSend: function() {
parent.animate({'backgroundColor':'#fb6c6c'},300);
},
success: function(data) {
if(data.status == 'success'){
parent.slideUp(300,function() {
parent.remove();
});
}else if(data.status == 'error'){
parent.animate({'backgroundColor':'#eeeeee'},300);
} else {
parent.animate({'backgroundColor':'#eeeeee'},300);
}
},
});
});
});
The table layout;
<table class="tab_var1">
<tr>
<td class="tab_var1_pad">
<div class="v_tables">
<table>
<tr>
<td>
Domain Name
</td>
<td>
Stats Overview
</td>
<td>
Remove from CRON
</td>
</tr>
<tr class="record" id="record-1">
<td >
<a href="http://www.challengept.com" target="_blank">challengept.com<a>
</td>
<td >
<table >
<tr>
<td>
Traffic (Month)
</td>
<td>
No. Keywords
</td>
<td>
No. PPC Ads
</td>
<td>
Visibility
</td>
</tr>
<tr>
<td>
1796
</td>
<td>
367
</td>
<td>
0
</td>
<td>
0.0236900
</td>
</tr>
<tr>
<td>
Data Source - UK
</td>
</tr>
</table>
</td>
<td >
Remove
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
If anyone can let me know how I can select a single TR within the ajax that would be a great help. The TR I wish to select is the tr with the class "record" which has an a href button in the last td. I am using select parents tr?
You can use closest for this:
var parent = $(this).closest('tr');
var string = 'id='+ parent.attr('id').replace('record-','') ;
I think you can do it with better way as following:
1. Assign data attr for id field
Remove
2. Get data id
var string = $(this).data('id');
.parents() returns all the parents of the element. You just need to select the immediate parent or closest ancestor tr. Use the following jquery method
var parent = $(this).closest('tr');
See links
https://www.w3schools.com/jquery/traversing_parents.asp
Jquery row selecting in a button onClick
Related
i need some help please i spent forever just searching and trying to know why nothing is happening when i click that button
html:
<td></td>
<td></td>
<td></td>
<td><button id="<?php echo($row['ID']); ?>" onClick="delord()" class="del" style="font-size: 12">delete</button></td>
jquery:
function delord(){
var x = event.target.id;
$.ajax({
url: 'delorder.php?id=' + x,
success: function(){
alert('deleted');
}
});
}
i tried to type alert(x); inside my jquery code and it returned the value
then i tried to go to "delorder.php?id=335" and the row has deleted successfully
just when i try it with ajax its not working
A cross platoform way is to pass the event object in the onclick method like this
onclick="delord(event)"
then register the function as
function delord(e){
e = e || window.event;
var target = e.target;
var id = target.getAttribute('id')
}
e will be the event and then your can grab the target element button
Do these
<table>
<thead>
<tr>
<td>Name</td>
<td></td>
</tr>
</thead>
<tbody>
<tr>
<td>Stephen</td>
<td><button onclick="deleteRow('delete',<?php echo($row['ID']); ?>)">Delete</button></td>
</tr>
</tbody>
</table>
jQuery:
function deleteRow(action,id){
$.ajax({
url: 'delorder.php?id=' + id,
success: function(){
alert('deleted');
}
});
}
I am looking for solution to below issue. I have dynamically created rows in table and they can have similar ids based on part number. I want some simple solution to doing simple math operations with numbers inside table. My table have one Q'ty field which is left for user to fill in, rest should be calculated automatically. Row math and column sums. I am beginner so this is what i came up with.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("[id*='kolicina_']").keyup(function(){
var a = $("[id*='cena_']").text();
var b = $("[id*='kolicina_']").val();
var c = a * b;
var d = c - b;
$("[id*='ukupno_']").html(c);
$("[id*='razlika_']").html(d);
});
});
</script>
</head>
<body>
<table>
<tr>
<td>Price</td>
<td>Q'ty</td>
<td>TTL</td>
<td>Difference</td>
</tr>
<tr>
<td id="cena_1234567">2</td>
<td ><input type="text" id="kolicina_1234567"/></td>
<td id="ukupno_1234567"></td>
<td id="razlika_1234567"></td>
</tr>
<tr>
<td id="cena_223421">10</td>
<td ><input type="text" id="kolicina_223421"/></td>
<td id="ukupno_223421"></td>
<td id="razlika_223421"></td>
</tr>
<tr>
<td id="cena_556677">200</td>
<td ><input type="text" id="kolicina_556677"/></td>
<td id="ukupno_556677"></td>
<td id="razlika_556677"></td>
</tr>
<tr>
<td>TTL</td>
<td>TTL</td>
<td>TTL</td>
<td>TTL</td>
</tr>
</table>
</body>
</html>
Note : From your question i think that you want to update the td values only for a single row (means based on the value which you are entering in 2nd column you want to update the calculated values in 3rd and 4th row).
Please check below code
$(document).ready(function(){
$("[id*='kolicina_']").keyup(function(){
var currentRow = $(this).parent().parent();
var td_cena = currentRow.find('td')[0];
var a = $(td_cena).text();
var b = $(this).val();
var c = a * b;
var d = c - b;
var td_ukupno = currentRow.find('td')[2];
var td_razlika = currentRow.find('td')[3];
$(td_ukupno).html(c);
$(td_razlika).html(d);
});
});
Please check this Fiddle.
Here you need to use id starts with selector like $("[id^='kolicina_']") which means grab all the DOM elements whose id starts with kolicina_. Then we search the tr object from where the event got originated and refer to associated DOM elements like below:
$(document).ready(function() {
$("[id^='kolicina_']").keyup(function() {
var trObj = $(this).closest("tr");
var a = parseFloat($(trObj).find("[id^='cena_']").text());
var b = parseFloat($(this).val());
var c = a * b;
var d = c - b;
$(trObj).find("[id^='ukupno_']").html(c);
$(trObj).find("[id^='razlika_']").html(d);
});
});
td {
min-width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<tr>
<td>Price</td>
<td>Q'ty</td>
<td>TTL</td>
<td>Difference</td>
</tr>
<tr>
<td id="cena_1234567">2</td>
<td>
<input type="text" id="kolicina_1234567" />
</td>
<td id="ukupno_1234567"></td>
<td id="razlika_1234567"></td>
</tr>
<tr>
<td id="cena_223421">10</td>
<td>
<input type="text" id="kolicina_223421" />
</td>
<td id="ukupno_223421"></td>
<td id="razlika_223421"></td>
</tr>
<tr>
<td id="cena_556677">200</td>
<td>
<input type="text" id="kolicina_556677" />
</td>
<td id="ukupno_556677"></td>
<td id="razlika_556677"></td>
</tr>
<tr>
<td>TTL</td>
<td>TTL</td>
<td>TTL</td>
<td>TTL</td>
</tr>
</table>
Change the js like this
$(document).ready(function(){
$("[id*='kolicina_']").keyup(function(){
var clickid = $(this).attr('id') ;
var main = $(this).attr('id').slice(9);
var a = $('#cena_'+main).text();
var b = $('#kolicina_'+main).val();
var c = a * b;
var d = c - b;
$('#ukupno_'+main).html(c);
$('#razlika_'+main).html(d);
});
});
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.
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());
});
the jquery plugin that im using is this
http://code.google.com/p/jquery-in-place-editor/
if i have a table like this
<table>
<thead>
<tr>
<th>id</th>
<th>first name </th>
<th>last name </th>
</tr>
</thead>
<tbody>
<tr>
<td class="id">1</td>
<td class="fname">sarmen</td>
<td class="lname">mikey</td>
</tr>
<tr>
<td class="id">2</td>
<td class="fname">john</td>
<td class="lname">angelo</td>
</tr>
<tr>
<td class="id">3</td>
<td class="fname">sarmen</td>
<td class="lname">grande</td>
</tr>
</tbody>
</table>
and my js looked something like this
$("td.fname").editInPlace({
url: 'ajax.php',
params: '',
show_buttons: true
});
then lets say i click on the first record to edit it which is fname of sarmen. how can i pass a param that only accociates id 1 ? because if i do a query of lets say
"update tbl_users set fname = '$_POST['update_value']' where fname = '$_POST['original_html']'"
(note: im just showing an example so no need to clean posts if that was bothering you :) )
if i run this query the fname of sarmen will update in two records rather than one. How can i only update to id of 1 being to update only one record.
$("table tr").each(function(i, el) {
var tdId = $(el).find("td.id");
$(this).find("td.fname".editInPlace({
url: 'ajax.php',
params: 'id=' + $(tdId).text(),
show_buttons: true
});
});
you must add to params id from the first cell in row.
$("td.fname").editInPlace({
id = $(this).parents('tr').children('td:first-child').html();
url: 'ajax.php',
params: {id:id},
show_buttons: true
});
and $id = (int)$_POST['id']; in php