此前的版本,只支持一个页面一次使用,后来我结合prototype等js的思想,稍做改进,使之对象封闭性进一步加强.同一页面可多次实例化显示数据.并且保存其实例句柄.
http.js的内容也封装到
XMLDATA 内.并且显示内容格式的可控性更好些,不过还是不很理想
pageSize : 12, 分页条数
showPages : true, 是否显示分页
container : "showXml", 显示数据容器(如:<div id="showXml"></div>)
pageadd : "pageadd", 保存当前页号的容器(<input type="hidden" id="pageadd" name="pageadd"/>)
next : "next", 显示下一页链接的容器
next2 : "next2", 显示最后一页的容器
pre : "pre", 显示上一页链接的容器
pre2 : "pre2",显示第一页链接容器
pagecount : "pagecount",保存总页数的容器
pagination : "pagination",
aClass : "",链接CSS样式
tbHead : "",头
tbFoot : "",尾
tdHeight : -1 行高
这些都可在实例化时可改变的项,如:
new XMLDATA(xmlurl,{showPages:false,tdHeight:24});
1 String.prototype.replaceAll = stringReplaceAll;
2 function stringReplaceAll(AFindText,ARepText){
3 var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");
4 return this.replace(raRegExp,ARepText);
5 }
6 XMLDATA = Class.create();
7 Object.extend(XMLDATA,{instance:new Array(),count:0,literals : new Array("确定要删除?","确定要执行操作?"),
8 paginHTML : "<input type=\"hidden\"id=\"search_category\"/> <input type=\"hidden\" id=\"pageadd\" value=\"0\"/><span id=\"pagecount\"></span> <span id=\"pre2\"><A href=\"javaScript:#{ins}.searchclass();\" class=\"cn12a gry\" >[首页]</A></span> <span id=\"pre\"><A href=\"javaScript:#{ins}.list('sub');\" class=\"cn12a gry\">[上一页]</A></span> <span id=\"next\"><A href=\"javaScript:#{ins}.list('in');\" class=\"cn12a gry\">[下一页]</A></span> <span id=\"next2\"><A href=\"javaScript:#{ins}.list(#{ins}.maxPage);\" class=\"cn12a gry\">[末页]</A></span>",
9 clear:function(){for(i=0;i<this.count;i++){this.instance[i]=null;}this.instance = new Array();this.count=0;}});
10 XMLDATA.prototype = {
11 maxPage : 1,data : "",num : 0,page : 0,url : "",handle : -1,
12 /******************* http *****************************/
13 xmlHttp : null,
14 callBack : function (){},
15 createXMLHttpRequest : function (){
16 if(window.ActiveXObject){
17 this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
18 }
19 else if(window.XMLHttpRequest){
20 this.xmlHttp = new XMLHttpRequest();
21 }
22 },
23 get : function (url){
24 this.createXMLHttpRequest();
25 try{
26 this.xmlHttp.open("GET",url,true);
27 this.xmlHttp.onreadystatechange = this.startCallback.bind(this);
28 this.xmlHttp.send(null);
29 }catch(e){alert(e.message);}
30 },
31 startCallback : function (){
32 if(this.xmlHttp.readyState == 4){
33 if(this.xmlHttp.status == 200){
34 this.callBack();
35 }
36 }},
37 /******************* http *****************************/
38 setOptions: function(options) {
39 this.options = {
40 pageSize : 12,
41 showPages : true,
42 container : "showXml",
43 pageadd : "pageadd",
44 next : "next",
45 next2 : "next2",
46 pre : "pre",
47 pre2 : "pre2",
48 pagecount : "pagecount",
49 pagination : "pagination",
50 aClass : "",
51 tbHead : "",
52 tbFoot : "",
53 tdHeight : -1
54 }
55 Object.extend(this.options, options || {});
56 },
57 initialize : function(baseUrl,options) {
58 XMLDATA.instance[XMLDATA.count++]=this;
59 this.handle = XMLDATA.count - 1;
60 this.baseUrl = baseUrl;
61 this.url = baseUrl;
62 this.setOptions(options);
63 if(this.options.showPages){this.$(this.options.pagination).innerHTML = XMLDATA.paginHTML.replaceAll("#{ins}","XMLDATA.instance["+this.handle+"]");}
64 this.list();
65 },
66 load : function(){
67 if(this.data == "Microsoft"){
68 var doc = new ActiveXObject( "Microsoft.XMLDOM" );
69 doc.async = false;
70 doc.load(this.url);
71 this.parse(doc);
72 }else if(""!=this.data){
73 var p = new DOMParser();
74 try{
75 var doc = p.parseFromString(this.data,"text/xml");
76 }catch(e){return;}
77 this.parse(doc);
78 }
79 },
80 initDom : function (){
81 if(window.DOMParser)//firefox内核的浏览器
82 {
83 this.callBack = function (){
84 this.data = this.xmlHttp.responseText;
85 this.load();
86 };
87 this.get(this.url);
88 return true;
89 }
90 else if( window.ActiveXObject )//ie内核的浏览器
91 {
92 this.data = "Microsoft";
93 this.load();
94 return true;
95 }
96 else
97 return false;
98 },
99 searchclass : function ()
100 {
101 this.setPageadd(0);
102 this.list();
103 },
104 getPageadd : function(){
105 return this.options.showPages?this.$(this.options.pageadd).value:this.page;
106 },
107 setPageadd : function(v){
108 if(this.options.showPages)
109 this.$(this.options.pageadd).value = v;
110 },
111 xmlRefresh : function (){
112 this.list(this.getPageadd());
113 },
114 $ : function (f){
115 return document.getElementById(f);
116 },
117 showMsg: function(msg){
118 this.$(this.options.container).innerHTML = msg;
119 },
120 execute : function (url){
121 this.showMsg("正在执行操作");
122 this.callBack = function (){
123 var r = this.xmlHttp.responseText;
124 if(r=="1")
125 this.showMsg("操作成功,正在加载数据");
126 else
127 this.showMsg("操作失败,正在加载数据");
128 setTimeout("XMLDATA.instance["+this.handle+"].xmlRefresh()",1000);
129 };
130 this.get(url);
131 },
132 list : function (fun){
133 this.showMsg("正在加载数据");
134 var n;
135 n = parseInt(this.getPageadd());
136 if(isNaN(parseInt(fun))){
137 if(fun == "sub"){n = n - 1;}else{n = n + 1;}
138 }else{
139 n = parseInt(fun);
140 }
141 this.displayPre(!(n <= 1));
142 this.displayNext(false);
143 this.setPageadd(n);
144 this.page = n;
145 this.url = this.baseUrl.replaceAll("#[page]",n).replaceAll("#[size]",this.options.pageSize);
146 if(!this.initDom()){
147 this.showMsg("没有找到任何数据!");
148 }else {
149
150 }
151 },
152 getNodeText : function(nodes,o){
153 return nodes.getElementsByTagName(o)[0]?nodes.getElementsByTagName(o)[0].firstChild.nodeValue:"";
154 },
155 nextNode : function(root,i){
156 return root.childNodes[i];
157 },
158 getAttribute : function(node,name){
159 for(i=0;i<node.attributes.length;i++)
160 if(name==node.attributes[i].name)
161 return node.attributes[i].value;
162 return null;
163 },
164 getNodeName : function(node){
165 return node.nodeName;
166 },
167 getNodeValue : function(node){
168 return node.firstChild.nodeValue;
169 },
170 parse : function(xmldoc){
171 var n = this.page;
172 var xmlroot = xmldoc.documentElement;
173 var showStr = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
174 var j,i,num;
175 var node = xmlroot.childNodes.length;
176 var nodes;
177 var nodeId;
178 i = 0;
179 while(i != node){
180 showStr += "<tr>";
181 nodes = this.nextNode(xmlroot,i);
182 if(i == 0)
183 {
184 this.maxPage = this.getNodeText(nodes,"page");
185 this.options.pageSize = parseInt(this.getNodeText(nodes,"size"));
186 this.displayNext(!(node<this.options.pageSize));
187 if(this.options.showPages){this.$(this.options.pagecount).innerHTML = '第 '+n+' 页'+'(共'+this.getNodeText(nodes,"page")+'页)';}
188 var hiddiv = (this.getNodeText(nodes,"hiddiv")=="true");
189 }
190 this.displayNext(this.maxPage>1 && n<this.maxPage);
191 if(nodes == null)
192 {
193 showStr += '</tr></table>';
194 this.showMsg(showStr);
195 return;
196 }
197 if(i == 0){showStr += this.options.tbHead;}//this.getNodeText(nodes,"head");}
198 j=0;
199 nodes_its = nodes.childNodes;
200 num = nodes_its.length;
201 while(j != num){
202 nnn = this.nextNode(nodes,j);
203 j++;
204 if(nnn == null)
205 {
206 break;
207 }
208 if(this.getNodeName(nnn)!="pro"){
209 if(this.getNodeName(nnn)=="id"){nodeId=this.getNodeValue(nnn);}
210 continue;}
211 if(this.options.tdHeight==-1){showStr += "<td>";}else{
212 showStr += "<td height=\""+this.options.tdHeight+"\">";
213 }
214 if(this.getAttribute(nnn,"url")!=null){
215 showStr += "<a ";
216 if(this.getAttribute(nnn,"confirm")=="true"){
217 showStr +="href=\"javaScript:;\" onClick=\"XMLDATA.instance["+this.handle+"].confirm('"+this.getAttribute(nnn,"url")+"',XMLDATA.literals["+this.getAttribute(nnn,"literal")+"],"+this.handle+");\"";
218 }else if(this.getAttribute(nnn,"onclick")=="true"){
219 showStr +="href=\"javaScript:;\" onClick=\""+this.getAttribute(nnn,"url")+"\"";
220 }else{
221 showStr +="href=\""+this.getAttribute(nnn,"url")+"\" target='"+(this.getAttribute(nnn,"open")=="1"?"_blank'":"'");
222 }
223 showStr += "class=\""+this.options.aClass+"\">";
224 showStr += this.getNodeValue(nnn);
225 showStr += "</a>";
226 }else{showStr += this.getNodeValue(nnn);}
227 showStr += '</td>';
228 }
229 showStr += '</tr>';
230 if(hiddiv){showStr += '<tr style=\"display:none"><td colspan=\"20\"'+(nodeId==''?'':' id=\"'+nodeId+'\"')+'>正在加载数据</td></tr>';};
231 i++;
232 }
233 if(showStr == '<table width="100%" border="0" cellspacing="0" cellpadding="0">')
234 {this.showMsg("没有找到任何数据!");if(this.page>1)this.list(this.page-1);}
235 else
236 {showStr += this.options.tbFoot+'</table>';this.showMsg(showStr);}
237 },
238 displayNext : function (flag){
239 if(flag)
240 {
241 if(this.options.showPages){
242 this.$(this.options.next).style.display = "";
243 this.$(this.options.next2).style.display = "";
244 }
245 }else
246 {
247 if(this.options.showPages){
248 this.$(this.options.next).style.display = "none";
249 this.$(this.options.next2).style.display = "none";
250 }
251 }
252 },
253 displayPre : function (flag){
254 if(flag)
255 {if(this.options.showPages){this.$(this.options.pre).style.display = "";
256 this.$(this.options.pre2).style.display = "";}}
257 else
258 {if(this.options.showPages){this.$(this.options.pre).style.display = "none";
259 this.$(this.options.pre2).style.display = "none";}}
260 },
261 confirm : function (url,literal,hInstance){
262 if(!confirm(literal)) return false;
263 XMLDATA.instance[hInstance].execute(url);
264 }
265 }
posted on 2007-04-30 18:34
盐巴 阅读(575)
评论(0) 编辑 收藏