473,382 Members | 1,480 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,382 software developers and data experts.

How to access controls in a table?

hi all,
im pranav. i have a table with checkbox, sno, book name, book price as coloumns in it. Everything is 5ne while adding and deleting rows.
But my requirement is that, now i want to update the checkbox IDs and row numbers.
For example, pretend that there are 5 rows in the table. I have deleted 2nd and 4th. Now, it should make the 3rd row as 2nd row by updating the checkbox id and sno as chk2 and 2 resp.

here is my code.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE> Adding & Deleting Rows of a Table Dynamically </TITLE>
  5. <SCRIPT type="text/javascript">
Expand|Select|Wrap|Line Numbers
  1. //This function validates the form.
  2. function validateForm()
  3. {
  4.     var bookName=document.getElementById("bookName");
  5.     if(bookName.value.length<=0)
  6.     {
  7.         alert("Please enter Book Name.");
  8.         bookName.focus();
  9.         return false;
  10.     }
  11.     var authorName=document.getElementById("authorName");
  12.     if(authorName.value.length<=0)
  13.     {
  14.         alert("Please enter Author Name.");
  15.         authorName.focus();
  16.         return false;
  17.     }
  18.     var price=document.getElementById("price");
  19.     if(price.value.length<=0)
  20.     {
  21.         alert("Please enter Price.");
  22.         price.focus();
  23.         return false;
  24.     }
  25.     return true;
  26. }
  27.  
  28. //This function returns only numbers.
  29. function nochars(e)
  30. {
  31.     var keynum;
  32.     if(window.event) 
  33.         keynum = e.keyCode;
  34.     if(keynum>=65 && keynum<=90)
  35.         return false;
  36.     else
  37.         return true;
  38. }
  39.  
  40. // This function checks wheather atleast one checkbox is checked or not.
  41. function checkBoxCheck()
  42. {
  43.         var rowCount=document.getElementById("books").rows.length;
  44.         //alert("row count in checkBoxCheck() : "+rowCount);
  45.         for(var i=0;i<=rowCount;i++)
  46.         {
  47.             //alert(document.getElementById("books").rows[i].innerHTML)
  48.             var element="chk"+i;
  49.             //alert(document.getElementById(element))
  50.             if(document.getElementById(element))
  51.             {
  52.  
  53.                     if(document.getElementById(element).checked)
  54.                         return true;
  55.             }
  56.         }
  57.         alert("Please select atleast one row to delete.");
  58.         return false;
  59. }
  60.  
  61. // This function checks all the check boxes.
  62. function checkAll()
  63. {
  64.     if(document.getElementById("chkAll").checked)
  65.     {
  66.         var rowCount=document.getElementById("books").rows.length;
  67.         //alert("row count in checkALL() : "+rowCount);
  68.         for(var i=1;i<=rowCount;i++)
  69.         {
  70.             var element="chk"+i;
  71.             if(document.getElementById(element))
  72.                     document.getElementById(element).checked=true;    
  73.         }
  74.     }
  75.     else
  76.     {
  77.         var rowCount=document.getElementById("books").rows.length;
  78.         for(var i=1;i<=rowCount;i++)
  79.         {
  80.             var element="chk"+i;
  81.             if(document.getElementById(element))
  82.                 document.getElementById(element).checked=false;    
  83.         }
  84.     }
  85. }        
  86.  
  87. // This functions adds new row to the table.
  88. function addRow()
  89. {
  90.     if(validateForm())
  91.     {
  92.         var booksTable=document.getElementById("books");
  93.  
  94.         var newRow= document.createElement('tr');
  95.         newRow.setAttribute('className',"tr");
  96.  
  97.         var cell1= document.createElement('td');
  98.         cell1.setAttribute('className',"td");    
  99.         cell1.setAttribute('width',"20px");    
  100.         var cell2= document.createElement('td');
  101.         cell2.setAttribute('className',"td");
  102.         cell2.setAttribute('width',"50px");    
  103.         var cell3= document.createElement('td');
  104.         cell3.setAttribute('className',"td");
  105.         cell3.setAttribute('width',"225px");    
  106.         var cell4= document.createElement('td');
  107.         cell4.setAttribute('className',"td");
  108.         cell4.setAttribute('width',"225px");    
  109.         var cell5= document.createElement('td');
  110.         cell5.setAttribute('className',"td");
  111.  
  112.         var chkBox=document.createElement('input');
  113.         chkBox.setAttribute('type',"checkbox");
  114.         chkBox.setAttribute('id',"chk"+(booksTable.rows.length+1));
  115.  
  116.         cell1.appendChild(chkBox);
  117.         cell2.appendChild(document.createTextNode(booksTable.rows.length+1));
  118.         cell3.appendChild(document.createTextNode(document.getElementById("bookName").value));
  119.         cell4.appendChild(document.createTextNode(document.getElementById("authorName").value));
  120.         cell5.appendChild(document.createTextNode(document.getElementById("price").value));
  121.  
  122.         newRow.appendChild(cell1);
  123.         newRow.appendChild(cell2);
  124.         newRow.appendChild(cell3);
  125.         newRow.appendChild(cell4);
  126.         newRow.appendChild(cell5);
  127.         booksTable.tBodies(0).appendChild(newRow);
  128. //        alert(newRow.innerHTML);
  129.     }
  130. }
  131.  
  132. // Deletes the selected rows in the table.
  133. function deleteRowz()
  134. {
  135.     if(checkBoxCheck())
  136.     {
  137.         var myBooks=document.getElementById("books");
  138.         rowCount=document.getElementById("books").rows.length;
  139.         for(i=0 ; i<=rowCount ; i++)
  140.         {
  141.             var myChkBox="chk"+(i+1);
  142.             if(document.getElementById(myChkBox))
  143.             {
  144.                 if(document.getElementById(myChkBox).checked==true)
  145.                 {
  146.                         myBooks.deleteRow(findRowNumber(document.getElementById(myChkBox)));
  147.                 }
  148.             }
  149.         }
  150.         document.getElementById("chkAll").checked=false;
  151.         checkAll();
  152.         //updateAllValues();
  153.     }
  154. }
  155. // This function returns the row index of element passed to it.
  156. function findRowNumber(element) 
  157.     while(element.tagName.toLowerCase() != "tr") 
  158.     {
  159.         element = element.parentNode; 
  160.     }
  161.     return element.rowIndex;
  162. }
  163.  
  164. //// NEW FUNCTION : Under Coding :
  165. function updateAllValues()
  166. {
  167.     booksTable=document.getElementById("books")
  168.     rowCount=booksTable.rows.length
  169.     for(var i=0;i<rowCount;i++)
  170.     {
  171.         var currentCheckBox=booksTable.rows[i].cells[0].getElementsByTagName("checkbox")
  172.         alert(currentCheckBox[i].checked);
  173.         for(var j=1;j<=currentCheckBox.length;j++)
  174.             alert(currentCheckBox[i].checked);
  175.     }
  176.  
  177.  
  178. }
  179.  
[HTML]</SCRIPT>
<style>
.table
{
width:640px;
text-align: center;

border-width: 0px;
border-style: solid;
border-color: black;
border-collapse: collapse;
}

.td
{
padding: 1px;
border-width: 1px;
border-style: solid;
border-color: black;

FONT-FAMILY: Arial;
font-size: 14;
}

.tr
{
height: 25px;
text-align: center;
}

.text
{
border-style:solid;
border-width:1px;
}
</style>

</HEAD>

<BODY onload="document.getElementById('bookName').focus( )"> <br><br><input type="button" value="Sample Update" onclick="updateAllValues()"/><br><br>
<table bgcolor=#C8C8C8 align="center" class="table" style="width:240px" >

<tr id="0" class="tr">
<td class="td" > Book Name</td>
<td class="td"> <input type="text" id="bookName" class="text"/> </td>
</tr>

<tr id="1" class="tr">
<td class="td" > Author Name</td>
<td class="td"> <input type="text" id="authorName" class="text"/> </td>
</tr>

<tr id="2" class="tr">
<td class="td" > Price</td>
<td class="td"> <input type="text" id="price" class="text" onkeydown="return nochars(event)"/> </td>
</tr>

<tr id="3" class="tr" bgcolor="white">
<td class="td" colspan=2> <img src="add.jpg" style="cursor:pointer" onclick="addRow()"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<img src="remove.jpg" style="cursor:pointer" onclick="deleteRowz()">
</td>
</tr>

</table>

<br/> <br/> <br/>

<table align="center" class="table" >

<tr class="tr">
<td bgcolor=#00CCCC class="td" colspan=5> <center> <b> Online Book Shopping </b> </center> </td>
</tr>

<tr class="tr">
<th class="td" width=20px> <input type="checkbox" ID="chkAll" onclick="checkAll()"/> </td>
<th class="td" width=50px> S.No. </th>
<th class="td" width=225px> <center> Book Name </center> </th>
<th class="td" width=225px> <center> Author </center> </th>
<th class="td"> <center> Price (in Rs.) </center> </th>
</tr>
</table>
<table id="books" align="center" class="table" >
</table>

</BODY>
</HTML>[/HTML]


Thanks in advance!
Sep 12 '08 #1
3 2739
omerbutt
638 512MB
watch a demo here http://www.myrage.biz/custom_order.php hope that you were looking for it do tell me if you face any prob
Sep 12 '08 #2
thnq for ur quick reply.
i ll go thru it n revert bck my queries 2 u, if any,
Sep 12 '08 #3
omerbutt
638 512MB
thnq for ur quick reply.
i ll go thru it n revert bck my queries 2 u, if any,
you are anytime pranav13 but do avoid these such LINGO (tym , 2 u ) because you need to be descriptive and such things do confuse the other reader who is tryn to givwe a solution for your prob
:) regards,
Omer
Sep 12 '08 #4

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

Similar topics

49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
1
by: Abareblue | last post by:
I have no clue on how to insert a record into access. here is the whole thing using System; using System.Drawing; using System.Collections; using System.ComponentModel;
52
by: Neil | last post by:
We are running an Access 2000 MDB with a SQL 7 back end. Our network guy is upgrading to Windows Server 2003 and wants to upgrade Office and SQL Server at the same time. We're moving to SQL Server...
4
by: Boris Yeltsin | last post by:
OK, on my Master Page I have a control: <a id="hypTabAccount" href="#" runat="server">Account</a> Now, in the code-behind (Root.master.vb) I can refer to it simply thus: ...
0
by: comp974 | last post by:
ok, here is the situation, I am trying to construct a table with several ASP.net 2.0 controls in it during execution time in an VB.net enviroment. For starters, I have a textbox and a linkbutton...
2
by: Mo | last post by:
I am trying to produce an Access report which behaves like a two column table in Word. In other words each cell in column 1 contains a label and each cell in column 2 contains data from a...
9
by: Ron | last post by:
Hi All, I've recently installed a program written in Access 2000 on a laptop. The laptop had an existing Office 2000 Pro which of course included Access. But the program acts oddly (more oddly...
10
by: Les Desser | last post by:
In article <fcebdacd-2bd8-4d07-93a8-8b69d3452f3e@s50g2000hsb.googlegroups.com>, The Frog <Mr.Frog.to.you@googlemail.comMon, 14 Apr 2008 00:45:10 writes Not sure if I quite follow that. 1....
6
by: Wesley Peace | last post by:
I hate to cross post, but I've gotten no answer yet on a problem I'm having with visual studio 2008. I've created a series of forms with controls to access a Access database tables. The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.