Table row delete using jquery - php

i have made this code to remove table row :
jQuery('tr#rowAttend1').remove();
HTML :
Three rows with same id .
For example :
<tr id="rowAttend1" ><td>ssss</td></tr>
<tr id="rowAttend1" ><td>ddddd</td></tr>
<tr id="rowAttend1" ><td>ccccc</td></tr>
but
i want all three to be removed
it works fine in all browsers except ie7 ?
Can anybody help me?

<tr id="rowAttend1" ><td>ssss</td></tr>
<tr id="rowAttend2" ><td>ddddd</td></tr>
<tr id="rowAttend3" ><td>ccccc</td></tr>
$('#rowAttend1,#rowAttend2,#rowAttend3').remove();
Change the ids to unique ids and then you can remove the table row
Class example
<tr class="rowAttend1" ><td>ssss</td></tr>
<tr class="rowAttend1" ><td>ddddd</td></tr>
<tr class="rowAttend1" ><td>ccccc</td></tr>
$('.rowAttend1').remove();
This will remove all elements with the class rowAttend

Your ID is not unique please use different ID for like,
<tr id="rowAttend1" ><td>ssss</td></tr>
<tr id="rowAttend2" ><td>ddddd</td></tr>
<tr id="rowAttend3" ><td>ccccc</td></tr>
(OR)
Pass particular eventcatch(e) (i.e) (this) to that javascript function and remove from that this like,
$(this).remove();

some different way try if u like
<table>
<tr><td>one</td> <td><button class="removeTR"> one</button></td> </tr>
<tr><td>two</td> <td><button class="removeTR"> two</button></td> </tr>
<tr><td>three</td> <td><button class="removeTR">three</button></td> </tr>
<tr><td>four</td> <td><button class="removeTR"> four</button></td> </tr>
<tr><td>five</td> <td><button class="removeTR"> five</button></td> </tr>
</table>
$("tr .removeTR").live('click', function() {
$(this).parent().parent().remove();
});
or some thing that gives good smooth row deleting
$("tr .removeTR").live('click', function() {
$(this).parent().parent().fadeOut(1000);
setTimeout(function(){$(this).parent().parent().remove();}, 1000);
});

Related

how would i remove some portion of <tr><td> from a table in php?

this is my table -
<table>
<tr>
<td>ABC</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
and I want to remove this one table row:
<tr>
<td> </td>
</tr>
my expected output is:
<table>
<tr>
<td>ABC</td>
</tr>
</table>
is it possible??please help me
As you have tagged your question with the tag php i would recommend using a regular expression.
The pattern \s*<tr>\s*<td> <\/td>\s*<\/tr> will find the tr with an empty ( ) td.
To test and look into the regex you can have a look here: https://regex101.com/r/ax6Xdg/1
Put together this will look something like this:
$table = "<table>
<tr>
<td>ABC</td>
</tr>
<tr>
<td> </td>
</tr>
</table>";
$pattern = "/\s*<tr>\s*<td> <\/td>\s*<\/tr>/";
var_dump( preg_replace( $pattern , "" , $table ) );
This will output something very simmilar to this:
string '<table>
<tr>
<td>ABC</td>
</tr>
</table>' (length=60)
You can do this by using JQuery function .remove(). You can look it up here
Edit: If you want to locate that specific tag, you can do that by using .next()read here, .find() read here,.parent()read here, .children read here
Just add id to your table :
<table id="tableid">
<tr>
<td>ABC</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
This script find , if found then remove !
$('#tableid tr').each(function() {
if ($(this).find('td').html()==' ') $(this).remove();
});
If you want to find some text and then remove then replace html() with text()
$('#tableid tr').each(function() {
if ($(this).find('td').text()=='ABC') $(this).remove();
});
You should try this:
<table>
<tr id="abc>
<td>ABC</td>
</tr>
<tr id="remove">
<td> </td>
</tr>
<script>
$('#remove').remove();
</script>
When rendering the table, add a unique class for the rows you wish to delete. Lets say the class is: _rowToDelete, and then using jQuery, remove all the rows that have this class.
In the below example, when you click on the button the rows are being removed, so you can see the changes. But you can do the same on page load if you wish so.
$(function() {
$("#removeBtn").click(function() {
$("._rowToDelete").remove() ;
});
}) ;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>ABC 1</td>
</tr>
<tr class="_rowToDelete">
<td> </td>
</tr>
<tr>
<td>ABC 2</td>
</tr>
<tr class="_rowToDelete">
<td> </td>
</tr>
<tr>
<td>ABC 3</td>
</tr>
<tr class="_rowToDelete">
<td> </td>
</tr>
</table>
Remove

How to show/hide specific table rows using jQuery

I'm displaying some data in a table and for each row I have another one with some details that is hidden by default and I want to make it visible only when the user clicks a link.
HTML looks like this:
<div id="listing">
<?php for(): ?>
<tr>
<td>Toggle</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr class="details" style="display: none">
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<?php endfor; ?>
</div>
I tried using jQuery like so, but with no results:
$('#listing').on('click', '.toggle', function(e) {
e.preventDefault();
$(this).closest('tr .details').toggle();
});
On each first row element provide it a data-target this will be which class to target and toggle the show/hide.
So basically on click for toggle class we get the data-target use that value to find our target and then simply toggle the target.
JsFiddle : https://jsfiddle.net/63ujphmj/
Javascript
$(function()
{
$('.toggle').on('click', function()
{
var target = $(this).data('target');
$('.'+target).toggle();
});
});
Html
<div id="listing">
<table>
<tr>
<td>Toggle 1</td>
<td>Toggle 2</td>
<td>Toggle 3</td>
<td>Toggle 4</td>
</tr>
<tr class="details">
<td class="details-1" style="display: none">Toggle 1 details</td>
<td class="details-2" style="display: none">Toggle 2 details</td>
<td class="details-3" style="display: none">Toggle 3 details</td>
<td class="details-4" style="display: none">Toggle 4 details</td>
</tr>
</table>
</div>
Assuming you fix the invalid HTML and include <table> tags, you want to use:
$('#listing').on('click', '.toggle', function(e) {
e.preventDefault();
$(this).closest('tr').next('.details').toggle();
});
You need to use .closest('tr') to traverse up in the DOM from the button clicked to the parent row, then .next('.details') to move to the next row element.

jquery datatable sorting not working for run time values

I used jquery data table for sorting. It works fine under normal condition. If i changed the value in run time, the sorting is not working.
This is my table data
<table width="94%" cellpadding="2" cellspacing="1" class="tablehead" id="pls-batting">
<thead>
<tr class="tab_head" align="right" id="pls-play" >
<th width="44" align="center" ># No </th>
</tr>
</thead>
<tbody>
<tr id="116706">
<td align="left" id='1' >test</td>
</tr>
<tr id="116707">
<td align="left" id='2'>bbb</td>
</tr>
<tr id="116708">
<td align="left" id='3' >xxx</td>
</tr>
</tbody>
</table>
Jquery method used for sorting is :
$(document).ready(function() {
$('#pls-batting').dataTable( {
} );
} );
By clicking the '# No' head the corresponding column displayed in asc and desc order respectively. These TD values will be changed onload by using
document.getElementById(3).innerHTML = 'something';
So as a result the 3rd column value is 'something'. So sorting will be done by this values. But it is not working.
It takes the old values. Please help me. Thanks
In DataTables, you should not update content of HTML cell. DT uses internal JavaScript array structure for searching/sorting and this cell is only a display value.
To update some cell you will need to use DT fnUpdate function see http://datatables.net/api#fnUpdate. Example of updating cell in the table is:
var oTable = $('#example').dataTable();
oTable.fnUpdate( 'New content', 10, 3 );
Note that cells are referenced by row/cell positions. If you don't know row/cell position then you can use http://datatables.net/api#fnGetPosition function to find position of TR with id 3 and use this info to update cell data using the fnGetData function (you can find example on the http://datatables.net/api#fnGetPosition)
Jovan

How to highlight multiple items with same name in a dynamic management system

Ok, I have a backend system that watches tables update live (made in php) and i'm looking for a method where I can hover over an ID on one table and all the same IDs across tables would highlight also.
I can't have a page with just their ID as the page includes watching other live site data that needs to be watched.
Seeing the HTML you're using would be helpful, but maybe something like (assume the database IDs are in TDs with a class of 'dbid')
(fair warning I did not have time to test this, but it should work or at least convey the idea)
$('td.dbid').hover(function() {
id = $(this).html();
$("td.dbid:contains('" + id + "')").css('background-color','yellow');
},
function () {
id = $(this).html();
$("td.dbid:contains('" + id + "')").css('background-color','none');
});
Sample HTML
<table>
<tr>
<td class="dbid">14</td>
<td>blah</td>
</tr>
<tr>
<td class="dbid">14</td>
<td>blah</td>
</tr>
<tr>
<td class="dbid">13</td>
<td>blah</td>
</tr>
<tr>
<td class="dbid">14</td>
<td>blah</td>
</tr>
<tr>
<td class="dbid">13</td>
<td>blah</td>
</tr>
</table>

jQuery how to get the class of the table when clicking the tr

How can I get the class of the table when clicking the tr?
This is my code
<table class='table1' border='1'>
<tr class='graph1'>
<td>Test1</td>
<td><a class='viewHistory'>View History<a></td>
</tr>
<tr class='graph2'>
<td>Test2</td>
<td><a class='viewHistory'>View History<a></td>
</tr>
<tr class='graph3'>
<td>Test3</td>
<td><a class='viewHistory'>View History<a></td>
</tr>
</table>
<table class='table2' border='1'>
<tr class='graph1-1'>
<td>Test1</td>
<td><a class='viewHistory'>View History<a></td>
</tr>
<tr class='graph2-1'>
<td>Test2</td>
<td><a class='viewHistory'>View History<a></td>
</tr>
<tr class='graph3-1'>
<td>Test3</td>
<td><a class='viewHistory'>View History<a></td>
</tr>
</table>
When I click on
<tr class='graph2'>
<td>Test2</td>
<td><a class='viewHistory'>View History<a></td>
</tr>
It should display 'table1'
or if I click on
<tr class='graph3-1'>
<td>Test3</td>
<td><a class='viewHistory'>View History<a></td>
</tr>
It should display: 'table2'
Take not that this is dynamic, its not only graph2 or graph3-1 will be clicked.
how would I do that using jquery?
Any help would be greatly appreciated and rewarded! Thanks!
I would use closest() to find the table parent. See also http://api.jquery.com/closest/
$('.viewHistory').click(function() {
var classname = $(this). closest('table').attr('class');
alert(classname);
}
jQuery('table tr').click(function(){
//this is set to the row's DOM element
jQuery(this).parent('table').attr('class');
});
See the jQuery.parent for details.
Never used it personally so not sure about this, but jQuery.closest may give better performance compared to jQuery.parent.
Also, please note that the following code sample will attach click event for any table row in the document, so you'd better add some specific class to your tables (say clickable_table and use it like this)
jQuery('.clickable_table tr').click(function(){
//this is set to the row's DOM element
jQuery(this).parent('table').attr('class');
});
You can use the .parent() function (if you want to click the a tag, use it twice)
and the .attr("class") to get the class. I think there is also a .class or something but not sure right now.

Categories