TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   Multiple rows - dynamic (http://www.talkphp.com/javascript-ajax-e4x/1373-multiple-rows-dynamic.html)

hostfreak 11-03-2007 05:30 PM

Multiple rows - dynamic
 
I've been trying to figure this out for awhile to no avail. What I want to do is have a main row of inputs and a sub row of inputs that can be dynamically generated. I want to be able to make each sub row append to it's main row. So for example:
Code:

Main Row:
|Input|Input|Input| + (Add new main row) - (Delete)
    -Sub Row:
    Input|Input|Input| + (add new sub) - (Delete)
    Input|Input|Input| + (add new sub) - (Delete)

Main Row:
|Input|Input|Input| + (Add new main row) - (Delete)
    -Sub Row:
    Input|Input|Input| + (add new sub) - (Delete)

If I were to add a new sub row, it would add a row under it's main row. Then if a new main row was added, it would add one below the others.

I've figured out how to dynamically create rows, but not have them append to their main row (considering I would also be dynamically adding the main row). Any help is appreciated. Thanks.

Karl 11-04-2007 05:43 PM

I'm using Prototype for this example, but the following code should do sort out adding new main rows:

Basically attach this function to onclick event of your "Add New Main Row" link, like so:

Code:

pAddMainRowLink.onclick = function()
{
    // Grab the main row container element. 
    // We're selecting it by the className, so you need to update
    // this to match the className of your container
    var pMainRowContainer = this.up('mainRowContainerClassName');

    if (!pMainRowContainer)
    {
        return;
    }

    pMainRowContainer.appendChild(page.createMainRow());
}

You'll also need to add the following function (if you remove it from the object literal make sure you update the page.createMainRow() call to reflect the changes). This function is responsible for creating the new main row in the DOM and returning a pointer to it.

Code:

page =
{
    createMainRow: function()
    {
      var pMainRow = $(document.createElement('div'));
      // Add some test content
      pMainRow.update('My new main row!')
      //... setup main row, add content, etc.
      return pMainRow;
    }
}

I've written this from the top of my head and so it may have a few mistakes. If you need any help getting it to work don't be afraid to ask. Also, as I said at the start, this solution also uses the Prototype JavaScript Framework, so you will need to install that before you can try it.

hostfreak 11-04-2007 07:58 PM

Hey Karl,
Thanks for the reply. To be honest, I am not sure how to implement your provided code. I have a couple more aspects to this than I forgot to mention; which at the time I didn't think would have any effect on the actual Inserting / Removing rows, but see now that it will. Let me provide some code:

Here is the page that will need to Add / Remove - Main Rows / Sub Rows (including your provided code, not modified yet):
Code:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Learning JavaScript</title>
        <script type="text/javascript" src="prototype.js">
            pAddMainRowLink.onclick = function()
            {
                // Grab the main row container element. 
                // We're selecting it by the className, so you need to update
                // this to match the className of your container
                var pMainRowContainer = this.up('mainRowContainerClassName');

                if (!pMainRowContainer)
                {
                    return;
                }

                pMainRowContainer.appendChild(page.createMainRow());
            }
            page =
            {
                createMainRow: function()
                {
                  var pMainRow = $(document.createElement('div'));
                  // Add some test content
                  pMainRow.update('My new main row!')
                  //... setup main row, add content, etc.
                  return pMainRow;
                }
            }
        </script>
        <script type="text/javascript" src="totalPrice.js"></script>
    </head>
    <body onload="commercialLabor(); inventoryParts(); commercialPrice();">
        <form name="work_order">
            <table>
                <tr class="main_heading">
                    <th>Initials</th>
                    <th>Work Code</th>
                    <th>Commercial Labor</th>
                    <th>Inventory Price</th>
                    <th>Commercial Price</th>
                </tr>
                <tr class="main_row">
                    <td><input type="" name="initials" /></td>
                    <td><input type="" name="work_code" /></td>
                    <td><input type="" name="commercialLabor_total" /></td> <!-- Total for Commercial Labor for Sub-Rows under this Main-Row -->
                    <td><input type="" name="inventoryParts_total" /></td>  <!-- Total for Inventory Price for Sub-Rows under this Main-Row -->
                    <td><input type="" name="commercialPrice_total" /></td> <!-- Total for Commercial Price for Sub-Rows under this Main-Row -->
                </tr>
                <tr class="sub_heading">
                    <th>Part Name</th>
                    <th>Quantity</th>
                    <th>Commercial Labor</th>
                    <th>Inventory Price</th>
                    <th>Commercial Price</th>
                </tr>
                <tr class="sub_row">
                    <td><input type="" name="part" /></td>
                    <td>
                        <input type="" name="commercial_quantity" style="width: 16px;" />
                        <input type="" name="inventory_quantity" style="width: 16px;" />
                    </td>
                    <td><input type="" name="commercial_labor" onchange="commercialLabor();" /></td>
                    <td><input type="" name="inventory_price" onchange="inventoryParts();" /></td>
                    <td><input type="" name="commercial_price" onchange="commercialPrice();" /></td>
                </tr>
                <tr class="sub_row">
                    <td><input type="" name="part" /></td>
                    <td>
                        <input type="" name="commercial_quantity" style="width: 16px;" />
                        <input type="" name="inventory_quantity" style="width: 16px;" />
                    </td>
                    <td><input type="" name="commercial_labor" onchange="commercialLabor();" /></td>
                    <td><input type="" name="inventory_price" onchange="inventoryParts();" /></td>
                    <td><input type="" name="commercial_price" onchange="commercialPrice();" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>

As you can see I also have a couple more aspects to it; the price totaling. For which here is the JS file:

totalPrice.js
Code:

<!--
    //-----------------------------------
    // Expense Row Totals
    //-----------------------------------
    function my_round(num, dec)
    {
        dec = dec === undefined ? 2 : dec;
        if (typeof num == "number" && typeof dec == "number")
        {
            return num.toFixed(dec);
        }
        return Number.NaN;
    }

    //Commercial Labor
    function commercialLabor()
    {
        var elems = document.forms['work_order'].elements;
        var total_price = 0;
        for (var i = 0; i < elems.length; i++)
        {
            if (elems[i].name.indexOf('commercial_labor') !=-1)
            {
                total_price += +(elems[i].value);
            }
        }
        elems['commercialLabor_total'].value = my_round(total_price);
    }

    //Inventory Parts
    function inventoryParts()
    {
        var elems = document.forms['work_order'].elements;
        var total_price = 0;
        for (var i = 0; i < elems.length; i++)
        {
            if (elems[i].name.indexOf('inventory_price') !=-1)
            {
                total_price += +(elems[i].value);
            }
        }
        elems['inventoryParts_total'].value = my_round(total_price);
    }

    //Commercial Parts
    function commercialPrice()
    {
        var elems = document.forms['work_order'].elements;
        var total_price = 0;
        for (var i = 0; i < elems.length; i++)
        {
            if (elems[i].name.indexOf('commercial_price') !=-1)
            {
                total_price += +(elems[i].value);
            }
        }
        elems['commercialPrice_total'].value = my_round(total_price);
    }
//-->

And the heading rows. Which I am sure will complicate things a little more, being as the would have to be added dynamically too.

I will also need to figure out how to get my prices to total with the dynamic rows now. A big headache for me.

If at this point this is getting too in depth to help me, I understand. I don't blame anyone at all.

Edit: the above code doesn't contain any of the Add / Remove code I had, it seemed pretty messy.

Karl 11-05-2007 01:22 PM

Hey, yes sorry I just don't have time to give you a full solution at the moment. The code I gave yesterday would need a lot of work to fit into your script. Hopefully one of the other members can help.

hostfreak 11-06-2007 12:19 AM

Yeah, I was sure it is pretty complicated. Beyond my skills javascript wise at this moment. Just out of curiosity, how much do you think a freelancer would charge to complete a task like this?

hostfreak 11-21-2007 06:37 PM

Sorry to bump this thread, however I am still in search of help. Thanks guys.

RamananKalirajan 08-19-2008 04:17 AM

Dynamic row
 
HTML Code:

<html>
<head>
<script language="javascript">
var taskno=0;
var ROW_BASE=1;
function addTask()
{
    var oTable=document.getElementById('myTable');
    var lastRow = oTable.rows.length;
    var row=oTable.insertRow(lastRow);
   
  //create WBS Cell
  oCell = row.insertCell(0);
  var el = document.createElement('input');
    el.type="text";
    el.size="5";
    taskno++;
    el.value=taskno;
    oCell.appendChild(el);
    oCell.innerHTML=oCell.innerHTML;
 
//Task Name
    oCell = row.insertCell(1);
    var el1 = document.createElement('input');
    el1.type="text";
    el1.size="25";
    oCell.appendChild(el1);
    oCell.innerHTML=oCell.innerHTML;
 
//Adding SubTask 
    oCell = row.insertCell(2);
    var el2 = document.createElement('input');
    el2.type="button";
    el2.value="Sub Task";
    el2.onclick="addSubTask(this)";
    oCell.appendChild(el2);
    oCell.innerHTML=oCell.innerHTML;
 
//Remove Button   
    oCell = row.insertCell(3);
    var el3 = document.createElement('input');
    el3.type="button";
    el3.value="Remove";
    el3.onclick="removeThis(this)";
    oCell.appendChild(el3);
    oCell.innerHTML=oCell.innerHTML;
 
//Resource
    oCell = row.insertCell(4);
    var el4 = document.createElement('input');
    el4.type="text";
    oCell.appendChild(el4);
    oCell.innerHTML=oCell.innerHTML;
 
//Start Date
    oCell = row.insertCell(5);
    var el5 = document.createElement('input');
    el5.type="text";
    oCell.appendChild(el5);
    oCell.innerHTML=oCell.innerHTML;
 
//End Date
    oCell = row.insertCell(6);
    var el6 = document.createElement('input');
    el6.type="text";
    oCell.appendChild(el6);
    oCell.innerHTML=oCell.innerHTML;
 
//Total Hrs
    oCell = row.insertCell(7);
    var el7 = document.createElement('input');
    el7.type="text";
    oCell.appendChild(el6);
    oCell.innerHTML=oCell.innerHTML;
}
function addSubTask(rowel)
{
    var temp;
    var insert;
    var subRow = rowel.parentNode.parentNode;
    var tbl = subRow.parentNode.parentNode;
    var ri = subRow.sectionRowIndex;
    var nval = tbl.tBodies[0].rows[ri].cells[0].firstChild.value;
    var x=ri+1;
    var nval1;
    if(ri==(document.getElementById('myTable').rows.length-1))
    {
        nval1 = tbl.tBodies[0].rows[ri].cells[0].firstChild.value;
    }
    else
  {
    nval1 = tbl.tBodies[0].rows[x].cells[0].firstChild.value;
  }   
    var str=nval1+"";
    flag="TRUE";
    if(str.indexOf(".")==-1)
    {
      var subtaskno=1;
      temp =nval+"."+subtaskno;
      insert=ri+1;
    }
    else
    {
      while(flag=="TRUE")
      {
          ri=ri+1;
        if(ri==(document.getElementById('myTable').rows.length))
      {
        nval1 = tbl.tBodies[0].rows[ri-1].cells[0].firstChild.value;
          flag="FALSE";
          ri=ri-1;
          str = tbl.tBodies[0].rows[ri].cells[0].firstChild.value; 
          subtaskno=str.substring (str.lastIndexOf (".")+1, str.length);
          var stask = parseInt(subtaskno);
          stask=stask+1;
          temp =nval+"."+stask;
        }
        else
      {
        nval1 = tbl.tBodies[0].rows[ri].cells[0].firstChild.value;
        }
          var str=nval1+"";
          if(str.indexOf(".")==-1)
          {
          flag="FALSE";
          ri=ri-1;
          str = tbl.tBodies[0].rows[ri].cells[0].firstChild.value; 
          subtaskno=str.substring (str.lastIndexOf (".")+1, str.length);
          var stask = parseInt(subtaskno);
          stask=stask+1;
          temp =nval+"."+stask;
          }
        else
          {
          flag=="TRUE" ;
          }   
      }
      insert=ri+1; 
  }
 
  var oTable=document.getElementById('myTable');
    var row=oTable.insertRow(insert);
   
  //create WBS Cell
  oCell = row.insertCell(0);
  var el = document.createElement('input');
    el.type="text";
    el.size="5";
    el.value=temp;
    oCell.appendChild(el);
    oCell.innerHTML=oCell.innerHTML;
 
//Task Name
    oCell = row.insertCell(1);
    var el1 = document.createElement('input');
    el1.type="text";
    el1.size="25";
    oCell.appendChild(el1);
    oCell.innerHTML=oCell.innerHTML;
 
//Adding SubSubTask 
    oCell = row.insertCell(2);
    var el2 = document.createElement('input');
    el2.type="button";
    el2.value="Sub Sub Task";
    el2.onclick="createSubSubTask(this)";
    oCell.appendChild(el2);
    oCell.innerHTML=oCell.innerHTML;
 
//Remove Button   
    oCell = row.insertCell(3);
    var el3 = document.createElement('input');
    el3.type="button";
    el3.value="Remove";
    el3.onclick="removeThis(this)";
    oCell.appendChild(el3);
    oCell.innerHTML=oCell.innerHTML;
 
//Resource
    oCell = row.insertCell(4);
    var el4 = document.createElement('input');
    el4.type="text";
    oCell.appendChild(el4);
    oCell.innerHTML=oCell.innerHTML;
 
//Start Date
    oCell = row.insertCell(5);
    var el5 = document.createElement('input');
    el5.type="text";
    oCell.appendChild(el5);
    oCell.innerHTML=oCell.innerHTML;
 
//End Date
    oCell = row.insertCell(6);
    var el6 = document.createElement('input');
    el6.type="text";
    oCell.appendChild(el6);
    oCell.innerHTML=oCell.innerHTML;
 
//Total Hrs
    oCell = row.insertCell(7);
    var el7 = document.createElement('input');
    el7.type="text";
    oCell.appendChild(el6);
    oCell.innerHTML=oCell.innerHTML;
}

// Function for creating Sub Sub Task
 
function createSubSubTask(rowel)
{
    var temp;
    var insert;
    var subRow = rowel.parentNode.parentNode;
    var tbl = subRow.parentNode.parentNode;
    var ri = subRow.sectionRowIndex;
    var nval = tbl.tBodies[0].rows[ri].cells[0].firstChild.value;
    var x=ri+1;
    var nval1;
    if(ri==(document.getElementById('myTable').rows.length-1))
    {
        nval1 = tbl.tBodies[0].rows[ri].cells[0].firstChild.value;
    }
    else
  {
    nval1 = tbl.tBodies[0].rows[x].cells[0].firstChild.value;
  }   
    var str=nval1+"";
    flag="TRUE";
    if(str.lastIndexOf(".")==1)
    {
      var subsubtaskno=1;
      temp =nval+"."+subsubtaskno;
      insert=ri+1;
    }
    else
    {
    if(str.lastIndexOf(".")==-1)
    {
      subsubtaskno=1;
      temp =nval+"."+subsubtaskno;
      insert=ri+1;
    }
    else
      { 
      while(flag=="TRUE")
      {
          ri=ri+1;
        if(ri==(document.getElementById('myTable').rows.length))
        {
              flag="FALSE";
        }
        else
        { 
        var check=tbl.tBodies[0].rows[ri].cells[0].firstChild.value
        str = check+"";
          if(str.lastIndexOf(".")==-1)
        {
            nval1 = tbl.tBodies[0].rows[ri-1].cells[0].firstChild.value;
          flag="FALSE";
          ri=ri-1;
          str = tbl.tBodies[0].rows[ri].cells[0].firstChild.value; 
          subsubtaskno=str.substring (str.lastIndexOf (".")+1, str.length);
          var stask = parseInt(subsubtaskno);
          stask=stask+1;
          temp =nval+"."+stask;
        }
        else
          {
                flag="TRUE";
          }
        } 
        if(ri==(document.getElementById('myTable').rows.length))
      {
        nval1 = tbl.tBodies[0].rows[ri-1].cells[0].firstChild.value;
          flag="FALSE";
          ri=ri-1;
          str = tbl.tBodies[0].rows[ri].cells[0].firstChild.value; 
          subsubtaskno=str.substring (str.lastIndexOf (".")+1, str.length);
          var stask = parseInt(subsubtaskno);
          stask=stask+1;
          temp =nval+"."+stask;
        }
        else
      {
        nval1 = tbl.tBodies[0].rows[ri].cells[0].firstChild.value;
        }
          var str=nval1+"";
          if(str.lastIndexOf(".")==1)
          {
          flag="FALSE";
          ri=ri-1;
          str = tbl.tBodies[0].rows[ri].cells[0].firstChild.value; 
          subsubtaskno=str.substring (str.lastIndexOf (".")+1, str.length);
          var stask = parseInt(subsubtaskno);
          stask=stask+1;
          temp =nval+"."+stask;
          }
        else
          {
          flag=="TRUE" ;
          }   
      }
      }
      insert=ri+1; 
  }
 
  var oTable=document.getElementById('myTable');
    var row=oTable.insertRow(insert);
   
  //create WBS Cell
  oCell = row.insertCell(0);
  var el = document.createElement('input');
    el.type="text";
    el.size="5";
    el.value=temp;
    oCell.appendChild(el);
    oCell.innerHTML=oCell.innerHTML;
 
//Task Name
    oCell = row.insertCell(1);
    var el1 = document.createElement('input');
    el1.type="text";
    el1.size="25";
    oCell.appendChild(el1);
    oCell.innerHTML=oCell.innerHTML;
 
//Adding SubTask 
    oCell = row.insertCell(2);
    var el2 = document.createElement('nbsp');
    oCell.appendChild(el2);
    oCell.innerHTML=oCell.innerHTML;
 
//Remove Button   
    oCell = row.insertCell(3);
    var el3 = document.createElement('input');
    el3.type="button";
    el3.value="Remove";
    el3.onclick="removeThis(this)";
    oCell.appendChild(el3);
    oCell.innerHTML=oCell.innerHTML;
 
//Resource
    oCell = row.insertCell(4);
    var el4 = document.createElement('input');
    el4.type="text";
    oCell.appendChild(el4);
    oCell.innerHTML=oCell.innerHTML;
 
//Start Date
    oCell = row.insertCell(5);
    var el5 = document.createElement('input');
    el5.type="text";
    oCell.appendChild(el5);
    oCell.innerHTML=oCell.innerHTML;
 
//End Date
    oCell = row.insertCell(6);
    var el6 = document.createElement('input');
    el6.type="text";
    oCell.appendChild(el6);
    oCell.innerHTML=oCell.innerHTML;
 
//Total Hrs
    oCell = row.insertCell(7);
    var el7 = document.createElement('input');
    el7.type="text";
    oCell.appendChild(el6);
    oCell.innerHTML=oCell.innerHTML;
}
 
// End of Sub Sub Task Creation



Code: ( text )
function removeThis(rowel)
{
  var delRow = rowel.parentNode.parentNode;
    var tbl = delRow.parentNode.parentNode;
    var rIndex = delRow.sectionRowIndex;
  var nval = tbl.tBodies[0].rows[rIndex].cells[0].firstChild.value;
      var str=nval;
if(str.indexOf(".")==-1)
{
    document.getElementById('myTable').deleteRow(rIndex);
    taskno--;
  var flag="TRUE";
  while(flag=="TRUE")
  {
    if(rIndex<document.getElementById('myTable').rows.length)
    {   
        var nval = tbl.tBodies[0].rows[rIndex].cells[0].firstChild.value;
        var str=nval;
        if(str.indexOf(".")==-1)
        {
            flag="FALSE";
          }
          else
          {
              if(rIndex==document.getElementById('myTable').rows.length)
              {
                  flag=="FALSE";
              }
            document.getElementById('myTable').deleteRow(rIndex);
          }
    }
    else
    {
        flag="FALSE";
    }
 }
 reorder();
}
else if(str.lastIndexOf(".")==1)
{
  document.getElementById('myTable').deleteRow(rIndex);
  var flag="TRUE";
  var nval = tbl.tBodies[0].rows[rIndex].cells[0].firstChild.value;
  var str=nval+"";
  if(str.indexOf(".")==-1)
  {
        flag="FALSE";
  }
  while(flag=="TRUE")
  {
    if(rIndex<document.getElementById('myTable').rows.length)
    {
      var nval = tbl.tBodies[0].rows[rIndex].cells[0].firstChild.value;
        var str=nval+"";
        if(str.indexOf(".")==-1)
            flag="FALSE";   
        if(str.lastIndexOf(".")==1)
        {
            flag="FALSE";
          }
          else
          {
              if(rIndex==document.getElementById('myTable').rows.length)
              {
                  flag=="FALSE";
              }
                document.getElementById('myTable').deleteRow(rIndex);
          }
    }
    else
    {
        flag="FALSE";
    }
  }
reorder(); 
}
else
{
    document.getElementById('myTable').deleteRow(rIndex);
    reorder();
}
reorder();
}
var lastValue;
function reorder()
{
      var len=document.getElementById('myTable').rows.length  ;
      var tbl = document.getElementById('myTable');
      i=0;
      if(len>
1)
      {
      tbl.tBodies[0].rows[1].cells[0].firstChild.value=1;
      lastValue=tbl.tBodies[0].rows[1].cells[0].firstChild.value;
      }
    var taskno=0;
      var staskno=0;
      var subtaskno=0;
      i=2; 
    while(i<len)
      {
        var x = tbl.tBodies[0].rows[i].cells[0].firstChild.value;
        var y= x+"";
        if(y.indexOf(".")==-1)
          {
                lastValue=parseInt(lastValue)+1;
    tbl.tBodies[0].rows[i].cells[0].firstChild.value=lastValue;
                lastValue= tbl.tBodies[0].rows[i].cells[0].firstChild.value;
                taskno=0;
            }
        else if(y.lastIndexOf(".")==1)
        {
              taskno=taskno+1;
    var staskno = lastValue+"."+taskno;
                tbl.tBodies[0].rows[i].cells[0].firstChild.value=staskno;
    subtaskno=0;
          }   
        else
            {
                subtaskno=subtaskno+1;
    var temp = staskno+"."+subtaskno;
                tbl.tBodies[0].rows[i].cells[0].firstChild.value=temp;
              }
        i=i+1;
      }
       
  }


Code: ( text )
</script>

</head>
<body>
<br/>
<input type="button" value="Add Task" onclick="addTask()">
<br/>
<br/>
<table id='myTable'>
<tr>
<th>WBS</th>
<th>Task Name</th>
<th></th>
<th></th>
<th>Resource</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total Hrs.</th>
</tr>
</table>
</body>
</html>

Hello Sir, I did this component for my requirement inorder to create a WBS. I hope this may be useful for you.

Regards
Ramanan Kalirajan


All times are GMT. The time now is 09:38 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0