DWR使用体会2:获取数据库表数据,使用addRows生成列表

DWRUtil.addRows()

语法:

DWRUtil.addRows(id, array, cellfuncs, [options]);

描述:
向指定id的table元素添加行。它使用数组中的每一个元素在table中创建一行。然后用cellfuncs数组中的没有函数创建一个列。单元格是依次用cellfunc根据没有数组中的元素创建出来的。

DWR1.1开始,addRows()也可以用对象做为数据。如果你用一个对象代替一个数组来创建单元格,这个对象会被传递给cell函数。

你可以写一些像这样的伪代码:

for each member in array  for each function in cellfuncs    create cell from cellfunc(array[i])

参数:

  • id: table元素的id(最好是tbody元素的id)
  • array: 数组(DWR1.1以后可以是对象),做为更新表格数据。
  • cellfuncs: 函数数组,从传递过来的行数据中提取单元格数据。
  • options: 一个包含选项的对象(见下面)

选项包括:

  • rowCreator: 一个用来创建行的函数(例如,你希望个tr加个css). 默认是返回一个document.createElement("tr")
  • cellCreator: 一个用来创建单元格的函数(例如,用th代替td). 默认返回一个document.createElement("td")

实际使用:

1.根据输入查询,列表显示中加入删除按钮,输入参数动态检查功能。

类文件:UserPostInfoList

package com.ucbbs.dwr;

import java.util.*;
import java.sql.*;
import com.ucbbs.db.*;

public class UserPostInfoList {
 
 private static final String getUserTopicInfoList = "select * from t_Topic where PostUID=? and PostTime<getDate()";

 List lUserTopic = null;
 List lUserPost = null;
 
 public List getUserTopicList(int iUID){
  DbTrans DBSQL = null;
  CallableStatement cstmt = null;
  ResultSet rs = null;  
  
  try{
   lUserTopic = new Vector();
   DBSQL = new DbTrans();
   cstmt = DBSQL.conn.prepareCall(getUserTopicInfoList);
   cstmt.setInt(1,iUID);
   rs = cstmt.executeQuery();
   while(rs.next()){

    String mystr[] = {rs.getString("TopicID"),rs.getString("BoardsID"),rs.getString("Title"),"<input name='bt' type='button' value='删除' onclick='mySubmit("+rs.getString(1)+")'/>"};
       lUserTopic.add(mystr); 
   
    lUserTopic.add(mystr);   
    
   }
  }
  catch(SQLException e ){
   /*............省略..........*/
  }
  finally{
   /*............省略..........*/
  }
  return lUserTopic;  
 }

 
 public boolean delTopic(int iID){
  DbTrans DBSQL = null;
  CallableStatement cstmt = null;
  boolean isOK = false;
  
  try{
   DBSQL = new DbTrans();
   cstmt = DBSQL.conn.prepareCall(DEL_Topic);
   cstmt.setInt(1,iID);
   cstmt.executeUpdate();
   isOK = true;
   
  }
  catch(SQLException e){
   /*............省略..........*/
  }
  finally{
   /*............省略..........*/
  }
  return isOK;
 }
 
 
 
 
 public boolean isNumber(String validString){
  boolean isOK = false;
  
  try{
   
      if (validString==null) return false;
        byte[] tempbyte=validString.getBytes();
        for(int i=0;i<validString.length();i++) {
            //by=tempbyte[i];
            if((tempbyte[i]<48)||(tempbyte[i]>57)){
                isOK = false;
            }
            else{
             isOK = true;
            }
        }
           
  }
  catch(Exception e){
   
  }
  return isOK;
  
 }

}

页面:

<%@ page language="java" contentType="text/html; charset=gb2312" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<script src='/dwr/interface/uPostInfo.js'></script>
<script src='/dwr/engine.js'></script>
<script src='/dwr/util.js'></script>
<script>

var cellFuncs = [
  function(data) { return data[0]; }, //因为得到的返回值是数组,可以用此方式取值
  function(data) { return data[1]; },
  function(data) { return data[2];}
  ];

  function getList(){
  DWRUtil.removeAllRows("apartmentsbody");
  var id = document.all.strID.value;
  uPostInfo.getUserTopicList(id,fillt); 
     document.all.resultTable.style.display = '';
}
function fillt(list){DWRUtil.addRows("apartmentsbody",list,cellFuncs);
}

function checkID(){     //strID的onChange事件执行此检查操作,并将结果给回调函数
 uPostInfo.isNumber(replayID,DWRUtil.getValue("strID"));
}
function replayID(valid){  //参数1,检查结果;参数2,输入;参数3,信息显示ID;参数4,信息
  processReply(valid, "strID", "id-error", "请输入正确的号码");
}
function processReply(valid, id, errid, error) {
  if (valid) {
    DWRUtil.setValue(errid, "");
    $(id).style.color = "black";
  }
  else {
    DWRUtil.setValue(errid, error);
    $(id).style.color = "red";
 $(id).focus(); 
  }
}

</script></head>

<body>

<table width=350 border=0 cellpadding=0 cellspacing=1 height=44 align="center">
<tr>
      <td width=84 height="42"  align="center" bgcolor="#50C3FF"> 输入UC号: </td>
      <td width="168"  bgcolor="#CDEEFF"><input type="text" name="strID" onChange="checkID()"></td>
      <td width="94"  align="center" bgcolor="#CDEEFF"><input type="button" name="Submit" value="确定" onClick="getList()"></td><td width="101"  align="center" bgcolor="#CDEEFF"><span id="id-error" class="error">  </span></td><!-- 此行用来显示错误信息-->
    </tr>
</table>

<div id="resultTable">
<table width=350 border=1 cellpadding=0 cellspacing=1 height=44 align="center">
  <thead>
    <tr>
      <th width="60" height="20">TopicID</th>
      <th width="180" height="20">BoardsID</th>
      <th width="80" height="20">Title</th>
    </tr>
  </thead>
  <tbody id="apartmentsbody">

  </tbody>
 </table>
</div>
</body>
</html>

体会:

1.所参考的例子中,是使用类,向List添加对象,但是调试中却返回空.....只好修改为使用数组,这样所产生的性能问题....

2.回调函数的取值问题,也修改为数组操作。

 

例子2:实现下拉选择后,显示查询结果,点删除后自动更新列表。

页面

<%@ page language="java" contentType="text/html; charset=gb2312" %>
<%
  String sysid= request.getParameter("sysid");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>封禁用户查询</title>
  <script src='/dwr/interface/unPostUser.js'></script>
  <script src='/dwr/engine.js'></script>
  <script src='/dwr/util.js'></script>
  <script>

var cellFuncs = [
  function(data) { return data[0]; },
  function(data) { return data[1]; },
  function(data) { return data[2];},
  function(data) { return data[3]; },
  function(data) { return data[4]; },
  function(data) { return data[5]; }];


  function getList(){
  DWRUtil.removeAllRows("apartmentsbody");
  var id = document.all.unPostUID.value;
  var itype = document.all.type.value;
  unPostUser.getUnpostUserSel(id,itype,fillt); 
     //document.all.resultTable.style = '';
}
function fillt(list){
  //alert("2");
DWRUtil.addRows("apartmentsbody",list,cellFuncs);
}
function check(){
 var uid =document.all.unPostUID.value;
 if(uid==""){
   document.all.unPostUID.focus();
   alert("请输入要查询的号码");
 }
}
function mySubmit(strID){
 //document.all.unPostUID.value = strID; 
 //document.fform.submit();

unPostUser.setPostOK(strID,0,getList);//类添加删除方法后,在这里可以直接删除而不用提交
}
 
</script>
</head>

<body>
<form name=fform  action="SetUnpost_Sel.jsp?sysid=<%=sysid%>" method="post">
<table width=800 border=0 cellpadding=0 cellspacing=1 height=33 align="center">
  <tr>
    <td width=126 height="31"  align="center" bgcolor="#50C3FF"> 全区封禁用户查询</td>
    <td width="256"  bgcolor="#CDEEFF"> UC号:
      <input type="text" name="unPostUID"></td>
    <td width="334"  align="left" bgcolor="#CDEEFF">
  <select name="type" onFocus="check()" onChange="getList()">
    <option value="-1">-请选择查询类型-</option>
    <option value="1">---当前封禁---</option>
    <option value="2">---过期封禁---</option>
     </select></td>
  </tr>
</table>
<div id="resultTable">
  <table width=800 border=1 cellpadding=0 cellspacing=1 height=24 align="center">
    <thead>
    <tr bgcolor="#50C3FF">
    <th width="123"><font color="#FFFFFF"><strong>被封用户</strong></font></th>
    <th width="114"><font color="#FFFFFF">执罚者</font></th>
    <th width="135"><font color="#FFFFFF">被封原因</font></th>
    <th width="135"><font color="#FFFFFF">开始时间</font></th>
    <th width="131"><font color="#FFFFFF">结束时间</font></th>
 <th width="100"><font color="#FFFFFF">操作</font></th>
    </tr>
  </thead> 
  <tbody id="apartmentsbody">    
  </tbody>
</table>
</div>
</form> 
</body>
</html>

类文件

import java.sql.*;
import java.util.*;
/**省略**/

public class UnpostUserSel {

 private static final String GET_UNPOST_USER = "select UID,ManagerUID,UnpostReason,UnpostStartTime,UnpostEndTime,IsForever from t_Unpost where UID=? order by UID";

 private static final String GET_OVER_UNPOST = "select UID,ManagerUID,UnpostReason,UnpostStartTime,UnpostEndTime,IsForever from t_Unpost where UID =? and UnpostEndTime < getDate() order by UID";

private static final String SET_POST_OK = "{call pr_setPostOK(?,?)}";

 List userList = null;

 public List getUnpostUserSel(int iUID, int iType) {
  DbTrans DBSQL = null;
  CallableStatement cstmt = null;
  ResultSet rs = null;
  AdminUser myAdmin = new AdminUser();
  userList = new ArrayList();

  try {
   DBSQL = new DbTrans();
   if (iType == 1) {
    cstmt = DBSQL.conn.prepareCall(GET_UNPOST_USER);
   }
   if (iType == 2) {
    cstmt = DBSQL.conn.prepareCall(GET_OVER_UNPOST);
   }

   cstmt.setInt(1, iUID);
   rs = cstmt.executeQuery();

   if (rs.next()) {
    String str1 = "";
    String str2 = "";
    String str3 = "";
    str1 = rs.getString(1);
    str1 = myAdmin.getNickName(str1) + "【" + str1 + "】";
    str2 = rs.getString(2);
    str2 = myAdmin.getNickName(str2) + "【" + str2 + "】";
    if (rs.getInt(6) == 1) {
     str3 = "永久封禁";
    } else {
     str3 = rs.getString(5);
    }
    String str[] = { str1, str2, rs.getString(3), rs.getString(4),
      str3,
      "<input name='bt' type='button' value='解禁该用户' onclick='mySubmit("+rs.getString(1)+")'/>" };
    userList.add(str);
   } else {
    String str[] = {"无封禁记录"};
    userList.add(str);
   }
  }

  catch (SQLException e) {/**省略**/}

   finally { /**省略**/ }
    return userList;

 }

   public void setPostOK(String UID, String BoardsID) {
   
      DbTrans DBSQL = null;
   CallableStatement cstmt = null;

      try {
        DBSQL = new DbTrans();
        cstmt = DBSQL.conn.prepareCall(SET_POST_OK);
        cstmt.setString(1,UID);
        cstmt.setString(2,BoardsID);
        cstmt.executeUpdate();
      }
      catch (SQLException e) {/**省略**/}
      finally {/**省略**/}

}


 



你可能感兴趣的:(html,sql,jsp,css,DWR)