I have a table with a double click event on every each of the rows. When I double click on any row, it will display another row called ".matching". Besides that, I also want to create a column rowspan if a hidden ".matching" row is displayed. Below shows my php part:
<table>
<tr ondblclick="rowdoubleclick(this);">
<td>...</td>
<td>...</td>
<td class="lastcolumn">Rowspan is here</td>
</tr>
<tr class="matching" style="display:hidden;">
<td colspan="2">Hidden row shows here</td>
</tr>
</table>
And this is my jquery:
function rowdoubleclick(e) {
var tablerow = $(e).closest('tr');
var rowammend = $('.lastcolumn');
var matching = $(e).closest('tr').next('tr.matching');
if(matching.is(":hidden")) {
tablerow.attr('rowspan','2').css({'border-bottom':'transparent'});
matching.slideToggle("fast");
rowammend.attr('rowspan','2');
}
else {
tablerow.css({'border-bottom':'1px solid #939393'});
matching.hide();
rowammend.attr('rowspan','1');
}
}
The problem with this code, when i double click on any row, it creates the rowspan on another row. Or in another word, it create all rowspan to all of the rows in the table.
How to add particular column rowspan to that particular double click row event?
Try this, You should find lastcolumn of clicked row
function rowdoubleclick(e) {
var tablerow = $(e).closest('tr');
// here you should find from clicked row and not from each row
var rowammend = $(e).find('.lastcolumn');
var matching = $(e).closest('tr').next('tr.matching');
if(matching.is(":hidden")) {
tablerow.attr('rowspan','2').css({'border-bottom':'transparent'});
matching.slideToggle("fast");
rowammend.attr('rowspan','2');
}
else {
tablerow.css({'border-bottom':'1px solid #939393'});
matching.hide();
rowammend.attr('rowspan','1');
}
}
Related
my for loop is creating this at run time using jquery
<tr id="auctionLocation_0"></tr>
<tr id="auctionLocation_0"></tr>
<tr id="auctionLocation_0"></tr>
<tr id="auctionLocation_1"></tr>
<tr id="auctionLocation_1"></tr>
<tr id="auctionLocation_1"></tr>
and I'm removing it using jquery each loop and my code is
function testing() {
var current = 0;
$("#user").each(function() {
$("#user").find("tr[id='auctionLocation_"+current+"']").remove();
current++;
});
}
OR
function testing() {
$("#user").each(function(i, v) {
$("#user").find("tr[id='auctionLocation_"+i+"']").remove();
});
}
its only remove 0 index not 1 index so how I could do it?
thats my creating code
var counterForLocationsField = 1;
var auctionCompanyLocationArray = data.auctionCompanyLocationArray;
for(var auctionCompanyLocationLoop = 0; auctionCompanyLocationLoop < auctionCompanyLocationArray.length; auctionCompanyLocationLoop++) {
var completeTr = "<tr id='auctionLocation_"+auctionCompanyLocationLoop+"'><td>Location Name "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationName"]+"</td> <td>Location Address "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationAddress"]+"</td></tr>";
completeTr += "<tr id='auctionLocation_"+auctionCompanyLocationLoop+"'><td>Location City "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationCity"]+"</td> <td>Location State "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationState"]+"</td></tr>";
completeTr += "<tr id='auctionLocation_"+auctionCompanyLocationLoop+"'><td>Location Zipcode "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationZipcode"]+"</td> <td>Location Phone "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationPhone"]+"</td></tr>";
$(completeTr).insertBefore("#lastRow");
counterForLocationsField++;
}
As you select the id user, the loop only loops once. Ids are unique.
You can use the starts with selector.
$("tr[id^='auctionLocation_']").remove();
No loop needed. If you want to loop use $("tr[id^='auctionLocation_']").each().
Do not use same ids for different elements. Id must be unique in the page.
Use class instead, like:
<tr class="auctionLocation_0"></tr>
<tr class="auctionLocation_0"></tr>
<tr class="auctionLocation_0"></tr>
<tr class="auctionLocation_1"></tr>
<tr class="auctionLocation_1"></tr>
<tr class="auctionLocation_1"></tr>
And when you want to remove them, just do the above, without loops:
$('tr.auctionLocation_0').remove()
I have php code that creates a table. For each item on the purchase order a new row is created and populated. The goal is when the purchase order is edited it gets posted back and saved in the database. Everything is working except my jQuery code will only iterate over the first row for however many rows I have. (So if I have three rows it will print the first rows cartid three times.)
Sorry if things look messy.
PHP Code:
<?php while($row = mysql_fetch_array($itemsquery)){?>
<tr class="item-row" id="<?php echo $row['cart_id']?>">
<td class="item-name"><div class="delete-wpr"><textarea><?php echo $row['on0'];?> <?php echo $row['itemname'];?></textarea><a class="delete" href="javascript:;" title="Remove row">X</a></div></td>
<td class="description"><textarea class="cartid"><?php echo $row['cart_id'];?></textarea></td>
<td><textarea class="cost" class="text-input" type="text"><?php echo $row['itemprice'];?></textarea></td>
<td><textarea class="qty" type="text" name="name"><?php echo $row['quantity'];?></textarea></td>
<td><span class="price" type="text">$<?php
$linecost = $row['quantity'] * $row['itemprice'];
echo $linecost;?></span></td>
</tr>
<?php }?>
jQuery statement to see what it has grabbed:
$(document).ready(function(){
$(".item-row").change(function() {
$((".cartid")).each(function(){
row = {
cartid: $(".cartid").val()
}
alert(row.cartid);
performAjaxSubmission();
})
});
It looks like you just have your selectors mixed up. You are wanting to iterate over each of the rows, but your selector is referencing $(".cartid"), which would always return the first result ever time.
This should fix your problem:
$(document).ready(function(){
$(".item-row").change(function() {
$(".item-row").each(function(){
var rowContext = $(this);
var row = {
cartid: $(".cartid", rowContext).val()
}
alert(row.cartid);
performAjaxSubmission();
});
});
You have extra () in your .cartid selector.
$(document).ready(function(){
$(".item-row").change(function() {
$(".cartid").each(function(){
row = {
cartid: $(".cartid").val()
}
alert(row.cartid);
performAjaxSubmission();
})
});
Also, shoudn't you be looping trough .item-row instead of .cartid?
You're iterating like this:
$(".cartid").each(function(){
row = {
cartid: $(".cartid").val()
}
Seems like you should be doing
$(".cartid").each(function(){
row = {
cartid: $(this).val()
}
Haven't checked the rest of the code.
EDIT:
$(".cartid").each(function(){
var cart = $(this);
row = {
cartid: cart.val()
}
I have a table whose td values are displayed as
<table>
<tr>
<td><?=$something?></td>
<td><?=$something?></td>
<td><?=$something?></td>
<td><?=$something?></td>
</tr>
<tr>
<td><?=$somethingelse?></td>
<td><?=$somethingelse?></td>
<td><?=$somethingelse?></td>
<td><?=$somethingelse?></td>
</tr>
I would like to apply jquery for all td in the second row after some condition is met, for example, the first somethingelse in the first td is smaller than zero.
You could do something like:
if(yourCondition) {
$('table > tbody > tr:eq(2) > td').each(function() {
$(this).css("color", "red"); //add color red for example
});
}
Do you mean something like that..
$('tr:nth-child(2) td') this will select tds of 2nd row
This will do it for you.
//get the second table row (eq is zero-indexed)
var second_tr = $('tr').eq(1);
//get the contents of the first element in second row
var first_td_val = second_tr.find('td:first').text();
if(first_td_val < 0){
//do something with the second table row.
second_tr.css('background-color', '#bada55');
}
See This jsFiddle
I have a table created with PHP from an Sql Database that successfully displays the table information
using the following code example.
<table class="mytable loottable" id="guildmembers">
<?php
$query = mysql_query("SELECT $member.charname, $member.char_lvl, $trskill.skill_desc, .....
if (mysql_num_rows($query) > 0) {
while (false !== ($row = mysql_fetch_assoc($query))) {
echo '<tr id="'.$row['charname'].'">'; <-- setting row id here
echo '<td class="lootrow" id="memth1">'.$row['charname'].'</td>';
echo '<td class="lootrow" id="memth2">'.$row['rank_desc'].'</td>';
echo '<td class="lootrow" id="memth3">'.$row['class_name'].'</td>';
echo '<td class="lootrow" id="memth4">'.$row['char_lvl'].'</td>';
echo"</tr>\n";
}
mysql_free_result($query);
}
?>
</table>
Example of page source where i have verified that each table row is assigned the correct id.
<tr id="Calysta"><td class="lootrow" id="memth1">Calysta</td><td class="lootrow" id="memth2">Guild Leader</td><td class="lootrow" id="memth3">Inquisitor</td></tr>
<tr id="Rynanx"><td class="lootrow" id="memth1">Rynanx</td><td class="lootrow" id="memth2">Guild Leader</td><td class="lootrow" id="memth3">Mystic</td>
I am setting each table ROW ID to the first field which is always unique. Then i am trying to use this
ID in the following function to redirect users to a profile page when they click on a row in the table.
<script type="text/javascript">
$(document).ready(function () {
$("table.mytable").each( function() {
table = $(this);
if ($(table).attr("id")=="guildmembers") {
$(this).click(function(){
window.location.href = "http://eq2players.com"+
"/Kithicor/" + $(this).attr("id") + "/";
});
}
})
});
</script>
Which should open a new page in the browser with the following address for instance if the
first row is clicked : "http://everquest2.com/Kithicor/Calysta/"
Instead no matter which row is clicked the web url being created
is "http://everquest2.com/Kithicor/guildmembers/"
which is the Tables ID. not the ID of the row being clicked. I have tried various solutions
and cannot figure how to get the row ID in this click function. Any help with this would
much appreciated. Thanks.
Here you go, just did this quick, changed some things, please be mindful of using duplicate id's its BAD, you shouldn't. Also you over complicated your Javascript:
HTML EXAMPLE:
<table class="mytable loottable" id="guildmembers">
<tr class="linkRow" id="test1">
<td>test1</td>
</tr>
<tr class="linkRow" id="test2">
<td>test2</td>
</tr>
<tr class="linkRow" id="test3">
<td>test3</td>
</tr>
</table>
jQuery:
$(document).ready(function () {
$(".linkRow").each( function() {
id = $(this).attr("id")
$(this).click(function(){
window.location.href = "http://eq2players.com"+
"/Kithicor/" + id + "/";
});
});
});
jsFiddle Link:
http://jsfiddle.net/TJBX5/
You are installing your click handler on the table. Therefore, when you call $(this).attr("id"), you are asking for the table's id. You need to install the click handler on each row in the table.
I have a table of data from mysql rendered on page via PHP into a HTML table.
Within this table of data, I have a row of data that should be focussed on (let's call it) row X.
I want the 2 rows above and below row X to be shown but all others hidden, as row X moves up and down, this would change (obviously) what was hidden, when row X is at the top/bottom I want to show 4 rows below/above.
I have done this with static content and JQuery, I am just unsure how to track row X and then apply the class names as required
I thought this was an interesting request so I threw up an example here. The interesting part is the selectors to select the siblings to display. Here is a function i wrote.
function rowXMoved()
{
// hide all rows besides rowX
$('.tableCSS tr:not(.rowX)').hide();
if($('.rowX').prev('tr').size() == 0)
{
// we are row number 1, show 4 more
$('.rowX').siblings('tr:lt(4)').show(); //:lt is less than(index)
}
else if($('.rowX').next('tr').size() == 0)
{
// we are the last row
// find the index of the tableRow to show.
var rowCount = $('.tableCSS tr').size();
$('.rowX').siblings('tr:gt(' + (rowCount - 6) +')').show(); //:gt is greater than(index)
}
else
{
// show 2 rows before and after the rowX
// there is probably a better way, but this is the most straight forward
$('.rowX').prev('tr').show().prev('tr').show();
$('.rowX').next('tr').show().next('tr').show();
}
}
You can show hide the normal way and based on the current row in focus change the innerHtml of the div in focus.
Lets say there are 4 divs holding four rows of data then if focus is on div 2 then it will contain row 2 data in inner html. As focus move or onchange the content in div 2 will keep changing based on which row is in focus. I hope the drift helps
You could give each row a class name, and set a click event handler. When the user clicks for the first time, hide the entire table except for the clicked row and four below if row < 4, four above if row > row.last-4, or two above and two below (if neither of the foregoing is true).
Basically it's dom manipulation so I'd take a look at the prev() and next() functions if I were you. You can get the number of rows in the table by doing, for example, $("table > tr").length.
Noah
Okay I have wrote an example that illustrates selecting different rows. Enter a number into the box (1 - 10) and click the button. Rows 1 or 10 will be shown (here you will change your class with jQuery or whatever) with one row above or below. Selecting other numbers (2 - 9) will show its self, and show one row above and one below.
Obvously this isn't exactly what you asked for - but it should illustrate the logic of how this can be done...
Enter row:
<input id="Text1" type="text" />
<input id="Button1" type="button" value="button" onClick="updateTable()"/>
<!-- Example table, note the Ids -->
<table id="yourTable">
<tr><td id="row1">Row 1</td></tr>
<tr><td id="row2">Row 2</td></tr>
<tr><td id="row3">Row 3</td></tr>
<tr><td id="row4">Row 4</td></tr>
<tr><td id="row5">Row 5</td></tr>
<tr><td id="row6">Row 6</td></tr>
<tr><td id="row7">Row 7</td></tr>
<tr><td id="row8">Row 8</td></tr>
<tr><td id="row9">Row 9</td></tr>
<tr><td id="row10">Row 10</td></tr>
</table>
<script type="text/javascript">
function updateTable()
{
var table = document.getElementById('yourTable');
var row = parseInt(document.getElementById('Text1').value);
var rows = table.rows.length;
// Reset the classes, styles etc etc for each row
for (var i = 0; i < rows; i++) {
table.rows[i].style.visibility = 'hidden';
}
// Subtract one as we start from 0.
row = row - 1;
// Top row, select the first and one below.
if (row == 0) {
table.rows[0].style.visibility = 'visible';
table.rows[1].style.visibility = 'visible';
}
// Rows in between. Select the middle, one above and one below.
if ((row > 0) && (row < rows - 1)) {
table.rows[row - 1].style.visibility = 'visible';
table.rows[row].style.visibility = 'visible';
table.rows[parseInt(row + 1)].style.visibility = 'visible';
}
// Bottom row, select the last row and one above that.
if (row == rows - 1) {
table.rows[row].style.visibility = 'visible';
table.rows[row - 1].style.visibility = 'visible';
}
}
</script>