JavaScript - Js Table Sorter: Sortcol
Code:
<html> <head> <title>Table Sort Example</title> <script type="text/javascript"> function generateCompareTRs(iCol) { return function compareTRs(oTR1, oTR2) { var sValue1 = oTR1.cells[iCol].firstChild.nodeValue; var sValue2 = oTR2.cells[iCol].firstChild.nodeValue; return sValue1.localeCompare(sValue2); }; } function sortTable(sTableID, iCol) { var oTable = document.getElementById(sTableID); var oTBody = oTable.tBodies[0]; var colDataRows = oTBody.rows; var aTRs = new Array; for (var i=0; i < colDataRows.length; i++) { aTRs[i] = colDataRows[i]; } if (oTable.sortCol == iCol) { aTRs.reverse(); } else { aTRs.sort(generateCompareTRs(iCol)); } var oFragment = document.createDocumentFragment(); for (var i=0; i < aTRs.length; i++) { oFragment.appendChild(aTRs[i]); } oTBody.appendChild(oFragment); oTable.sortCol = iCol; } </script> </head> <body> <P>Click on the table header to sort in ascending order.</p> <table border="1" id="tblSort"> <thead> <tr> <th onclick="sortTable('tblSort', 0)" style="cursor:pointer">Last Name</th> <th onclick="sortTable('tblSort', 1)" style="cursor:pointer">First Name</th> </tr> </thead> <tbody> <tr> <td>Smith</td> <td>John</td> </tr> <tr> <td>Johnson</td> <td>Betty</td> </tr> <tr> <td>Henderson</td> <td>Nathan</td> </tr> <tr> <td>Williams</td> <td>James</td> </tr> <tr> <td>Gilliam</td> <td>Michael</td> </tr> <tr> <td>Walker</td> <td>Matthew</td> </tr> </tbody> </table> </body> </html> The only part I don't quite understand is the use of sortCol. Is it just the scripter's created variable? Or a build-in method? How is the logic behind? I bet the coder's using an integer to control the sorting. Could anyone help me a bit? Similar Tutorialshi guys i having some problems with jquery plugin table sorter on a dynamic table. if i use it on a regular html table it works fine,but on my dynamic table its not working. if someone could look at my code and see if i done something wrong i would appreciate it . Thanks will43 Code: <script type="text/javascript" src="table sorter/jquery.tablesorter/jquery-latest.js" ></script> <script type="text/javascript" src="table sorter/jquery.tablesorter/jquery.tablesorter.js" ></script> <script type="text/javascript"/> $(document).ready(function() { $("#myTable").tablesorter(); } ); </script> <form id="form1" name="form1" method="post" action=""> <label for="Position">Position</label> <select name="Position" id="Position"> <option>QB</option> <option>HB</option> <option>FB</option> <option>WR</option> <option>TE</option> <option>RG</option> <option>RT</option> <option>C</option> <option>LG</option> <option>LT</option> <option>CB</option> <option>LB</option> <option>SS</option> <option>FS</option> <option>DE</option> <option>DT</option> <option>P</option> <option>K</option> </select> <input type="submit" name="Summit" id="Summit" value="Submit" /> </form> <p> </p> <p> </p> <table width="950" border="1" cellpadding="3" cellspacing="3" class="tablesorter" id="myTable"> <thead> <tr> <th bgcolor="#FF0000">id</th> <th bgcolor="#FF0000">Team</th> <th bgcolor="#FF0000">FirstName</th> <th bgcolor="#FF0000">LastName</th> <th bgcolor="#FF0000">Position</th> <th bgcolor="#FF0000">Year</th> <th bgcolor="#FF0000">Overall</th> <th bgcolor="#FF0000">Speed</th> </tr> </thead> <?php do { ?> <tbody> <tr> <td bgcolor="#FFFF00"><?php echo $row_Recordset1['id']; ?></td> <td bgcolor="#FFFF00"><?php echo $row_Recordset1['Team']; ?></td> <td bgcolor="#FFFF00"><?php echo $row_Recordset1['FirstName']; ?></td> <td bgcolor="#FFFF00"><?php echo $row_Recordset1['LastName']; ?></td> <td bgcolor="#FFFF00"><?php echo $row_Recordset1['Position']; ?></td> <td bgcolor="#FFFF00"><?php echo $row_Recordset1['Year']; ?></td> <td bgcolor="#FFFF00"><?php echo $row_Recordset1['Overall']; ?></td> <td bgcolor="#FFFF00"><?php echo $row_Recordset1['Speed']; ?></td> </tr> </tbody> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> </table> My issue is that I have a javascript function applied on page load via an addLoadEvent function call in the head to every row (dynamic number of records retrieved) on a table. On every row of the table I also have an element (image or button) that performs a different function that is assigned on the element itself. Unfortunately, when I click the element it also performs the function that was applied to the row in the page load. Is there any way to not call this function on element click? I know usually you include code, but due to the nature of the work I am limited in that respect. Basically its: Code: <script type="text/javascript"> addLoadEvent(function() { function1(1); }) //function1 makes the table rows do something appearance wise. </script> <tr> <td></td> <td></td> <td> <img src="pics/delete.png" alt="Delete" onclick="function2();"> </td> </tr> I apologize if my post doesn't conform to the general guidelines of proper posting on this board. I'm new on this site. Hello all i have to implement simple LIFO algorithm ( if i can call it like this .. ) say i have html table and and i fetching data i like thet the last data that fetched to be the first record in the table for example : im fetching the string "111" so it will be the first record next im fetching the string "222" now the string "111" will be the second and the string "222" will be the first .. and so on hi all, i have a table, just a basic table with 6 rows. depending on the value of the first <td>thevalue</td> i want to start a end the existing table and start a new one. the newly added table (i hope) will contain all the remaining <tr><td></td></tr> nodes until i run into the next check that will close the newly added table and begin a new table again etc.... i can get the correct node (tr) after i find the value in the td im looking for, and then try a : document.createElement("table"); , but my problem is that this newly created table tag comes with an automatically created </table> element / tag, so i end up with just inserting a new blank table after a row instead of inserting a new table tag that has all the remaining tr and tds under it. hope that makes sense. my entire test page is: (click the text link and an alert will show the output with the new <table></table> after the row that my check is looking for (first td with a 2) -------------------------------------------------------------------- Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> <script type="text/javascript"> function reformatMe() { var divObj = document.getElementById('reformatMe'); var numRows = document.getElementsByTagName("tr"); var z = "1"; for (i = 0; i < numRows.length; i++) { if (numRows[i].childNodes[0].innerHTML == "2") { var markStart = numRows[i]; alert(markStart.nextSibling); var closeTable = document.createElement("/table"); var addTable = document.createElement("table"); //var tbody = document.createElement("tbody"); //addTable.appendChild(tbody); addTable.setAttribute("id", "subOptionID" + z); if(numRows[i+1] != null){ numRows[i].parentNode.insertBefore(addTable, numRows[i+1]); } z++; } } alert(divObj.innerHTML); } </script> </head> <body> <a href="#" onclick="reformatMe();">reformat table</a> <div id="reformatMe"> <table border="1" cellpadding="4" cellspacing="2" id="menuItemID1"> <tr><td>1</td> <td>1</td> <td>How would you like your food prepared?</td> <td>1</td> <td>1</td> <td>0</td></tr> <tr><td>1</td> <td>1</td> <td>Choose two of the following sides</td> <td>0</td> <td>2</td> <td>0</td></tr> <tr><td>1</td> <td>1</td> <td>What do you want on your Baked Potato?</td> <td>0</td> <td>2</td> <td>1</td></tr> <tr><td>2</td> <td>1</td> <td>What type of dressing do you want on your salad?</td> <td>1</td> <td>1</td> <td>1</td></tr> <tr><td>1</td> <td>1</td> <td>Do you want fries?</td> <td>1</td> <td>1</td> <td>0</td></tr> <tr><td>1</td> <td>1</td> <td>Do you want ketchup on your fries?</td> <td>1</td> <td>1</td> <td>1</td></tr> </table> </div> </body> </html> I have a master page with a table that contains main content and side bar content. The main content is where the different pages of the site are shown, and the side bar content has stuff like email and friends lists. The mail list is on a model popup and when opened will ajax populate the table with rows containing image, subject, sender, time, all in different td's. When I click a row, the message pops up. From the message I can choose to reply or delete. When I delete I want to fade out the row that was deleted. I can wait the 10 seconds until the list does a refresh but I would rather have this cool visual effect. My problem is I am having difficulty selecting the tr I want. My table has an id of MainMailTable and when I check it does not have the ct100: added in front. Here is what I am trying: Code: $('#MainMailTable:tr:has(td:contains(' + subject + '))').fadeOut(1000); I am trying to find the row that contains the subject of the message I want to delete. I guess that isnt really unique, and maybe I should add a hidden td with an id to the row (can Jquery get a hidden field?). But I would like to be able to get this first step to work. When I try: Code: $('tr:has(td:contains(' + subject + '))').fadeOut(1000); the whole page disappears, so I think I may be close. Thank you in advance for any assistance. I've tried using this to do it, but it isn't working... string.replace(/(<table([^>]+)table>)/ig,""); I'm making an additional page for a phpbb forum for a game. On the page there's 2 textareas to paste in a list of coordinates from a map and then the time it takes to get from each of the coordinates in the left box to each of the coordinates in the right box is calculates and shown in a table. Also is a date/time input, where the user will pick the desired time of arrival. I have this all calculating the times etc. but I would like in one of the columns for it to have a real-time counter that will count down the time before they need to start on the trip in order to arrive at the desired time. I am having a nightmare of a time with this. Here's the script, can someone tell me what I am doing wrong? Code: <!-- INCLUDE overall_header.html --> <script language="JavaScript" type="text/javascript"> function timeLeft(now, next) { var count = next - now; count = count/1000; //change to hours:mins:secs var secs = count % 60; if (secs < 10) secs = '0'+secs; var countdown1 = (count - secs) / 60; var mins = countdown1 % 60; if (mins < 10) mins = '0'+mins; countdown1 = (countdown1 - mins) / 60; var hours = countdown1 % 24; if(hours == 1){htext = " hr, "}else if(hours == 0){hours = "", htext = ""}else{htext = " hrs, "}; if(mins < 2){mtext = " min, "}else{mtext = " mins, "}; if(secs < 2){stext = " sec"}else{stext = " secs"}; document.getElementById('timeLeft').innerHTML = hours+htext+mins+mtext+secs+stext; t=setTimeout('timeLeft()',500); } </script> </head> <body> <table class="borderlist" style="width: 100%; align: center; text-align: center"> <tbody> <tr><td>{MSG}</td></tr> <tr> <td>NOW</td> <td>ID</td> <td>FIELDS</td> <td>TRAVEL_TIME</td> <td>SEND_SECS</td> <td>SEND_TIME</td> <td>TIME_LEFT</td> <td>SEND_IN</td> </tr> <!-- BEGIN times --> <tr> <td>{times.NOW}</td> <td>{times.ID}</td> <td>{times.FIELDS}</td> <td>{times.TRAVEL_TIME}</td> <td>{times.SEND_SECS}</td> <td>{times.SEND_TIME}</td> <td><span id="timeLeft" onload="timeLeft({times.NOW}, {times.SEND_SECS})"></td> <td>{times.SEND_IN}</td> </tr> <!-- END times --> </tbody> </table> </div> <!-- INCLUDE overall_footer.html --> hi, I am working on adding a datepicker on each row of the table grid using javascript . I have a done like below. Code: oCell.innerHTML = "<input type='text' name='DateTime' id='DateTime' size='18' class='maincontent1'> <a href=javascript:NewCssCal('DateTime','yyyymmdd','arrow',true,24,false);><img src='images/cal.gif' width='16' height='16' border='0'alt='Pick a date'> </a> "; Code: oCell = newRow.insertCell(); Is this correct way? with the above code , cursor control pointing to previous textbox not the current one. can anyone help me? thanks I keep getting an error on the following bit of code: document type does not allow element "table" here. Can anyone help Code: return_by_id('quoteboxpre').innerHTML = '<table><tr><td colspan="2"><strong>'+cat.options[cat.selectedIndex].text+'</strong> '+p.options[p.selectedIndex].text+'</td></tr></table>'; Im making a html javascript table that is set to take the number of rows and cols from the user input. i cant make sense of the psuedocode the teacher gave us though. can anyone convert the psuedocode to actual code? the point of the lesson is while loops and do while loops. i basically understand the concept but his psuedocode confuses me :/ im trying to make my question is clear as possible. ask if i've left out a key detail var cols; var rows; cols = prompt("enter number of cols", "5"); cols = parseFloat(cols); rows = prompt("Enter ynumber of rows", "5"); rows = parseFloat(rows); print "<table border=1>" FOR i = 1 to rows DO print "<tr>" FOR j=1 to cols DO print "<td>Row " + i + " Col " + j + "</td>" END FOR print "</tr>" END FOR print "</table>" im confused about the "print" is that meant to be document.write.? ive tried a million ways. ps. the "cols" and "rows" are the variables. i already have it set to take the values with a prompt from the user. thanks alot (im a student trying to learn this in college) thats why the whole program is kinda pointless) I have an 11pm deadline to submit this. Hi, I would like to increase my table row when users hit enter. Please find my html code below. Code: <form id="form1" name="form1" method="post" action=""> <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" id="tblSample"> <tr> <td align="center"><strong>NO</strong></td> <td align="center"><strong>DESCRIPTION</strong></td> <td align="center"><strong>QTY</strong></td> <td align="center"><strong>PRICE</strong></td> <td align="center"><strong>TOTAL</strong></td> </tr> <tr> <td align="center"><label> <input name="textfield2" type="text" id="textfield2" size="8" /> </label></td> <td align="center"><label> <input name="textfield" type="text" id="textfield" onclick="addRowToTable();" size="80"/> </label></td> <td align="center"><input name="textfield3" type="text" id="textfield3" size="8" /></td> <td align="center"><input name="textfield4" type="text" id="textfield4" size="8" /></td> <td align="center"><input name="textfield5" type="text" id="textfield5" size="8" /></td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center">total</td> </tr> </table> </form> hi i have a table with some document.write statements inside that write inside the cells. i need to refresh the table from a function so the statements will execute again. but i only want the table to refresh not the whole page document.location.reload() this does the page but can i put the tables id in the brackets or something. thanks I'm working on a content management system for my website, and when you add a new user, there is an option to select the users class, such as member, moderator, administrator. I want to make it that when moderator or admin is selected, a new table row appears under it showing options for the users permissions. Also, the PHP script for adding users checks the information for errors. If there are errors, it will display them. All of the form info is posted to the error page. Therefore if moderator or admin is selected, the permissions need to come up as well. I tried googling this but I could only find ways to make this happen with DIV's. Thank you in advance for any help you may be able to give me. Hi All, I have this table with a repeat region (this pulls a series of records from my database and displays them in the table). I have a button in one of the table cells that calls a function to delete the current row where the button is pressed (I am trying to pass the row index to the function). Here is what I have which is clearly wrong but I am hoping someone can offer an option that will work. Code: <script type="text/javascript> function deleteRow(tableID,tr) { //alert('tableID is '+tableID+' and rowCount is '+tr); if (document.getElementById(tableID)) { try { var table = document.getElementById(tableID); var rowCount = table.rows.length; for(var i=0; i<rowCount; i++) { var row = table.rows[i]; //alert ("row is "+i); if(i == tr) { table.deleteRow(i); i++; if(rowCount==0){ table.parentNode.removeChild(table); } } } }catch(e) { alert(e); } } } </script> <table id="dataTable" width="100%" border="0"> <?php do { ?> <tr> <td width="33%"><?php echo $row_rstRequestedEquip['EquipNeeded']; ?></td> <td width="33%"><?php echo $row_rstRequestedEquip['Quantity']; ?></td> <td width="33%"><input type="button" name="remove" value="Remove" class="button" onclick = "deleteRow(dataTable,this.rowIndex)" /></td> </tr> <?php } while ($row_rstRequestedEquip = mysql_fetch_assoc($rstRequestedEquip)); ?> </table> Thanks in advance for any help. Kind regards, Ken Code: rownumber = 2; detailsTable = document.getElementsByName("detailsTable"); detailsRow = detailsTable.getElementsByTagName("tr")[rownumber]; name = detailsRow.getElementsbyTagName("td")[0]; address = detailsRow.getElementsbyTagName("td")[1]; address2 = detailsRow.getElementsbyTagName("td")[2]; phone = detailsRow.getElementsbyTagName("td")[3]; email = detailsRow.getElementsbyTagName("td")[4]; alert(name); I have been told if I want to get the text from each cell I can use: alert(name.firstChild.nodeValue); But I don't understand how to do this for what I am wanting to do. Here is my HTML: Code: <table border="0" name="detailsTable"> <tr> <td>a</td> <td>b</td> <td>c</td> <td>d</td> <td>e</td> </tr> </table> Resolution if interested: Code: <head> <title>Lab8</title> <script src="arrays.js" type="text/javascript"></script> <link href="css.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> function amountTotal() { var totalDonations = 0; for (var i = 0; i < amount.length; i++) { totalDonations += amount[i]; } return totalDonations; } </script> </head> <body> <table id="donations" rules="rows"> <tr> <th> Date </th> <th> Name </th> <th> Amount </th> <th> Address </th> </tr> <script type="text/javascript"> for (var i = 0; i < amount.length; i++) { if (i % 2 == 0) document.write("<tr>") else document.write("<tr class='yellowrow'>"); document.write("<td>" + date[i] + "</td>"); document.write("<td>" + firstName[i] + lastName[i] + "</td>"); document.write("<td>" + amount[i] + "</td>"); document.write("<td>" + street[i] + "<br />" + city[i] + "," + " " + state[i] + " " + zip[i] + "</td>"); } document.write("</tr>") </script> </table> <table id="totalsTable"> <tr> <th colspan="2"> Summary </th> </tr> <tr> <td> Contributions </td> <td class="amt"> <script type="text/javascript"> document.write(amount.length); </script> </td> </tr> <tr> <td> Amount </td> <td class="amt">$ <script type="text/javascript"> var totalTotal = amountTotal(); document.write(totalTotal); </script> </td> </tr> </table> </body> </html> I have a seperate .js sheet with some arrays on them (firstName, lastName, date, amount, city, state, zip) and I am trying to populate a table with these indexes. The 'yellowrow' highlights everyother row which is called out in my css sheet. My header rows fill in but not the rest. Here is the link for the site actually posted on the web. Code: <body> <table id="donations" rules="rows"> <tr> <th>Date</th> <th>Name</th> <th>Amount</th> <th>Address</th> </tr> <script type="text/javascript"> for (var i = 0; i < amount.length; i++) { if (i % 2 == 0) document.write("<tr>") else document.write("<tr class='yellowrow'>"); document.write("<td>" + date(i) + "</td>"); document.write("<td>" + firstName(i) + lastName(i) + "</td>"); document.write("<td>" + amount(i) + "</td>"); document.write("<td>" + "<br />" + city(i) + "," + state(i) + zip(i) + "</td>"); } document.write("</tr>") </script> </table> </body> Hi all, I need some help to update my table.It consists about selecting one team from each two drop-down list and enter the score to the two textbox. Textbox 1 relate to the team selected from the first drop-down list and textbox2 relate to the team selected from the second drop-down list.If the score entered in textbox1 is greater than textox2 means that team selected from the first drop-down win and need to update the point of the related team in the table. win = 3 point draw = 1 point lost = 0 point Here what I have down so far...any idea or referencing how I can tackle this task? Code: <form id="foot"> <label for="first">Please select the first team:</label> <select id="first"> <option value="team 1">team 1</option> <option value="team 2">team 2</option> <option value="team 3">team 3</option> </select> <label for="second">Please select the second team:</label> <select id="second"> <option value="team 1">team 1</option> <option value="team 2">team 2</option> <option value="team 3">team 3</option> </select> <input type="submit" value="Update"/><br/> <label for="username">Enter score for the match (First team and Second team)</label> <input type="text" id="txtbox1" size="2"/> <input type="text" id="txtbox2" size="2"/> </form><br/> <table border="1" width="50%" id="score"> <thead> <tr> <th>Team</th> <th>Point</th> </tr> <tbody> <tr> <td>Team 1</td> <td>30</td> </tr> <tr> <td>Team 2</td> <td>20</td> </tr> <tr> <td>Team 3</td> <td>10</td> </tr> </tbody> </thead> </table> function updateTable(){ var form = document.getElementById("foot"); var firstdrop = document.getElementById("first"); var seconddrop = document.getElementById("second"); var box1 = ParseInt(document.getElementById("txtbox1").value); var box2 = ParseInt(document.getElementById("txtbox2").value); var table = document.getElementById("score"); arrwin = ["3"]; arrdraw = ["1"]; arrlost = ["0"]; } </script> Hello there, I'm an IT student that recently started to learn JavaScript. I'm trying to write a code that starts with a prompt inputs a number like n. Then the code will ask another n prompts for data input. For example I will input 5 for first prompt, code will ask for 5 more prompts for input. Can't figure out how to do it
Hi, I am trying to create a radio button that when clicked shows a table. IF you can recommend a way to do it without Javascript that would be MOST helpful. Otherwise here is what I've got so far: [CODE]<input type="radio" name="product" onclick="OnProductClick(this)" value="Stats" />Statistics <input type="radio" name="product" onclick="OnProductClick(this)" value="Tradinghistory" /> Trading History </p> <script type="text/javascript"> var c = true; var b = true; function OnProductClick(product) { if ((c) && (product.value == "Stats")) { document.getElementById('St').style.display = "block"; c = false; b = true; } else if ((b) &&(product.value == "Tradinghistory")) { document.getElementById('TrdH').style.display = "block"; c = true; b = false; } } </script>[CODE] Where St and TrdH are 2 seperate div ids for 2 different tables. Please Help. Many Thanks |