473,513 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add Table Row, Better Coding Advice

129 New Member
I have created an add table row function in javascript, now it works but the code seems like a bodge job and when it adds a row, each field is added at a time for the entire new row so it is slow, rather than displaying the entire new row at one.

Here is my code;

Expand|Select|Wrap|Line Numbers
  1. function addTimesheetRowFromEdit(currRow)
  2. {
  3.     /* First of all, let's check if the next row already exists */
  4.     var nextTableRow = currRow + 1;
  5.     if(document.getElementById('employeeID_0' + nextTableRow))
  6.     {
  7.         return false;
  8.     } 
  9.  
  10.     var tbl = document.getElementById('editEmployeeTimesheet');
  11.     var lastRow = tbl.rows.length;
  12.     var iteration = lastRow;
  13.     var row = tbl.insertRow(lastRow);
  14.  
  15.     var nextRow = tbl.rows.length - 1; // It's -1 because the next row is +1 but there is a header row and we start numbering at 0 so we compensate for this by taking 2 off.
  16.     for(var i = 0; i < 13; i++)
  17.     {
  18.         // Get the innerHTML of the previous row and insert it into the current row.
  19.         j = i + 1;
  20.  
  21.         var code1 = "var cell" + j.toString() + "= row.insertCell(" + i.toString() + ");"
  22.         var code2 = "var textNode" + j.toString() + " = document.createTextNode(iteration);";
  23.         var code3 = "cell" + j.toString() + ".id = 'DDRow_" + nextRow.toString() + "_" + i.toString() + "'";
  24.  
  25.         if (i == 0) {
  26.             var checkCode = "<input type='checkbox' id='batchSelect[]' name='batchSelect[]' align='absmiddle' value='0'/><input type='hidden' id='rowID_0" + nextRow + "' name='rowID_0" + nextRow + "' value='0' />";
  27.             var code4 = "cell" + j.toString() + ".innerHTML = document.getElementById('DDRow_" + currRow.toString() + "_" + i.toString() + "').innerHTML.replace = checkCode";
  28.         } else if (i == 1) { // If this is the employee column then we need the employee select, not just the text
  29.             var code4 = "cell" + j.toString() + ".innerHTML = tdEmployeeIDContent.replace(/_0" + currRow.toString() + "/gi, '_0" + nextRow.toString() + "')";
  30.         } else {    
  31.             var code4 = "cell" + j.toString() + ".innerHTML = document.getElementById('DDRow_" + currRow.toString() + "_" + i.toString() + "').innerHTML.replace(/_0" + currRow.toString() + "/gi, '_0" + nextRow.toString() + "')";
  32.         }
  33.  
  34.         eval(code1);
  35.         eval(code2);
  36.         eval(code3);
  37.         eval(code4);
  38.  
  39.         if (i == 2) {
  40.             document.getElementById('taskID_0' + nextRow.toString()).value = '0';
  41.         }
  42.         else if (i == 3) {
  43.             document.getElementById('startTime_0' + nextRow.toString()).value = '';
  44.         }
  45.         else if (i == 4) {
  46.             document.getElementById('finishTime_0' + nextRow.toString()).value = '';
  47.         }
  48.         else if (i == 5) {
  49.             document.getElementById('xHour_0' + nextRow.toString()).value = '0';
  50.         }
  51.         else if (i == 6) {
  52.             document.getElementById('description_0' + nextRow.toString()).value = '';
  53.         }
  54.         else if (i == 7) {
  55.             document.getElementById('lodgingValue_0' + nextRow.toString()).value = '';
  56.         }
  57.         else if (i == 8) {
  58.             document.getElementById('expenseValue_0' + nextRow.toString()).value = '';
  59.         }
  60.         else if (i == 9) {
  61.             document.getElementById('ttoc_0' + nextRow.toString()).checked = false;
  62.         }
  63.         else if (i == 10) {
  64.             document.getElementById('ttcc_0' + nextRow.toString()).checked = false;
  65.         }
  66.         else if (i == 11) {
  67.             document.getElementById('td_0' + nextRow.toString()).checked = false;
  68.         }
  69.     }
  70. }
Basically i am after any advice on how to create better code for this function but was also wondering how one would write regex for '_([0-9]+)' in javascript as i have a glitch for my employeeID not changing id's or names depending on the table row?
Apr 13 '11 #1
2 1734
Constantine AI
129 New Member
Also i have tried using the clone row but i couldn't get it to work like this does!
Apr 13 '11 #2
acoder
16,027 Recognized Expert Moderator MVP
Firstly, remove the eval. For example, for code1:
Expand|Select|Wrap|Line Numbers
  1. var window["cell" + j] = row.insertCell(i);
You can remove a lot of the toString() calls too.

Can you post your code attempt for cloneNode:
https://developer.mozilla.org/En/DOM/Node.cloneNode
Apr 14 '11 #3

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

Similar topics

3
1522
by: Kova | last post by:
Hello. As I just started, I wan't to know what's the best (fastest, easiest, whatever just best) way to deal with instances of objects when creating them. For example I use JFileChooser more times in application and should I: 1. every time I need a instance of JFileChooser I create a new one.... or.... 2. create one instance at the begginig...
7
4926
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already developed significant content for the C programming language that is available at: https://www.securecoding.cert.org/ by clicking on the "CERT C...
2
1928
by: ruthboaz | last post by:
Hi, How to create a linked table thro coding? And do a refresh thro coding as in Linked table manager. Is the menu option the only way? I see macros for import and export but for linking I am unable to trace.. Any help?? Thanks & Regards, Ruth.
2
2441
by: XML Beginner | last post by:
I have an XML file that contains values that my application needs, so it knows which database to connect to. It also contains a configuration option so that I can specify which node to return values from. For example, the XML file is meant to return the values from node"config2", because the "configoption" node's value is that. I'm...
3
1675
by: bh | last post by:
If I want to loop through the values in a listbox, and get either all items in the box, or only selected items, based on a boolean variable passed into a subroutine, which method would be more efficient? First Method (test allvalues, first and either loop through all items or selecteditems, accordingly): Dim dview As DataRowView Dim...
2
1478
by: esmith7912 | last post by:
What I really need is a SUMIF, but it looks like a DSUM may be able to help... I want to sum invoice amounts from an invoice table when the id from the original table = the id from the invoice table. Below is a picture of what I'm trying to do: http://www.imagehoop.com/view_image/19db81144/untitled.JPG The DSUM formula I've tried...
2
1501
by: mimidoll | last post by:
Hello everyone, I'm a newbie here and in desperate need of some Table/html coding help! I have a table and within that, 2 table layout, one on the left and the other to the right. In the left I have buttons aligned horizontally. In the right box next to it is all text and a few pictures. The problem is when I add additional text or images,...
1
1615
by: noorulahilah | last post by:
How can i create the table through coding in access? And how can i connect with vb. Suppose "db" is database variable. "stu1" is already existing table name. Now i want to create table "stu2" that copy the structure and data of "stu1" s1="create table stu2 as select * from stu1" db.execute(s1) is it possible?. If not send correct coding
8
3838
natalie99
by: natalie99 | last post by:
hi everyone i have reseached this topic and cannot seem to find a solution that suits my problem, please help!! I have two tables, "Inventory" which contains 30,000 or so records, with about 2000 duplicates. Each duplicate contains the exact same data in every field except one, Field "Code", which may have "Agreed" or "Rejected". The...
6
11173
by: fishercraigj | last post by:
How do I code variables for a simple "running total" box without using an array? IE: I have a "Points Earned" text box that the user inputs a value into. I have another output "Total Points Earned" box that keeps a running count of all points earned every time the user inputs a new value. I've been doing this (which is obviously wrong): ...
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7394
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7559
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.