查看: 1020|回复: 9
|
Javascript-Dynamic Table 加 row 的问题
[复制链接]
|
|
发表于 22-12-2006 12:35 AM
|
显示全部楼层
要 pre-load 资料的话, 先用 php 一边把资料 loop 出来, 一边用 php 输出 html table row 的 tag |
|
|
|
|
|
|
|
楼主 |
发表于 22-12-2006 10:45 AM
|
显示全部楼层
我的code如下:
<script language="Javascript">
var INPUT_NAME_PREFIX1 = 'rate'; // this is being set via script
var INPUT_NAME_PREFIX2 = 'total'; // this is being set via script
var RADIO_NAME = 'totallyrad'; // this is being set via script
var TABLE_NAME = 'tbl'; // this should be named in the HTML
var ROW_BASE = 1; // first number (for display)
var hasLoaded = false;
<?if($rate_charge_type=="1" || $rate_charge_type=="2" || $rate_charge_type=="3"){?>
window.onload=fillInRows;
<?}?>
function fillInRows()
{
hasLoaded = true;
addRowToTable();
}
// CONFIG:
// myRowObject is an object for storing information about the table rows
function myRowObject(one, two, three, four, five)
{
this.one = one; // text object
this.two = two; // input text object
this.three = three; // input text object
this.four = four; // input checkbox object
this.five = five; // input radio object
}
/*
* insertRowToTable
* Insert and reorder
*/
function insertRowToTable()
{
if (hasLoaded) {
var tbl = document.getElementById(TABLE_NAME);
var rowToInsertAt = tbl.tBodies[0].rows.length;
for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
if (tbl.tBodies[0].rows.myRow && tbl.tBodies[0].rows.myRow.five.getAttribute('type') == 'radio' && tbl.tBodies[0].rows.myRow.five.checked) {
rowToInsertAt = i;
break;
}
}
addRowToTable(rowToInsertAt);
reorderRows(tbl, rowToInsertAt);
}
}
/*
* addRowToTable
* Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
*/
function addRowToTable(num,rt,tt)
{
if(!rt)
rt='';
if(!tt)
tt='';
if (hasLoaded) {
var tbl = document.getElementById(TABLE_NAME);
var nextRow = tbl.tBodies[0].rows.length;
var iteration = nextRow + ROW_BASE;
if (num == null) {
num = nextRow;
} else {
iteration = num + ROW_BASE;
}
// add the row
var row = tbl.tBodies[0].insertRow(num);
// CONFIG: requires classes named classy0 and classy1
row.className = 'classy' + (iteration % 2);
// CONFIG: This whole section can be configured
// cell 0 - text
var cell0 = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cell0.appendChild(textNode);
// cell 1 - input text
var cell1 = row.insertCell(1);
var txtInp = document.createElement('input');
txtInp.setAttribute('type', 'text');
txtInp.setAttribute('name', INPUT_NAME_PREFIX1 + iteration);
txtInp.setAttribute('size', '15');
txtInp.setAttribute('value', rt); // iteration included for debug purposes
cell1.appendChild(txtInp);
// cell 2 - input text
var cell2 = row.insertCell(2);
var txtInp2 = document.createElement('input');
txtInp2.setAttribute('type', 'text');
txtInp2.setAttribute('name', INPUT_NAME_PREFIX2 + iteration);
txtInp2.setAttribute('size', '15');
txtInp2.setAttribute('value', tt); // iteration included for debug purposes
cell2.appendChild(txtInp2);
// cell 3 - input button
var cell3 = row.insertCell(3);
var btnEl = document.createElement('input');
btnEl.setAttribute('type', 'button');
btnEl.setAttribute('value', 'Remove Rows');
btnEl.onclick = function () {deleteCurrentRow(this)};
cell3.appendChild(btnEl);
// cell 4 - input checkbox
var cell4 = row.insertCell(4);
var cbEl = document.createElement('input');
cbEl.setAttribute('type', 'checkbox');
cell4.appendChild(cbEl);
// cell 5 - input radio
var cell5 = row.insertCell(5);
var raEl;
try {
raEl = document.createElement('<input type="radio" name="' + RADIO_NAME + '" value="' + iteration + '">');
var failIfNotIE = raEl.name.length;
} catch(ex) {
raEl = document.createElement('input');
raEl.setAttribute('type', 'radio');
raEl.setAttribute('name', RADIO_NAME);
raEl.setAttribute('value', iteration);
}
cell5.appendChild(raEl);
// Pass in the elements you want to reference later
// Store the myRow object in each row
row.myRow = new myRowObject(textNode, txtInp, txtInp2, cbEl, raEl);
}
}
// CONFIG: this entire function is affected by myRowObject settings
// If there isn't a checkbox in your row, then this function can't be used.
function deleteChecked()
{
if (hasLoaded) {
var checkedObjArray = new Array();
var cCount = 0;
var tbl = document.getElementById(TABLE_NAME);
for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
if (tbl.tBodies[0].rows.myRow && tbl.tBodies[0].rows.myRow.four.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows.myRow.four.checked) {
checkedObjArray[cCount] = tbl.tBodies[0].rows;
cCount++;
}
}
if(tbl.tBodies[0].rows.length==cCount)
{
alert('You can not remove all the row.');
return false;
}
if (checkedObjArray.length > 0) {
var rIndex = checkedObjArray[0].sectionRowIndex;
deleteRows(checkedObjArray);
reorderRows(tbl, rIndex);
}
}
}
// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRow(obj)
{
if (hasLoaded) {
var delRow = obj.parentNode.parentNode;
var tbl = delRow.parentNode.parentNode;
var rIndex = delRow.sectionRowIndex;
var rowArray = new Array(delRow);
deleteRows(rowArray);
reorderRows(tbl, rIndex);
}
}
function reorderRows(tbl, startingIndex)
{
if (hasLoaded)
{
if (tbl.tBodies[0].rows[startingIndex])
{
var count = startingIndex + ROW_BASE;
for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++)
{
//alert('run time'+i);
// CONFIG: next line is affected by myRowObject settings
tbl.tBodies[0].rows.myRow.one.data = count; // text
// CONFIG: next line is affected by myRowObject settings
tbl.tBodies[0].rows.myRow.two.name = INPUT_NAME_PREFIX1 + count; // input text
// CONFIG: next line is affected by myRowObject settings
tbl.tBodies[0].rows.myRow.three.name = INPUT_NAME_PREFIX2 + count; // input text
// CONFIG: next line is affected by myRowObject settings
var tempVal = tbl.tBodies[0].rows.myRow.two.value.split(' '); // for debug purposes
tbl.tBodies[0].rows.myRow.two.value = count + ' was' + tempVal[0]; // for debug purposes
// CONFIG: next line is affected by myRowObject settings
var tempVal = tbl.tBodies[0].rows.myRow.three.value.split(' '); // for debug purposes
tbl.tBodies[0].rows.myRow.three.value = count + ' was' + tempVal[0]; // for debug purposes
// CONFIG: next line is affected by myRowObject settings
tbl.tBodies[0].rows.myRow.five.value = count; // input radio
// CONFIG: requires class named classy0 and classy1
tbl.tBodies[0].rows.className = 'classy' + (count % 2);
count++;
}
}
}
}
function deleteRows(rowObjArray)
{
if (hasLoaded) {
var tbl = document.getElementById(TABLE_NAME);
var nextRow = tbl.tBodies[0].rows.length;
for (var i=0; i<rowObjArray.length; i++) {
var rIndex = rowObjArray.sectionRowIndex;
if(nextRow==1)
{
alert('You can not remove all the row.');
return false;
}
else
rowObjArray.parentNode.deleteRow(rIndex);
}
}
}
function openInNewWindow(frm)
{
// open a blank window
var aWindow = window.open('', 'TableAddRow2NewWindow',
'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
// set the target to the blank window
frm.target = 'TableAddRow2NewWindow';
// submit
frm.submit();
}
</script>
<?
//Shipping By Configuration
echo "<table border='1' cellspacing='1' cellpadding='2' width='56.2%'>";
echo "<tr align=center>\n";
echo "<td align='center' class='pneAltTitleCell' width='29%'>Rules (>=)</td>\n";
echo "<td align=center class='pneAltTitleCell' width='27%'>Charge</td>\n";
echo "<td align=center class='pneAltTitleCell' colspan=3>Action</td>\n";
echo "</tr>\n";
echo "</tr></table>\n";
echo "<table border='0' cellspacing='1' cellpadding='2' name='tbl' id='tbl' width='40%'>";
$sql = "SELECT * FROM shipping_rule_mul WHERE rate_ID='$rate_ID'";
$result = odbc_exec($link, $sql);
if(odbc_num_rows($result))
{
$recordExist=1;
$i=1;
$j=$i-1;
while($row = odbc_fetch_array($result))
{
$rate = $row[rate];
$total = $row[total];
echo "<tr align=center>\n";
echo "<script language='Javascript'>hasLoaded=true;addRowToTable(".$j.",".$rate.",".$total.");</script>";
echo "</tr>\n";
$i++;
$j++;
}
}
else
{
$recordExist=0;
}
echo "</table>";
echo "<table border='0' width='98%' cellspacing='0' cellpadding='2'>";
echo "<tr align='center'>";
echo "<td width='40%' align='center' height='70'>";
echo "<br>";
echo "<input type=\"button\" value=\"Add More Rows\" onclick=\"addRowToTable();\" />";
echo "<input type=\"button\" value=\"Insert Before [I]\" onclick=\"insertRowToTable();\" />";
echo "<input type=\"button\" value=\"Remove Checked Rows [R]\" onclick=\"deleteChecked();\" />";
echo "<input type=\"button\" value=\"Submit\" onclick=\"openInNewWindow(this.form);\" />";
echo "<input type='button' value='Remove Rows' name='removerows'> ";
echo "</td>";
echo "<td width='60%' align='left' class='pneStandardText' height='70'>";
echo "</td></tr>";
echo "</table>";
}
?>
当我从db load data进来后,如果要删除preload 的data,就有问题,问题出在我call reorderRows()时,能指点一下吗?感激不尽 |
|
|
|
|
|
|
|
发表于 22-12-2006 05:43 PM
|
显示全部楼层
有点无从下手...
不过... 我建议你这样,因为是 javascript, 当你 load 好你的 data, 就 view source 然后贴在这里... 这样我们才可以测试你的 delete row...
因为现在完全没有 data... 我们也无法执行你的 php, 所以看 php 输出来的 source 是最好的. |
|
|
|
|
|
|
|
楼主 |
发表于 23-12-2006 04:34 PM
|
显示全部楼层
<script language='Javascript'>
function updateRegistration() {
var len = document.forms[0].elements.length;
var t, r, tot, rat, temp="", num=0;
for(var i=0; i<len; i++) {
if(document.forms[0].elements.name == "rate[]") num++;
}
for(var j=1; j<=num; j++)
{
r = document.getElementById("rate"+j);
t = document.getElementById("total"+j);
rat = r.value;
tot = t.value;
if((rat.length==0 && tot.length!=0) || (tot.length==0 && rat.length!=0)) {
alert("Please enter both rate and total.");
return false;
}
temp = temp + t.value + r.value;
if(isNaN(t.value) || isNaN(r.value)) {
alert("Please enter only digit characters.");
return false;
}
}
if(temp.length==0) {
alert("Please enter a set of rates to continue.");
return false;
}
//return false;
}
/**************************************************/
// CONFIG:
// myRowObject is an object for storing information about the table rows
function myRowObject(one, two, three, four, five)
{
this.one = one; // text object
this.two = two; // input text object
this.three = three; // input text object
this.four = four; // input checkbox object
this.five = five; // input radio object
}
/*
* insertRowToTable
* Insert and reorder
*/
function insertRowToTable()
{
if (hasLoaded) {
var tbl = document.getElementById(TABLE_NAME);
var rowToInsertAt = tbl.tBodies[0].rows.length;
for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
if (tbl.tBodies[0].rows.myRow && tbl.tBodies[0].rows.myRow.five.getAttribute('type') == 'radio' && tbl.tBodies[0].rows.myRow.five.checked) {
rowToInsertAt = i;
break;
}
}
addRowToTable(rowToInsertAt);
reorderRows(tbl, rowToInsertAt);
}
}
/*
* addRowToTable
* Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
*/
function addRowToTable(num,rt,tt)
{
//alert(num);
//alert(rt);
//alert(tt);
if(!rt)
rt='';
if(!tt)
tt='';
if (hasLoaded) {
var tbl = document.getElementById(TABLE_NAME);
var nextRow = tbl.tBodies[0].rows.length;
var iteration = nextRow + ROW_BASE;
if (num == null) {
num = nextRow;
} else {
iteration = num + ROW_BASE;
}
// add the row
var row = tbl.tBodies[0].insertRow(num);
// CONFIG: requires classes named classy0 and classy1
row.className = 'classy' + (iteration % 2);
// CONFIG: This whole section can be configured
// cell 0 - text
var cell0 = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cell0.appendChild(textNode);
// cell 1 - input text
var cell1 = row.insertCell(1);
var txtInp = document.createElement('input');
txtInp.setAttribute('type', 'text');
txtInp.setAttribute('name', INPUT_NAME_PREFIX1 + iteration);
txtInp.setAttribute('size', '15');
txtInp.setAttribute('value', rt); // iteration included for debug purposes
cell1.appendChild(txtInp);
//cs end add
// cell 2 - input text
var cell2 = row.insertCell(2);
var txtInp2 = document.createElement('input');
txtInp2.setAttribute('type', 'text');
txtInp2.setAttribute('name', INPUT_NAME_PREFIX2 + iteration);
txtInp2.setAttribute('size', '15');
txtInp2.setAttribute('value', tt); // iteration included for debug purposes
cell2.appendChild(txtInp2);
//cs end add
// cell 3 - input button
var cell3 = row.insertCell(3);
var btnEl = document.createElement('input');
btnEl.setAttribute('type', 'button');
btnEl.setAttribute('value', 'Remove Rows');
btnEl.onclick = function () {deleteCurrentRow(this)};
cell3.appendChild(btnEl);
// cell 4 - input checkbox
var cell4 = row.insertCell(4);
var cbEl = document.createElement('input');
cbEl.setAttribute('type', 'checkbox');
cell4.appendChild(cbEl);
// cell 5 - input radio
var cell5 = row.insertCell(5);
var raEl;
try {
raEl = document.createElement('<input type="radio" name="' + RADIO_NAME + '" value="' + iteration + '">');
var failIfNotIE = raEl.name.length;
} catch(ex) {
raEl = document.createElement('input');
raEl.setAttribute('type', 'radio');
raEl.setAttribute('name', RADIO_NAME);
raEl.setAttribute('value', iteration);
}
cell5.appendChild(raEl);
// Pass in the elements you want to reference later
// Store the myRow object in each row
row.myRow = new myRowObject(textNode, txtInp, txtInp2, cbEl, raEl);
}
}
// CONFIG: this entire function is affected by myRowObject settings
// If there isn't a checkbox in your row, then this function can't be used.
function deleteChecked()
{
if (hasLoaded) {
var checkedObjArray = new Array();
var cCount = 0;
var tbl = document.getElementById(TABLE_NAME);
for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
if (tbl.tBodies[0].rows.myRow && tbl.tBodies[0].rows.myRow.four.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows.myRow.four.checked) {
checkedObjArray[cCount] = tbl.tBodies[0].rows;
cCount++;
}
}
if(tbl.tBodies[0].rows.length==cCount)
{
alert('You can not remove all the row.');
return false;
}
if (checkedObjArray.length > 0) {
var rIndex = checkedObjArray[0].sectionRowIndex;
deleteRows(checkedObjArray);
reorderRows(tbl, rIndex);
}
}
}
// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRow(obj)
{
if (hasLoaded) {
var delRow = obj.parentNode.parentNode;
var tbl = delRow.parentNode.parentNode;
var rIndex = delRow.sectionRowIndex;
var rowArray = new Array(delRow);
deleteRows(rowArray);
reorderRows(tbl, rIndex);
}
}
function reorderRows(tbl, startingIndex)
{
if (hasLoaded)
{
if (tbl.tBodies[0].rows[startingIndex])
{
var count = startingIndex + ROW_BASE;
for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++)
{
// CONFIG: next line is affected by myRowObject settings
tbl.tBodies[0].rows.myRow.one.data = count; // text
// CONFIG: next line is affected by myRowObject settings
tbl.tBodies[0].rows.myRow.two.name = INPUT_NAME_PREFIX1 + count; // input text
// CONFIG: next line is affected by myRowObject settings
tbl.tBodies[0].rows.myRow.three.name = INPUT_NAME_PREFIX2 + count; // input text
// CONFIG: next line is affected by myRowObject settings
//var tempVal = tbl.tBodies[0].rows.myRow.two.value.split(' '); // for debug purposes
//tbl.tBodies[0].rows.myRow.two.value = count + ' was' + tempVal[0]; // for debug purposes
// CONFIG: next line is affected by myRowObject settings
//var tempVal = tbl.tBodies[0].rows.myRow.three.value.split(' '); // for debug purposes
//tbl.tBodies[0].rows.myRow.three.value = count + ' was' + tempVal[0]; // for debug purposes
// CONFIG: next line is affected by myRowObject settings
tbl.tBodies[0].rows.myRow.five.value = count; // input radio
// CONFIG: requires class named classy0 and classy1
tbl.tBodies[0].rows.className = 'classy' + (count % 2);
count++;
}
}
}
}
function deleteRows(rowObjArray)
{
if (hasLoaded) {
var tbl = document.getElementById(TABLE_NAME);
var nextRow = tbl.tBodies[0].rows.length;
for (var i=0; i<rowObjArray.length; i++) {
var rIndex = rowObjArray.sectionRowIndex;
if(nextRow==1)
{
alert('You can not remove all the row.');
return false;
}
else
rowObjArray.parentNode.deleteRow(rIndex);
}
}
}
function openInNewWindow(frm)
{
// open a blank window
var aWindow = window.open('', 'TableAddRow2NewWindow',
'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
// set the target to the blank window
frm.target = 'TableAddRow2NewWindow';
// submit
frm.submit();
}
</script> |
|
|
|
|
|
|
|
楼主 |
发表于 23-12-2006 04:35 PM
|
显示全部楼层
<table border='0' cellspacing='1' cellpadding='2' width='56.2%'><tr align=left>
<td colspan=6 align=left class='pneStandardTextBoldLight'>Method Name :<b>1</b></td>
</table><br><table border='1' cellspacing='1' cellpadding='2' width='56.2%'><tr align=center>
<td align='center' class='pneAltTitleCell' width='29%'>Rules (>=)</td>
<td align=center class='pneAltTitleCell' width='27%'>Charge</td>
<td align=center class='pneAltTitleCell' colspan=3>Action</td>
</tr>
</tr></table>
<table border='0' cellspacing='1' cellpadding='2' name='tbl' id='tbl' width='40%'><tr align=center>
<td align='center' class='pneStandardTextBoldLight'>1</td><td align='center' class='pneStandardTextBoldLight'><input type='text' name=rate1 id=rate1 size='15' maxlength='12' value='44.00'></td>
<td align=center class='pneStandardTextBoldLight'><input type='text' name=total1 id=total1 size='15' maxlength='12' value='120.00'></td>
<td align='center' class='pneStandardTextBoldLight'><input type='button' value='Remove Rows' > </td>
<td align='center' class='pneStandardTextBoldLight'><input type="checkbox"></td>
<td align='center' class='pneStandardTextBoldLight'><input type="radio" name="totallyrad" value=1></td>
</tr>
<tr align=center>
<td align='center' class='pneStandardTextBoldLight'>2</td><td align='center' class='pneStandardTextBoldLight'><input type='text' name=rate2 id=rate2 size='15' maxlength='12' value='55.00'></td>
<td align=center class='pneStandardTextBoldLight'><input type='text' name=total2 id=total2 size='15' maxlength='12' value='55.00'></td>
<td align='center' class='pneStandardTextBoldLight'><input type='button' value='Remove Rows' > </td>
<td align='center' class='pneStandardTextBoldLight'><input type="checkbox"></td>
<td align='center' class='pneStandardTextBoldLight'><input type="radio" name="totallyrad" value=2></td>
</tr>
<tr align=center>
<td align='center' class='pneStandardTextBoldLight'>3</td><td align='center' class='pneStandardTextBoldLight'><input type='text' name=rate3 id=rate3 size='15' maxlength='12' value='66.00'></td>
<td align=center class='pneStandardTextBoldLight'><input type='text' name=total3 id=total3 size='15' maxlength='12' value='666.00'></td>
<td align='center' class='pneStandardTextBoldLight'><input type='button' value='Remove Rows' > </td>
<td align='center' class='pneStandardTextBoldLight'><input type="checkbox"></td>
<td align='center' class='pneStandardTextBoldLight'><input type="radio" name="totallyrad" value=3></td>
</tr>
<tr align=center>
<td align='center' class='pneStandardTextBoldLight'>4</td><td align='center' class='pneStandardTextBoldLight'><input type='text' name=rate4 id=rate4 size='15' maxlength='12' value='88.00'></td>
<td align=center class='pneStandardTextBoldLight'><input type='text' name=total4 id=total4 size='15' maxlength='12' value='888.00'></td>
<td align='center' class='pneStandardTextBoldLight'><input type='button' value='Remove Rows' > </td>
<td align='center' class='pneStandardTextBoldLight'><input type="checkbox"></td>
<td align='center' class='pneStandardTextBoldLight'><input type="radio" name="totallyrad" value=4></td>
</tr>
</table><table border='0' width='98%' cellspacing='0' cellpadding='2'><tr align='center'><td width='40%' align='center' height='70'><br><input type="button" value="Add More Rows" /><input type="button" value="Insert Before [I]" /><input type="button" value="Remove Checked Rows [R]" /><input type="submit" name="submit" value="Update" ></td><td width='60%' align='left' class='pneStandardText' height='70'></td></tr></table><!--
<br><br>
<table border='0' width='40%' cellspacing='1' cellpadding='2'>
<tr><td align=left>
<input type="submit" name="submit" value="Submit" >
</td></tr>
</table>
-->
</form>
</body>
先谢过了goatstudio兄
[ 本帖最后由 cscari 于 23-12-2006 04:36 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 23-12-2006 10:28 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 24-12-2006 01:13 AM
|
显示全部楼层
大约了解了你的 code, 由于我没运行 addRowToTable, 所以无法完全测试 (因为已经没有了 myRow 这个 object). 相信你已经在 php 里 loop 的时候运行了. 不过, 你的问题应该是以下这段:
- if (tbl.tBodies[0].rows.myRow && tbl.tBodies[0].rows.myRow.four.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows.myRow.four.checked) {
- checkedObjArray[cCount] = tbl.tBodies[0].rows;
- cCount++;
- }
-
复制代码
应该改为:
- if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.four.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.four.checked) {
- checkedObjArray[cCount] = tbl.tBodies[0].rows[i];
- cCount++;
- }
-
复制代码
[ 本帖最后由 goatstudio 于 24-12-2006 01:15 AM 编辑 ] |
|
|
|
|
|
|
|
发表于 24-12-2006 01:24 AM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 25-12-2006 03:23 PM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|