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.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td ><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_3' ><td>3</td><td>three</td>< td><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_4' ><td>3</td><td>three</td>< td><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></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.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td ><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td ><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_3' ><td>3</td><td>three</td>< td><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_4' ><td>3</td><td>three</td>< td><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></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("t ab"+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,vtLin eNum,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.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td ><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_2' ><td>2</td><td>two</td><td ><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_4' ><td>3</td><td>three</td>< td><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></td> </tr>
<tr id='tabRow_5' ><td>3</td><td>three</td>< td><input type='button' name='but' onclick="getRowId(this.par entNode.pa rentNode.i d)"/></td> </tr>
</table>
How do I work in reordering, specifically keeping in mind this successive row issue??
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
<tr id='tabRow_2' ><td>2</td><td>two</td><td
<tr id='tabRow_3' ><td>3</td><td>three</td><
<tr id='tabRow_4' ><td>3</td><td>three</td><
</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
<tr id='tabRow_2' ><td>2</td><td>two</td><td
<tr id='tabRow_2' ><td>2</td><td>two</td><td
<tr id='tabRow_3' ><td>3</td><td>three</td><
<tr id='tabRow_4' ><td>3</td><td>three</td><
</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("t
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,vtLin
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
<tr id='tabRow_2' ><td>2</td><td>two</td><td
<tr id='tabRow_2' ><td>2</td><td>two</td><td
<tr id='tabRow_4' ><td>3</td><td>three</td><
<tr id='tabRow_5' ><td>3</td><td>three</td><
</table>
How do I work in reordering, specifically keeping in mind this successive row issue??
ASKER
Hielo could you please check, if it is giving you the id>> tabRow_1 twice?? rest all are working fine, but for the first one?? Please let me know you have the same problem.
ASKER
Hielo, we would get same row ID for not just 1st row but for any row that is clicked the first time.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
wonderful!! thanks mate!
Open in new window