全编辑WebGrid控件LrcGrid(4)—— 脚本库和样式表
脚本库:包含了控件客户端操作的函数.包括:
将表格行切换到编辑模式的函数:chgEditRow(rowIndex,tab)()
将表格列切换到编辑模式的函数:chgEdit(colIndex,tab)
在客户端构造更新数据库的sql语句: BuildSql(tabName)
添加新记录的函数:AddRow(tab)
移除新添加行的函数: RemoveRow(tab)
全部js代码:
function chgEditRow(rowIndex,tab)
{
var tabBody = tab.tBodies[0];
var isE = 0;
//alert(rowIndex);
//alert(tabBody.rows[rowIndex].cells.length);
for(var i = 0;i< tabBody.rows[rowIndex].cells.length;i++)
{
//alert(tabBody.rows[rowIndex].cells[i].childNodes[0].id);
if(tabBody.rows[rowIndex].cells[i].hasChildNodes())
{
if(tabBody.rows[rowIndex].cells[i].childNodes[0].nodeType != 3)
{
if(tabBody.rows[rowIndex].cells[i].childNodes[0].id.indexOf('LrcTb_') != -1)
{
if(!tabBody.rows[rowIndex].cells[i].childNodes[0].disabled)
{
tabBody.rows[rowIndex].cells[i].childNodes[0].disabled = true;
tabBody.rows[rowIndex].cells[i].childNodes[0].className = "lrc_txt_hid";
}
else
{
tabBody.rows[rowIndex].cells[i].childNodes[0].disabled = false;
tabBody.rows[rowIndex].cells[i].childNodes[0].className = "lrc_txt_show";
}
}
else if(tabBody.rows[rowIndex].cells[i].childNodes[0].id.indexOf('LrcDrl_') != -1)
{
if(!tabBody.rows[rowIndex].cells[i].childNodes[0].disabled)
{
tabBody.rows[rowIndex].cells[i].childNodes[0].disabled = true;
}
else
{
tabBody.rows[rowIndex].cells[i].childNodes[0].disabled = false;
}
}
}
}
}
}
function chgEdit(colIndex,tab)
{
var tabBody = tab.tBodies[0];
var oneRow;
if(tabBody.rows[1].cells[colIndex].childNodes[0].disabled)
{
for(var i=1;i<tabBody.rows.length-1;i++)
{
oneRow = tabBody.rows[i];
if(oneRow.cells[colIndex].childNodes[0].id.indexOf('LrcTb_') != -1)
{
oneRow.cells[colIndex].childNodes[0].disabled = false;
oneRow.cells[colIndex].childNodes[0].className = "lrc_txt_show";
}
else if(oneRow.cells[colIndex].childNodes[0].id.indexOf('LrcDrl_') != -1)
{
oneRow.cells[colIndex].childNodes[0].disabled = false;
}
}
}
else
{
for(var i=1;i<tabBody.rows.length-1;i++)
{
oneRow = tabBody.rows[i];
if(oneRow.cells[colIndex].childNodes[0].id.indexOf('LrcTb_') != -1)
{
oneRow.cells[colIndex].childNodes[0].disabled = true;
oneRow.cells[colIndex].childNodes[0].className = "lrc_txt_hid";
}
else if(oneRow.cells[colIndex].childNodes[0].id.indexOf('LrcDrl_') != -1)
{
oneRow.cells[colIndex].childNodes[0].disabled = true;
}
}
}
}
function BuildSql(tabName)
{
var UpdSql = '';
var tabBody = tabName.tBodies[0];
var oneRow;
var len = tabBody.rows.length;
var celLen = tabName.rows[0].cells.length;
var isadd = tabName.getAttribute("isadd");
var isdel = tabName.getAttribute("isdel");
if(isdel == 'True')
{
celLen = celLen -1;
}
var hidIndex = 0;
if(isadd == 'True')
{
hidIndex = 2;
}
var tabN = tabBody.rows[len-1].cells[hidIndex].childNodes[1].value;
var colN = tabBody.rows[len-1].cells[hidIndex].childNodes[2].value;
var colNA = colN.split(",");
var priK = tabBody.rows[len-1].cells[hidIndex].childNodes[3].value;
var priKA = priK.split(",");
var editCol = tabBody.rows[len-1].cells[hidIndex].childNodes[4].value;
var editColA;
if(editCol == 'LrcAllEdit')
{
var len = colNA.length - priKA.length;
editColA = new Array(len);
var indexE = 0;
for(var i = 0 ; i< colNA.length ; i++)
{
var isPri = 0;
for(var j=0;j<priKA.length;j++)
{
if(priKA[j] == i)
{
isPri = 1;
}
}
if(isPri == 0)
{
editColA[indexE] = i;
indexE++;
}
}
}
else
{
editColA = editCol.split(",");
}
var strUpd = "UPDATE " + tabN + " SET ";
var strIns = "INSERT " + tabN + " (" + colN + ") VALUES ( ";
for(var i=1;i<tabBody.rows.length-1;i++)
{
oneRow = tabBody.rows[i];
//更新语句
if(oneRow.id != "lrcNewRow")
{
UpdSql += strUpd;
for(var j=0;j<editColA.length;j++)
{
UpdSql += colNA[editColA[j]];
UpdSql += " = '";
if(oneRow.cells[editColA[j]].firstChild.id.indexOf('LrcDrl_') != -1)
{
UpdSql += oneRow.cells[editColA[j]].firstChild.options[oneRow.cells[editColA[j]].firstChild.selectedIndex ].value;
}
else
{
UpdSql += oneRow.cells[editColA[j]].firstChild.value;
}
if(j != editColA.length - 1)
{
UpdSql += "', ";
}
else
{
UpdSql += "' ";
}
}
UpdSql += " WHERE ";
for(var k=0;k<priKA.length;k++)
{
UpdSql += colNA[priKA[k]];
UpdSql += " = '";
UpdSql += oneRow.cells[priKA[k]].firstChild.nodeValue;
if(k != priKA.length - 1)
{
UpdSql += "' AND ";
}
else
{
UpdSql += "' \r";
}
}
}
else //插入语句
{
UpdSql += strIns;
for(var k=0;k<celLen;k++)
{
if(oneRow.cells[k].firstChild.id.indexOf('LrcDrl_') != -1)
{
var selV = oneRow.cells[k].firstChild.options[oneRow.cells[k].firstChild.selectedIndex].value;
UpdSql = UpdSql + "'" + selV + "'";
}
else
{
UpdSql = UpdSql + "'" + oneRow.cells[k].firstChild.value + "'";
}
if(k+1 != celLen)
{
UpdSql += ",";
}
else
{
UpdSql += ") \r";
}
}
}
}
var tabBody = tabName.tBodies[0];
var len = tabBody.rows.length;
tabBody.rows[len-1].cells[hidIndex].childNodes[5].value = UpdSql;
return true;
}
function AddRow(tab)
{
var tabBody = tab.tBodies[0];
var oneRow;
var rowLen = tab.rows.length;
var celLen = tab.rows[0].cells.length;
var isadd = tab.getAttribute("isadd");
var isdel = tab.getAttribute("isdel");
if(isdel == 'True')
{
celLen = celLen -1;
}
var newrow = tabBody.insertRow(rowLen-1);
newrow.id = "lrcNewRow";
// for(var i = 0;i< celLen;i++)
// {
// var newcel = newrow.insertCell();< BR> // var str="<input type=textname=\"newT\"class=\"lrc_txt_normal\"onfocus=\"this.className='lrc_txt_edit';this.parentNode.parentNode.bgColor='#C1D2EE';\" onblur=\"this.className='lrc_txt_normal';this.parentNode.parentNode.bgColor='#FFFFFF';\"/>";
// newcel.innerHTML =str;
// }
for(var i = 0;i<celLen;i++)
{
var newcel = newrow.insertCell();
if(tabBody.rows[1].cells[i].childNodes[0].nodeType != 3)
{
if(tabBody.rows[1].cells[i].firstChild.id.indexOf('LrcDrl_') != -1)
{
var innerT = tabBody.rows[1].cells[i].innerHTML;
newcel.innerHTML = innerT;
newcel.firstChild.disabled = false;
}
else
{< BR> var str="<input type=textname=\"newT\"class=\"lrc_txt_normal\"onfocus=\"this.className='lrc_txt_edit';this.parentNode.parentNode.bgColor='#C1D2EE';\" onblur=\"this.className='lrc_txt_normal';this.parentNode.parentNode.bgColor='#FFFFFF';\"/>";
newcel.innerHTML =str;
}
}
else
{< BR> var str="<input type=textname=\"newT\"class=\"lrc_txt_normal\"onfocus=\"this.className='lrc_txt_edit';this.parentNode.parentNode.bgColor='#C1D2EE';\" onblur=\"this.className='lrc_txt_normal';this.parentNode.parentNode.bgColor='#FFFFFF';\"/>";
newcel.innerHTML =str;
}
}
}
function RemoveRow(tab)
{
var tabBody = tab.tBodies[0];
var oneRow;
var rowLen = tab.rows.length;
if(tabBody.rows[rowLen-2].id == "lrcNewRow")
{
tabBody.deleteRow(rowLen-2);
}
}
样式表文件:
.lrc_txt_edit
{
border-width:medium;
border-style:groove;
font-weight:bolder;
background-color:Yellow;
width:95;
}
.lrc_txt_normal
{
border-style:groove;
border-width:thin;
font-weight:normal;
background-color:#f5f7fb;
width:95;
}
.lrc_txt_hid
{
border-style:none;
width:95;
background:url(images/txt_back.gif);
}
.lrc_txt_show
{
border-style:groove;
width:95;
background-color:#ffffff;
}