Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

[EASY] Moving Rows - Change Form Value

Posted on 2006-06-06
6
Medium Priority
?
187 Views
Last Modified: 2012-08-13
Hello Experts,

I have managed to get a piece of code on here, when its finished i will be listing customers in run order, the problem is the drop numbers may have to change, so the drag n drop approach seems very appealing to my not so IT literate users.

I have the drag n drop working fine, the problem i am facing is probably a simple one for you guys... when the row is moved i need the hidden form field values to change to the current rown the order is placed.

Easy 500 points for someone :o)

Dave

-----

<html>
<head>
<script type="text/javascript">
document.onmousemove = drag;
document.onmouseup = clearObj;
document.ondragstart = function() {return false;};

var dragObj, tblBody;
var index = -1;
function setObj(obj)
{
  dragObj = obj;
  tblBody = obj.parentNode;
  for (var i = 0; i < tblBody.rows.length; i ++)
  {
    if (tblBody.rows[i] == obj)
    {
      index = i;
      break;
    }
  }

  var temp = obj;
  objTop = 0;
  objBottom = 0;
  while (temp.offsetParent)
  {
    objTop += temp.offsetTop;
    temp = temp.offsetParent
  }
  objBottom = objTop + obj.offsetHeight;
}

function clearObj()
{
  dragObj = null;
  index = -1;
  objTop = 0;
  objBottom = 0;
}

function drag(e)
{
  if (dragObj)
  {
    var y = window.event? event.clientY: e.pageY;
    if (y < objTop)
    {
      if (index > 0)
      {
        tblBody.insertBefore(tblBody.rows[index], tblBody.rows[index-1]);
        setObj(tblBody.rows[index-1]);
      }
      else
        clearObj();
    }
    else if (y > objBottom)
    {
      if (index < tblBody.rows.length - 1)
      {
        tblBody.insertBefore(tblBody.rows[index+1], tblBody.rows[index]);
        setObj(tblBody.rows[index+1]);
      }
      else
        clearObj();
    }
  }
}
</script>
</head>

<body>
<table width="3%"  border="0" align="left" cellpadding="3" cellspacing="0">
  <tr>
    <td height="20">1</td>
  </tr>
  <tr>
    <td height="20">2</td>
  </tr>
  <tr>
    <td height="20">3</td>
  </tr>
  <tr>
    <td height="20">4</td>
  </tr>
</table>
<table width="90%" cellpadding="3" cellspacing="0">
  <tbody>
    <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
      <td width="6%" onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
      <td width="23%">A 1
      <input name="dropno" type="hidden" id="dropno" value="1"></td>
      <td width="24%">A 2</td>
      <td width="24%">A 3</td>
      <td width="23%">A 4</td>
    </tr>
    <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
      <td onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
      <td>B 1
      <input name="dropno" type="hidden" id="dropno" value="2"></td>
      <td>B 2</td>
      <td>B 3</td>
      <td>B 4</td>
    </tr>
    <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
      <td onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
      <td>C 1
      <input name="dropno" type="hidden" id="dropno" value="3"></td>
      <td>C 2</td>
      <td>C 3</td>
      <td>C 4</td>
    </tr>
    <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
      <td onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
      <td>D 1
      <input name="dropno" type="hidden" id="dropno" value="4"></td>
      <td>D 2</td>
      <td>D 3</td>
      <td>D 4</td>
    </tr>
    <tr>
      <td colspan="5"><div align="center">
        <input type="submit" name="Submit" value="Submit">
      </div></td>
    </tr>
  </tbody>
</table>
</body>
</html>
0
Comment
Question by:davecestria
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16841527
Hi,

Modify your clearObj function so that it looks like the following.

function clearObj()
{
  var hobj=document.getElementById('dropno');
  if (dragObj != null)
    hobj.value=dragObj.rowIndex + 1;
  dragObj = null;
  index = -1;
  objTop = 0;
  objBottom = 0;
}
0
 
LVL 6

Author Comment

by:davecestria
ID: 16841782
Atul, that doesn't work, it gives me a null is null or not an object error on this line:

    hobj.value=dragObj.rowIndex + 1;

Also there is another problem with the code you posted, if i move row one to row four i need to set the value of all 4 hidden form values:

So if i moved row one to row four this would happen:

row 1 form value becomes 4
row 2 becomes 1
row 3 becomes 2
row 4 becomes 3
row 5 stays as 5

Do you undertsand? I'm not sure the modification you posted would do that without the error??
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16842000
Ok, Check now.

function clearObj()
{
  var hobj=document.getElementsByName('dropno');
  var i;
  for (i=0; i<hobj.length;)
    hobj[i++].value = i;
  dragObj = null;
  index = -1;
  objTop = 0;
  objBottom = 0;
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Author Comment

by:davecestria
ID: 16849680
Atul its still not working, i'm still nto quite sure if your changes will make the correct value changes to the form fields.

I have re-posted code, changing the form fields names to unique ones.

<%
if Request.Form <> "" then
Response.Write "A: " & Request.Form("A") & "<br>"
Response.Write "B: " & Request.Form("B") & "<br>"
Response.Write "C: " & Request.Form("C") & "<br>"
Response.Write "D: " & Request.Form("D") & "<br>"
end if
%>
<html>
<head>
<script type="text/javascript">
document.onmousemove = drag;
document.onmouseup = clearObj;
document.ondragstart = function() {return false;};

var dragObj, tblBody;
var index = -1;
function setObj(obj)
{
  dragObj = obj;
  tblBody = obj.parentNode;
  for (var i = 0; i < tblBody.rows.length; i ++)
  {
    if (tblBody.rows[i] == obj)
    {
      index = i;
      break;
    }
  }

  var temp = obj;
  objTop = 0;
  objBottom = 0;
  while (temp.offsetParent)
  {
    objTop += temp.offsetTop;
    temp = temp.offsetParent
  }
  objBottom = objTop + obj.offsetHeight;
}

function clearObj()
{
  var hobj=document.getElementById('A');
  var i;
  for (i=0; i<hobj.length;)
    hobj[i++].value = i;
  dragObj = null;
  index = -1;
  objTop = 0;
  objBottom = 0;
}


function drag(e)
{
  if (dragObj)
  {
    var y = window.event? event.clientY: e.pageY;
    if (y < objTop)
    {
      if (index > 0)
      {
        tblBody.insertBefore(tblBody.rows[index], tblBody.rows[index-1]);
        setObj(tblBody.rows[index-1]);
      }
      else
        clearObj();
    }
    else if (y > objBottom)
    {
      if (index < tblBody.rows.length - 1)
      {
        tblBody.insertBefore(tblBody.rows[index+1], tblBody.rows[index]);
        setObj(tblBody.rows[index+1]);
      }
      else
        clearObj();
    }
  }
}
</script>
</head>

<body>
<form name="form1" method="post" action="test3.asp">
  <table width="3%"  border="0" align="left" cellpadding="3" cellspacing="0">
    <tr>
      <td height="20">1</td>
    </tr>
    <tr>
      <td height="20">2</td>
    </tr>
    <tr>
      <td height="20">3</td>
    </tr>
    <tr>
      <td height="20">4</td>
    </tr>
  </table>
  <table width="90%" cellpadding="3" cellspacing="0">
    <tbody>
      <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
        <td width="6%" onmousedown="setObj(this.parentNode)" onmouseup="clearObj(this.parentNode)"><img src="ball.gif" height="20"></td>
        <td width="23%">A 1
            <input name="A" type="hidden" id="A" value="1"></td>
        <td width="24%">A 2</td>
        <td width="24%">A 3</td>
        <td width="23%">A 4</td>
      </tr>
      <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
        <td onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
        <td>B 1
            <input name="B" type="hidden" id="B" value="2"></td>
        <td>B 2</td>
        <td>B 3</td>
        <td>B 4</td>
      </tr>
      <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
        <td onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
        <td>C 1
            <input name="C" type="hidden" id="C" value="3"></td>
        <td>C 2</td>
        <td>C 3</td>
        <td>C 4</td>
      </tr>
      <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
        <td onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
        <td>D 1
            <input name="D" type="hidden" id="D" value="4"></td>
        <td>D 2</td>
        <td>D 3</td>
        <td>D 4</td>
      </tr>
      <tr>
        <td colspan="5"><div align="center">
            <input type="submit" name="Submit" value="Submit">
        </div></td>
      </tr>
    </tbody>
  </table>
</form>
</body>
</html>
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 2000 total points
ID: 16849735
davecestria, as I understand, after drag the value of the dropno should be the row number. Is that correct? If it is the try the following code. I just changed the hidden element to text so that its value can be ovserved.

<html>
<head>
<script type="text/javascript">
document.onmousemove = drag;
document.onmouseup = clearObj;
document.ondragstart = function() {return false;};

var dragObj, tblBody;
var index = -1;
function setObj(obj)
{
  dragObj = obj;
  tblBody = obj.parentNode;
  for (var i = 0; i < tblBody.rows.length; i ++)
  {
    if (tblBody.rows[i] == obj)
    {
      index = i;
      break;
    }
  }

  var temp = obj;
  objTop = 0;
  objBottom = 0;
  while (temp.offsetParent)
  {
    objTop += temp.offsetTop;
    temp = temp.offsetParent
  }
  objBottom = objTop + obj.offsetHeight;
}

function clearObj()
{
  var hobj=document.getElementsByName('dropno');
  var i;
  for (i=0; i<hobj.length;)
    hobj[i++].value = i;
  dragObj = null;
  index = -1;
  objTop = 0;
  objBottom = 0;
}

function drag(e)
{
  if (dragObj)
  {
    var y = window.event? event.clientY: e.pageY;
    if (y < objTop)
    {
      if (index > 0)
      {
        tblBody.insertBefore(tblBody.rows[index], tblBody.rows[index-1]);
        setObj(tblBody.rows[index-1]);
      }
      else
        clearObj();
    }
    else if (y > objBottom)
    {
      if (index < tblBody.rows.length - 1)
      {
        tblBody.insertBefore(tblBody.rows[index+1], tblBody.rows[index]);
        setObj(tblBody.rows[index+1]);
      }
      else
        clearObj();
    }
  }
}
</script>
</head>

<body>
<table width="3%"  border="0" align="left" cellpadding="3" cellspacing="0">
  <tr>
    <td height="20">1</td>
  </tr>
  <tr>
    <td height="20">2</td>
  </tr>
  <tr>
    <td height="20">3</td>
  </tr>
  <tr>
    <td height="20">4</td>
  </tr>
</table>
<table width="90%" cellpadding="3" cellspacing="0">
  <tbody>
    <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
      <td width="6%" onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
      <td width="23%">A 1
      <input name="dropno" type="text" id="dropno" value="1"></td>
      <td width="24%">A 2</td>
      <td width="24%">A 3</td>
      <td width="23%">A 4</td>
    </tr>
    <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
      <td onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
      <td>B 1
      <input name="dropno" type="text" id="dropno" value="2"></td>
      <td>B 2</td>
      <td>B 3</td>
      <td>B 4</td>
    </tr>
    <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
      <td onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
      <td>C 1
      <input name="dropno" type="text" id="dropno" value="3"></td>
      <td>C 2</td>
      <td>C 3</td>
      <td>C 4</td>
    </tr>
    <tr onMouseDown="this.style.background='#E6E6E6'" onMouseUp="this.style.background='#FFFFFF'">
      <td onmousedown="setObj(this.parentNode)" onmouseup="clearObj()"><img src="ball.gif" height="20"></td>
      <td>D 1
      <input name="dropno" type="text" id="dropno" value="4"></td>
      <td>D 2</td>
      <td>D 3</td>
      <td>D 4</td>
    </tr>
    <tr>
      <td colspan="5"><div align="center">
        <input type="submit" name="Submit" value="Submit">
      </div></td>
    </tr>
  </tbody>
</table>
</body>
</html>
0
 
LVL 6

Author Comment

by:davecestria
ID: 16849792
Thank you, works great.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question