黑夜的狼

沮丧吗,那就是一种无病呻吟!留恋它就是一种高度近视!目光应该放得更远一点! 别不想飞,只是要一步跨过太平洋!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
(function ($)
{
var methods = {
init:
function (options)
{
var table = this;
table.next().bind(
"click", table, methods.addRow);
var trArray = table.children().children("tr[ctrltype!='total']");
if (options.total)
{
// var newRow = methods.addRow(table);
// newRow.children().empty().removeClass().addClass("TableItemTotal").css("background-color", "#FFFF80"); ;
// newRow.attr("ctrltype", "total");
// newRow.children().eq(0).text("总计");
// var indexArray = [];
// var sTd = trArray.eq(0).find("td[ctrltype='sum']").each(function ()
// {
// indexArray.push($(this).index());
// }); ;
// options.sumIndex = indexArray;
// methods.setTotal(options);
var newRow = methods.addRow(table);
newRow.children().empty().removeClass().addClass(
"TableItemTotal").css("background-color", "#FFFF80"); ;
newRow.attr(
"ctrltype", "total");
newRow.children().eq(
0).text("总计");
options.table
= table;
methods.initTotal(options);
}
trArray.each(
function ()
{
var tdArray = $(this).children();
tdArray.eq(tdArray.length
- 1).children("a").bind("click", options, methods.deleteRow);
tdArray.eq(tdArray.length
- 2).children("a").bind("click", options, methods.saveData);
var dateControl = tdArray.find(":text[ctrltype='date']");
if (dateControl.length > 0)
dateControl.datepick(
'destroy');
});
var pmKey = trArray.eq(1).find("#pmKey");
if (trArray.length <= 2 && (pmKey.val() == "0" || pmKey.val() == ""))
{
trArray.eq(
1).find("#timeSpan").attr("value", (new Date()).valueOf());
methods.setEdit(trArray.eq(
1).find("a:contains('编辑')"));
}
},
initTotal:
function (options)
{
var indexArray = [];
var trArray = options.table.children().children("tr[ctrltype!='total']");
var sTd = trArray.eq(0).find("td[ctrltype='sum']").each(function ()
{
indexArray.push($(
this).index());
}); ;
options.sumIndex
= indexArray;
methods.setTotal(options);
},
setTotal:
function (event)
{
var options = event.data || event;
if (options.sumIndex)
{
var totalsumArray = [];
for (var i = 0; i < options.sumIndex.length; i++) totalsumArray.push(0);
var trArray = options.table.find("tr[ctrltype!='total']");
trArray.each(
function ()
{
for (var i = 0; i < options.sumIndex.length; i++)
{
var o = $(this).children().eq(options.sumIndex[i]).children();
var n = parseFloat(o.text() || o.val());
if (!isNaN(n))
{
totalsumArray[i]
+= n;
}
}
});
var totalRow = options.table.find("tr[ctrltype='total']");
for (var i = 0; i < options.sumIndex.length; i++)
totalRow.children().eq(options.sumIndex[i]).text(totalsumArray[i].toFixed(
5));
if (options.totalid)
{
$(options.totalid).val(totalsumArray[
0]);
}
}
},
setTableData:
function (obj)
{
var d = obj.data;
var table = this;
var trArray = table.find("tr[ctrltype!='total']");
if (trArray.length > 2)
{
trArray.slice(
2).remove();
}
if (!d || d.Rows.length == 0)
{
methods.addRow(table);
trArray.eq(
1).remove();
return;
}
for (var i = 0; i < d.Rows.length; i++)
{
methods.setRowData(methods.addRow(table), d.Rows[i]);
}
trArray.eq(
1).remove();
if (obj.total)
{
var op = {};
op.table
= table;
op.totalid
= obj.totalid;
methods.initTotal(op);
}
},
setRowData:
function (tableRow, dataRow)
{
tableRow.find(
"input[colName]").each(function ()
{
if (dataRow[this.colName])
{
this.value = dataRow[this.colName];
}
else if (this.ctrltype == "num")
{
this.value = "0";
}
});
tableRow.find(
"label[colName],span[colName]").each(function ()
{
this.innerText = dataRow[this.colName];
});
tableRow.find(
"select[colName]").each(function ()
{
var sColArray = this.colName.split(',');
this.value = dataRow[sColArray[0]];
$(
this).next("label").text(dataRow[sColArray[1]]);
});
var cells = tableRow.children();
methods.setShow(cells.eq(cells.length
- 2).children("a"));
},
setEdit:
function ($source)
{
var row = $source.parent().parent();
$source.text(
"保存");
row.find(
"select").each(function ()
{
var s = $(this);
s.css({ display:
"inline" });
s.next(
"label").css({ display: "none" });
var op = s.find("option[text='" + s.next("label").text() + "']");
if (op.length > 0)
{
op[
0].selected = true;
}
});
row.find(
":text").each(function ()
{
var txt = $(this);
txt.removeClass();
txt.addClass(
"textbox");
txt.attr(
"readonly", false);
if (txt.attr("ctrltype") == "date")
txt.datepick();
});
},
setShow:
function ($source)
{
var row = $source.parent().parent();
$source.text(
"编辑");
row.find(
":text").each(function ()
{
var $this = $(this);
$
this.addClass("textboxlabel");
$
this.attr("readonly", true);
if ($this.attr("ctrltype") == "date")
$
this.datepick('destroy');
});
row.find(
"select").each(function ()
{
var s = $(this);
s.next(
"label").text(s.find("option:selected").text());
s.next(
"label").css({ display: "inline" });
s.css({ display:
"none" });
});
//row.find("label").css({ display: "inline" });
},
saveData:
function (paramJson)
{
var source = $(this);
if (source.text() == "编辑")
{
methods.setEdit(source);
return;
}
var saveCheckFlag = true;
if (paramJson.data.onsave)
{
saveCheckFlag
= paramJson.data.onsave(source);
}
if (saveCheckFlag&&CheckForm(source.parent().parent()))
{
var row = source.parent().parent();
var model = {};
row.find(
":input[colName]").each(function ()
{
if (this.ctrltype == "date")
{
var dArray = this.value.split("-");
var d = new Date(dArray[0], dArray[1] - 1, dArray[2], new Date().getHours() + 8);
eval(
"model." + this.colName + "=d");
}
else if (this.ctrltype == "num")
{
if (this.value == "")
{
eval(
"model." + this.colName + "=0");
}
else
{
eval(
"model." + this.colName + "=" + parseFloat(this.value));
}
}
else
{
eval(
"model." + this.colName + "=\"" + this.value + "\"");
}

});
// tableRow.find("label[colName],span[colName]").each(function ()
// {
// eval("model." + this.colName + "=" + this.innerText);
// });
row.find("select[colName]").each(function (paramJson)
{
var colArray = this.colName.split(",");
eval(
"model." + colArray[0] + "=\"" + this.options[this.selectedIndex].value + "\"");
eval(
"model." + colArray[1] + "=\"" + this.options[this.selectedIndex].text + "\"");
});
paramJson.data.save.call(paramJson.data.obj, model, row.find(
"#timeSpan").attr("value"), methods.callBack);
methods.setShow(source);
methods.setTotal(paramJson);
}

},
clear:
function (row)
{
row.find(
"input[ctrltype!='notclear'],select").attr("value", "").css("background-color", "white");
row.find(
"label[colName],span[colName]").text("");
row.find(
"#pmKey").attr("value", "0");
row.find(
"#timeSpan").attr("value", (new Date()).valueOf());
row.children(
"td").css("background-color", "white");
},
addRow:
function (event)
{
var table = event.data[0] ? event.data : event;
var trArray = table.children().children("tr[ctrltype!='total']");
var lastTr = trArray.eq(trArray.length - 1);
var newTr = lastTr.clone(true);
methods.clear(newTr);
methods.setEdit(newTr.children().eq(newTr.children().length
- 2).find("a"));
newTr.insertAfter(lastTr);
return newTr;
},
deleteRow:
function (event)
{
if (confirm("确定删除吗?"))
{
var row = $(this).parent().parent();
var keyValue = row.find("#pmKey").attr("value");
var timeSpan = row.find("#timeSpan").attr("value");
if ((keyValue != "0" && keyValue != "") || timeSpan != "")
{
event.data.del.call(event.data.obj, keyValue, timeSpan, methods.callBack);
}
if (row.siblings("tr[ctrltype!='total']").length <= 1)
{
methods.clear(row);
methods.setEdit($(
this).parent().prev().children("a"));
}
else
{
row.remove();
}
methods.setTotal(event);
}
},
callBack:
function (res)
{
switch (res.value)
{
// case 0:
// alert("添加成功!");
// break;
// case 1:
// alert("修改成功!");
// break;
// case 2:
// alert("删除成功!");
// break;
case -1:
ReLogin();
break;
case 3:
alert(
"主键没有找到!");
break;
default:
break;
}
}
};
$.fn.TableOperate
= function (method)
{
if (this.length == 0) return;
if (methods[method])
{
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if (typeof method === 'object' || !method)
{
return methods.init.apply(this, arguments);
}
else { $.error('Method ' + method + ' does not exist on jQuery.tableoperate'); }
};
})(jQuery);
posted on 2011-04-16 06:27  anncesky  阅读(269)  评论(0)    收藏  举报