si********@hotm ail.com wrote:
Here is the code:
Note I can populate the cells no problem with the new order after you
dragged and dropped the fields.
Click on one of thr rows from the table at the top. It should populate
the fields.
Try dragging the text in field 1 to field 3. You notice that field 1
is missing the text.
I basically need all fields to be populated after the drag drop.
When posting code, don't use tabs, use spaces and indent two (my
preference) or four spaces for each indent.
I created a test case using simplified code based on what I think your
code was doing. Correct me if I'm wrong:
A click on the table row populates the text inputs. Dragging text in
the inputs changes the order in the table row. The text inputs are then
updated from the values in the table - correct?
The problem comes at the end - when you go to read the values from the
table and put them into the fields, the browser isn't ready so some of
the value are missing. You can either use the array of stored values to
update the text fields, or use setTimeout() to delay the text field
update (10ms seems to work fine).
My test code is below.
<html>
<head>
<script language="JavaS cript">
The language attribute is depreciated, type is required:
<script type="text/javascript">
var rowClicked;
var sourceIndex=-1;
var currentCarrierL ist=new Array();
var newCarrierList= new Array();
You can use initialisers here, saves a few keystrokes:
var currentCarrierL ist=[];
var newCarrierList=[];
var selectedCarrier ;
function setSourceIndex( index){
sourceIndex=ind ex;
selectedCarrier =currentCarrier List[index];
}
function orderCarrierCho ices(targetInde x){
if(sourceIndex>-1){
//alert("running! !");
var index=0;
for(var x=0;x<currentCa rrierList.lengt h;x++){
if(x==targetInd ex){
//alert("if "+x+"=="+target Index);
//alert("1:"+curr entCarrierList[x]);
newCarrierList[index]=currentCarrier List[x];
index++;
//alert("2:"+sele ctedCarrier);
newCarrierList[index]=selectedCarrie r;
index++;
}
else if(sourceIndex! =x){
//alert("else x="+x);
newCarrierList[index]=currentCarrier List[x];
index++;
}
}
}
//alert("x");
//document.all.ch oice1.value="";
[...]
currentCarrierL ist=newCarrierL ist;
*/
//alert("y");
Including huge slabs of commented-out code just wastes space and makes
it harder to find the problem.
}
function setCarrierChoic es(index){
//alert("newCarri erList.length:" +newCarrierList .length);
for(var x=0;x<newCarrie rList.length;x+ +){
//alert(x+" carrier:"+newCa rrierList[x]);
if(x==0){
document.all.ch oice1.value="";
Even though this is IE-only code (all that ondrag stuff), don't use
document.all, use DOM compliant document.getEle mentById (you may need to
include a document.all alternative to cover browsers from IE 5, but I
think IE 5.5 onward supports it, certainly IE 6 does).
[...]
function resetRowValues( ){
//alert("setField s:"+row.cells(0 ).innerText);
innerText is a Microsoft proprietary property, the DOM equivalent is
textContent but it's in DOM 3 so not widely supported. You can use
childNode values, or innerHTML and strip out any HTML tags with a
regular expression.
[...]
function select()
{
document.Testfo rm.Code.focus() ;
document.Testfo rm.Code.select( );
To reference the body element, use:
var theBody = (document.docum entElement||doc ument.body);
There appears to be no variable or element with either NAME or ID of code.
}
</script>
</head>
<body name="Testform" >
There is no name attribute for the body element.
<div id="divSelectDr agItem1" onmouseout"aler t('rowmouseleav e')"
style="position :absolute; top:140; left:50; width:100;
When specifying lengths in styles, you must use units - e.g. em, ex, px.
[...]
Here is some play code:
<html><head><ti tle>Drag n Drop test</title>
<script type="text/javascript">
var startEl;
var endEl;
var valueRow;
var valueValues = [];
function initDragEvents( )
{
document.body.o ndragstart = function() {
startEl = event.srcElemen t;
}
document.body.o ndrop = function() {
endEl = event.srcElemen t;
doDrop();
}
}
function doDrop()
{
if ( startEl && startEl.name
&& /input-/.test(startEl.n ame)){
var startIdx = startEl.name.sp lit('-')[1];
}
if ( endEl && endEl.name
&& /input-/.test(endEl.nam e)){
var endIdx = endEl.name.spli t('-')[1];
}
swapArrayValues (valueValues, startIdx, endIdx);
loadRow(valueRo w, valueValues);
setTimeout('loa dFields(valueRo w)',10);
startEl=null;
endEl=null;
}
function swapArrayValues (A, x, y)
{
var tmp = A[x];
A[x] = A[y];
A[y] = tmp;
}
function loadRow(r, A)
{
var i=A.length;
while(i--){
delKids(r.cells[i]);
r.cells[i].appendChild(do cument.createTe xtNode(A[i]));
}
}
function delKids(el)
{
while(el.firstC hild) el.removeChild( el.firstChild);
}
function loadFields(r)
{
var tCells = r.cells;
valueRow = r;
var f = document.forms['formA'];
for (var i=0, j=tCells.length ; i<j; ++i){
valueValues[i] = tCells[i].firstChild.dat a;
f.elements['input-'+i].value = valueValues[i];
}
}
window.onload=i nitDragEvents;
</script>
</head>
<body>
Click the row to put the values into the text fields
<table border="1">
<tr onclick="loadFi elds(this);">
<td>AAA</td><td>BBB</td><td>CCC</td>
</tr>
</table>
<form action="" name="formA">
<input type="text" name="input-0">
<input type="text" name="input-1">
<input type="text" name="input-2">
</form>
</body>
</html>
--
Rob