473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

swapping the rows of a table

I've number of rows, i need to take the rowindex of the selected row and pass it as parameter to swapUp or swapDown methods which are called when user selects a row and clicks on Up or Down button

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <body>
  4. <form name="myForm">
  5.  
  6.  <table id="mainTable" border="1" width="100%">
  7.  
  8.    <script>
  9.      document.write('<tr>')
  10.      document.write('<td>')
  11.      document.write('row1')
  12.      document.write('</td>')
  13.      document.write('</tr>')
  14.  
  15.      document.write('<tr>')
  16.      document.write('<td>')
  17.      document.write('row2')
  18.      document.write('</td>')
  19.      document.write('</tr>')
  20.      document.write('</table>')
  21.  
  22.      document.write('<table>')
  23.      document.write('<tr>')
  24.      document.write('<td>')
  25.      document.write('<input type="button" value=" move UP " onClick="swapRowUp(getRowIndex(this))"</input>')>
  26.      document.write('<input type="button" value="move DOWN" onClick="swapRowDown(getRowIndex(this))"</input>')>
  27.      document.write('</td>')
  28.      document.write('</tr>')
  29.      document.write('</table>')
  30. </script>
  31.  </table>
  32.  
  33. </form>
  34. </body>
  35. </head>
  36. </html>
  37. <script>
  38. var mainTable = document.getElementById("mainTable"); 
  39. function getRowIndex(el)
  40. {
  41. while( (el = el.parentNode) && el.nodeName.toLowerCase() !== 'tr' );
  42.  
  43.    if( el ) 
  44.         alert(el.rowIndex);
  45.         return el.rowIndex;
  46. }
  47.  
  48. function swapRowUp(chosenRow) {
  49.  if (chosenRow.rowIndex != 0) {
  50.    moveRow(chosenRow, chosenRow.rowIndex-1); 
  51.  }
  52. function swapRowDown(chosenRow) {
  53.  if (chosenRow.rowIndex != mainTable.rows.length-1) {
  54.    moveRow(chosenRow, chosenRow.rowIndex+1); 
  55.  }
  56.  
  57. function moveRow(targetRow, newIndex) {
  58. if (newIndex > targetRow.rowIndex) {
  59.    newIndex++; 
  60.  }
  61.  
  62.  var mainTable = document.getElementById('mainTable'); 
  63.  
  64.  var theCopiedRow = mainTable.insertRow(newIndex); 
  65.  
  66.  
  67.  for (var i=0; i<targetRow.cells.length; i++) {
  68.    var oldCell = targetRow.cells[i]; 
  69.    var newCell = document.createElement("TD"); 
  70.    newCell.innerHTML = oldCell.innerHTML; 
  71.    theCopiedRow.appendChild(newCell); 
  72.    copyChildNodeValues(targetRow.cells[i], newCell);
  73.  } 
  74. //delete the old row 
  75.  mainTable.deleteRow(targetRow.rowIndex); 
  76.  
  77.  
  78. function copyChildNodeValues(sourceNode, targetNode) {
  79.  for (var i=0; i < sourceNode.childNodes.length; i++) {
  80.    try{
  81.      targetNode.childNodes[i].value = sourceNode.childNodes[i].value;
  82.    }
  83.    catch(e){
  84.  
  85.    }
  86.  }
  87. }
  88.  
  89. </script>
  90.  
this is the code i've written, but its not working.
can anybody help me out
Jan 15 '12 #1
1 1985
Dormilich
8,658 Expert Mod 8TB
it may not work due to the many HTML errors.
Jan 15 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: jeffsal | last post by:
I am using sorttable.js to sort a table which works fine which allows a user to sort the table by clicking on the column header. Is there some code I could add to the page (onload or something) to...
1
by: cotton_gear | last post by:
Hello, Fiest of all let me thank this group for so quick in responding to any postings. I am using a javascript based utility from a site to sort the columns of the table. But, for some strange...
10
by: VA | last post by:
I got the following function to swap table columns from somewhere on the Internet function swapColumns (table, colIndex1, colIndex2) { if (table && table.rows && table.insertBefore && colIndex1...
8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
6
by: Ian Collins | last post by:
I'm having a spot of bother hiding table columns in IE. With FF, setting the column cell's style.display to none (or changing the cell's class name to style that has display: none) completely...
8
by: johkar | last post by:
I would like to ensure this script is optimized (runs correctly or does not execute) for the major browsers. Will checking getElementById and getElementsByTagName accomplish this? In addition,...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
6
by: google | last post by:
I am having trouble with the following function that I am modifying from www.isocra.com. I am trying to change the ID of a hidden form object inside a table row. I have used alert() to test where...
1
by: rakeshnair | last post by:
i wrote a code in jsp to create dynamic table..the problem is i need data base connection when cursor moves from one cell to other... eg...when i enter product id in the first cell, the product name...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.