Link to home
Start Free TrialLog in
Avatar of gautam_reddyc
gautam_reddyc

asked on

Insert a row at a specific index and reorder other rows.

Hello I have to insert a row at a specific position.  After insert, I would have to reorder the rows and change the ids of elements inside the rows.

The problem I am having is, I am able to change ids for all the elements but for the element just successive to it.

<table id='tab'>
<tr id='tabRow_1' ><td>1</td><td>one</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_3' ><td>3</td><td>three</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_4' ><td>3</td><td>three</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
</table>

So if I add a new row at position 2, I would have a table that would look similar to this

<table id='tab'>
<tr id='tabRow_1' ><td>1</td><td>one</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_3' ><td>3</td><td>three</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_4' ><td>3</td><td>three</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
</table>

To tackle this problem, I am passing in the row number where the row is added, i.e vtLineNum,  and am passing all the rows in the table. Matching them to where the row number is greater than line num, the change would take place.

var elems = document.getElementById("tab"+vtName).rows;
      for(var i in elems)
      {
            if(elems[i].id !==null && elems[i].id !=='' && elems[i].id !=null)
            {
            v = explodeArray(elems[i].id,'_');
                  if(Number(v[1])>=vtLineNum)
                  {      
                  getRowDataAdd(vtName,vtLineNum,elems[i].id);
                  lineNum = Number(v[1])+ 1;                  
                  elems[i].id = v[0] +'_'+ lineNum;
                  rowStyle = (lineNum % 2 == 1) ? "on" : "off";
                  elems[i].className = "row_" + rowStyle;            
                  }
            }
      }

Everything is working fine now, but I would have two identical rows where row insertion occurs, the reason being I am making sure only rows greater than the number passed would only be changed. So I have a table looking like this.


<table id='tab'>
<tr id='tabRow_1' ><td>1</td><td>one</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_4' ><td>3</td><td>three</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_5' ><td>3</td><td>three</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
</table>

How do I work in reordering, specifically keeping in mind this successive row issue??
Avatar of HonorGod
HonorGod
Flag of United States of America image

What you can do, as part of the row insertion process, is:

- locate all of the rows in the table:

var table; = document.getElementById( 'tab' )
  if ( table ) {
    var rows = table.rows;
    ... Then, here, you can iterate over all of the rows, assigning/reassigning the id attribute
    ... for each TR element...
  } else {
    alert( 'Error - document element not found. id="tab"' );
  }

Open in new window

Avatar of gautam_reddyc
gautam_reddyc

ASKER

well I have done that!! I am able to reassign IDs to all the existent rows, but the problem lies with the inserted row. It would carry an ID of where it was inserted at. So if it is inserted after 3rd, it takes id = tabRow_3, and there would be two rows having same ID. I am unable to change these two rows, rest of the rows are working good!
Did you have the id (re)adjustment occur after the insertion?
Yes, I am unable to change only for rows which would have the same row ID, as both of them get changed. If I insert a row after row 2, I would get the below mentioned.

<table id='tab'>
<tr id='tabRow_1' ><td>1</td><td>one</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_4' ><td>3</td><td>three</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
<tr id='tabRow_5' ><td>3</td><td>three</td><td><input type='button' name='but' onclick="getRowId(this.parentNode.parentNode.id)"/></td></tr>
</table>

The problem is I should get id ='tab_row3' for the third row
ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hey thanks!! got it myself using row.rowIndex. Appreciate your effort!! you code worked too!!
Thanks for the grade & points.

Good luck & have a great day.