123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
|
var arrOrgData = [
{ "Id" : 1, "OrgName" : "红十字会" },
{ "Id" : 2, "OrgName" : "壹基金" },
{ "Id" : 3, "OrgName" : "中华慈善总会" },
{ "Id" : 4, "OrgName" : "中国扶贫基金会" }
]; arrOrgData.getOrgById = function (id) {
for (
var i = 0; i <
this .length; i++) { if (
this [i].Id == id) { return this
[i]; }; }; }; var arrDetailData = [
{ "Id" : 1, "Name" : "成龙" , "OrgId" : 1, "Money" : "1000" , "Time" : "2013 - 07 - 08" }, { "Id" : 2, "Name" : "肥龙" , "OrgId" : 2, "Money" : "2000" , "Time" : "2013 - 07 - 08" }, { "Id" : 3, "Name" : "瘦龙" , "OrgId" : 3, "Money" : "3000" , "Time" : "2013 - 07 - 08" }, { "Id" : 4, "Name" : "傻龙" , "OrgId" : 4, "Money" : "4000" , "Time" : "2013 - 07 - 08" } ]; arrDetailData.deleteById = function (id) {
for (
var i = 0; i <
this .length; i++) { if (
this [i].Id == id) { for (
var j = i; j <
this .length - 1; j++) { this [j] = this [j + 1]; }; this .length--; break ; }; }; }; arrDetailData.update = function (model) {
for (
var i = 0; i <
this .length; i++) { if (
this [i].Id == model.Id) { this [i] = model; break ; }; }; }; var modelId=arrDetailData.length;
arrDetailData.addModel = function (model) {
modelId++; model.Id = modelId; this [ this .length] = model; return model;
}; arrDetailData.getSearchData = function (id) {
if (id==-1){ return this
; }; var arrSearch =
new Array();
for (
var i = 0; i <
this .length; i++) { if ( this [i].OrgId==id){ arrSearch[arrSearch.length]= this [i]; }; }; return arrSearch;
}; arrDetailData.pageIndex = 1; arrDetailData.pageCount = 5; arrDetailData.pages=0; arrDetailData.getPageData = function () {
var pageData =
new Array();
for (
var i = (
this .pageIndex - 1) * this .pageCount; i < this .pageIndex * this .pageCount; i++) { if (
this [i]) { pageData[pageData.length] = this [i]; }; } return pageData;
}; function loadOrgData(element) {
for (
var i = 0; i < arrOrgData.length; i++) {
var opt =
new Option(arrOrgData[i].OrgName, arrDetailData[i].Id);
element.options.add(opt); }; }; function loadDetailData() {
for (
var i = 0; i < arrDetailData.length; i++) {
addTr(arrDetailData[i]); } }; function loadDataToTb(arr) {
for (
var i = 0; i < arr.length; i++) {
addTr(arr[i]); }; }; function addTr(model) {
var tb = getElement(
"tbList" ); var tr = tb.insertRow(-1);
tr.insertCell(-1).innerHTML = model.Id; tr.insertCell(-1).innerHTML = model.Name; var td = tr.insertCell(-1);
td.setAttribute( "OrgId" , model.OrgId); td.innerHTML = arrOrgData.getOrgById(model.OrgId).OrgName; tr.insertCell(-1).innerHTML = model.Money; tr.insertCell(-1).innerHTML = model.Time; tr.insertCell(-1).innerHTML = "<a href='#' onclick='setUpdateState(this)'>修改</a> <a href='#' onclick='deleteRow(this)'>删除</a>" ; }; function getElement(id) {
return document.getElementById(id);
}; function deleteRow(element) {
if (GlobalUpdateTr !=
null ) { rollBack(GlobalUpdateTr); }; if (!confirm(
"确定删除吗?" )) { return ; }; var delTr = element.parentNode.parentNode;
delTr.parentNode.removeChild(delTr); arrDetailData.deleteById(delTr.childNodes[0].innerHTML); }; var inputPersonName = document.createElement(
"input" ); inputPersonName.type = "text" ; var inputMoney = document.createElement(
"input" ); inputMoney.type = "text" ; var inputTime = document.createElement(
"input" ); inputTime.type = "text" ; var selectOrg = document.createElement(
"select" ); var GlobalUpdateTr =
null ; function setUpdateState(element) {
if (GlobalUpdateTr !=
null ) { rollBack(GlobalUpdateTr); }; GlobalUpdateTr = element.parentNode.parentNode; txtToInput(GlobalUpdateTr.childNodes[1], inputPersonName); txtToInput(GlobalUpdateTr.childNodes[3], inputMoney); txtToInput(GlobalUpdateTr.childNodes[4], inputTime); txtToSelect(GlobalUpdateTr.childNodes[2], selectOrg); GlobalUpdateTr.childNodes[5].innerHTML = "<a href='#' onclick='update(this)'>确定</a> <a href='#' onclick='exitUpdateState(this)'>取消</a>" ; }; function txtToInput(td,element) {
element.value = td.innerHTML; td.setAttribute( "oldValue" ,td.innerHTML); td.appendChild(element); if (td.childNodes[1]) {
td.removeChild(td.childNodes[0]); }; }; function txtToSelect(td, element) {
td.appendChild(element); td.removeChild(td.childNodes[0]); element.value = td.getAttribute( "OrgId" ); }; function exitUpdateState(element) {
var cancelTr = element.parentNode.parentNode;
rollBack(cancelTr); GlobalUpdateTr = null ; }; function rollBack(element) {
element.childNodes[1].innerHTML = element.childNodes[1].getAttribute( "oldValue" ); element.childNodes[3].innerHTML = element.childNodes[3].getAttribute( "oldValue" ); element.childNodes[4].innerHTML = element.childNodes[4].getAttribute( "oldValue" ); element.childNodes[2].removeChild(selectOrg); var orgId = element.childNodes[2].getAttribute(
"OrgId" ); element.childNodes[2].innerHTML = arrOrgData.getOrgById(orgId).OrgName; element.childNodes[5].innerHTML = "<a href='#' onclick='setUpdateState(this)'>修改</a> <a href='#' onclick='deleteRow(this)'>删除</a>" ; }; function update(element) {
var updateTr = element.parentNode.parentNode;
updateTr.childNodes[1].innerHTML = inputPersonName.value; updateTr.childNodes[3].innerHTML = inputMoney.value; updateTr.childNodes[4].innerHTML = inputTime.value; updateTr.childNodes[2].removeChild(selectOrg); updateTr.childNodes[2].innerHTML = arrOrgData.getOrgById(selectOrg.value).OrgName; updateTr.childNodes[2].setAttribute( "OrgId" , selectOrg.value); updateTr.childNodes[5].innerHTML = "<a href='#' onclick='setUpdateState(this)'>修改</a> <a href='#' onclick='deleteRow(this)'>删除</a>" ; var model = {
"Id" : updateTr.childNodes[0].innerHTML, "Name" : inputPersonName.value, "OrgId" : selectOrg.value, "Money" : inputMoney.value, "Time" : inputTime.value }; arrDetailData.update(model); GlobalUpdateTr = null ; }; window.onload = function () {
loadOrgData(getElement( "selSearchOrg" )); loadOrgData(getElement( "selAddOrg" )); loadOrgData(selectOrg); loadDetailData(); getElement( "btnAdd" ).onclick = function () {
var model = {
"Name" : getElement( "txtName" ).value, "OrgId" : getElement( "selAddOrg" ).value, "Money" : getElement( "txtMoney" ).value, "Time" : getElement( "txtDate" ).value }; model = arrDetailData.addModel(model); addTr(model); }; getElement( "btnSearch" ).onclick = function () {
var tbList = getElement(
"tbList" ); for (
var i = tbList.rows.length - 1; i >= 1; i--) {
tbList.deleteRow(i); } var searchId = getElement(
"selSearchOrg" ).value; var arrSearchData = arrDetailData.getSearchData(searchId);
loadDataToTb(arrSearchData); }; getElement( "btnnextPage" ).onclick = function () {
if (arrDetailData.length == 0) {
alert( "没有数据" ); }; arrDetailData.pages = arrDetailData.length % arrDetailData.pageCount != 0 ? (arrDetailData.length - arrDetailData.length % arrDetailData.pageCount) / arrDetailData.pageCount + 1 : arrDetailData.length / arrDetailData.pageCount; if (arrDetailData.pageIndex == arrDetailData.pages) {
alert( "最后一页啦" ); return ; }; var tbList = getElement(
"tbList" ); for (
var i= tbList.rows.length-1;i>=1; i--) {
tbList.deleteRow(i); } arrDetailData.pageIndex++; var arrPage = arrDetailData.getPageData();
loadDataToTb(arrPage); }; getElement( "btnprePage" ).onclick = function () {
if (arrDetailData.length == 0) {
alert( "没有数据" ); }; arrDetailData.pages = arrDetailData.length % arrDetailData.pageCount != 0 ? (arrDetailData.length - arrDetailData.length % arrDetailData.pageCount) / arrDetailData.pageCount + 1 : arrDetailData.length / arrDetailData.pageCount; if (arrDetailData.pageIndex==1){ alert( "这个第一页" ); return ; }; var tbList = getElement(
"tbList" ); for (
var i = tbList.rows.length - 1; i >= 1; i--) {
tbList.deleteRow(i); } arrDetailData.pageIndex--; var arrPage = arrDetailData.getPageData();
loadDataToTb(arrPage); }; }; |