在图片附件中有效果图,可以先看一下.
先说下来由:因为不太喜欢zk的tree,也因为数据量实在太大,每次都用tree全部现实出来很耗费性能
所以就想到了google的搜索框,打算自己做一个.
整体思路是这样的:
做一个bandbox,里边包含一个textbox和一个listbox
textbox用于接收搜索的条件
listbox用于显示搜索的内容,选定后反填到bandbox中
step.1---页面(test.zul)
<window id="testWnd" title="test" border="normal"
use="com.test.TestWindow" height="95%">
<grid width="60%">
<columns>
<column label=""/>
<column label=""/>
</columns>
<rows>
<row height="40px" spans="1,2">
名称:
<hbox>
<!--
autodrop属性实现bandbox的自动弹出
constraint属性是对bandbox的验证,其中"no empty"是非空验证
如果你想定义验证后显示的内容只需要"no empty: 你想显示的信息"就可以了
-->
<bandbox id="gxsmc" readonly="true" autodrop="true"
constraint="no empty" width="300px">
<bandpopup>
<hbox>
<label value="输入代码或名称查找:"></label>
<!-- 用于输入搜索条件的textbox
为其添加onChang和onOK事件
目前这个版本需要输入完成后将textbox失去焦点或按回车键才能显示搜索内容
我想做个类似google那种效果的,一直没搞成,所有的事件都试过了
希望高手们帮忙看看
-->
<textbox id="term" onChange="testWnd.initTest(self.value)"
onOK="testWnd.initTest(self.value)"/>
</hbox>
<vbox>
<!-- 用于显示搜索内容的listbox -->
<listbox id="gxsList" width="320px" height="175px">
<listhead>
<listheader label="名称" width="300px" sort="auto" />
</listhead>
</listbox>
<!-- 用于分页的控件 -->
<paging id="gxsPag" pageSize="10"></paging>
</vbox>
</bandpopup>
</bandbox>
<image src="img/Centigrade-Widget-Icons/QuestionmarkButton-16x16.png" tooltip="title"
style="cursor:pointer" popup="title"/>操作说明
</hbox>
</row>
</rows>
</grid>
<popup id="title" width="300px">
<html>
单击左边输入框,输入管辖所代码或管辖所名称<br />
要求代码长度大于4位,名称长度大于2位<br />
输入完成后,按"回车(Enter)键"或"Tab"键选择
</html>
</popup>
</window>
step.2----建立页面use的后台类
public class TestWindow extends Window {
private static final long serialVersionUID = 1L;
public void initTest(final String value) {
//这么做有点笨了,主要是怕sql语句报错,dao中也用了PreparedStatementSetter,主要是以防万一
if (value.contains("'") || value.contains("_")) {
try {
Messagebox.show("包含非法字符","提示",Messagebox.OK,Messagebox.INFORMATION);
return ;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//限制输入的条件的长度大于4的时候才去数据查找数据
if (value.getBytes().length >= 4) {
//spring
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(
(ServletContext)getDesktop().getWebApp().getNativeContext());
DtgkService dtgkService = (DtgkService)ctx.getBean("dtgkService");
//获得总条数
int maxNum = dtgkService.getGxsInfoCountByInput(value);
Paging pag = (Paging)getFellow("gxsPag");
pag.setTotalSize(maxNum);
final int PAGE_SIZE = pag.getPageSize();
//显示数据
reDramGxsInfo(value, 0, PAGE_SIZE);
//注册onpaging事件
pag.addEventListener("onPaging", new EventListener() {
public void onEvent(Event event) {
PagingEvent pe = (PagingEvent) event;
int pgno = pe.getActivePage();//页数(从零计算)
int start=pgno * PAGE_SIZE;
int end = start+PAGE_SIZE;
// System.out.println("pgno"+pgno+"\tstart+"+start+"\tend"+end);
reDramGxsInfo(value, start, end);
}
});
}
}
public void reDramGxsInfo(String value, int firstNum, int maxNum) {
final Listbox gxsBox = (Listbox) this.getFellow("gxsList");
gxsBox.getItems().clear();
final Bandbox combo = (Bandbox) this.getFellow("gxsmc");
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(
(ServletContext)getDesktop().getWebApp().getNativeContext());
DtgkService dtgkService = (DtgkService)ctx.getBean("dtgkService");
//根据条件搜索内容
List<Map<String, String>> gxsList = dtgkService.getGxsInfoByInput(value,firstNum,maxNum);
//填充到listbox中
for (Map<String, String> map : gxsList) {
Listitem item = new Listitem();
Listcell lc = new Listcell();
lc.setLabel(map.get("NAME"));
lc.setValue(map.get("CODE"));
lc.setParent(item);
gxsBox.appendChild(item);
}
//为listbox添加select事件,当选中数据后返填到bandbox中
gxsBox.addEventListener(Events.ON_SELECT, new EventListener() {
public void onEvent(Event e) throws Exception {
Listitem item = gxsBox.getSelectedItem();
List<Listcell> cellList = item.getChildren();
for (Listcell lc : cellList) {
combo.setValue(lc.getLabel()+","+lc.getValue());
}
combo.closeDropdown();
}});
}
}
step.3---service的方法就两句话,一块贴出来了
/**
* 获取信息,根据输入框的录入
* @param value
* @param firstNum
* @param maxNum
* @return
*/
public List<Map<String, String>> getGxsInfoByInput(String value, int firstNum, int maxNum) {
return this.userDao.queryGxsByInput(value, firstNum, maxNum);
}
/**
* 获取count
* @param value
* @return
*/
public int getGxsInfoCountByInput(String value) {
return this.userDao.queryGxsCountByInput(value);
}
step.4-----dao
public int queryGxsCountByInput(final String value) {
String sql = "SELECT DISTINCT COUNT(*) FROM " +
"TABLENAME WHERE CODE LIKE '%"+value+"%' OR NAME LIKE '%"+value+"%'";
return this.getJdbcTemplate().queryForInt(sql);
}
public List<Map<String, String>> queryGxsByInput(final String value, int firstNum, int maxNum) {
String sql = "SELECT * FROM (SELECT DISTINCT ROWNUM AS RN, T.CODE,T.NAME FROM " +
"TABLENAME T WHERE T.CODE LIKE ? OR T.NAME LIKE ?) WHERE RN>"+firstNum+" AND RN<="+maxNum;
return this.getJdbcTemplate().query(sql, new PreparedStatementSetter(){
public void setValues(PreparedStatement pst) throws SQLException {
pst.setString(1, "%"+value+"%");
pst.setString(2, "%"+value+"%");
}}, new GxsMapper());
}
protected class GxsMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Map<String, String> gxsMap = new HashMap<String, String>();
gxsMap.put("CODE", rs.getString("CODE"));
gxsMap.put("NAME", rs.getString("NAME"));
return gxsMap;
}
}
写的很烂,希望大家不要拍砖,有什么不对的地方请高手们多指正,谢谢!
还是那句话,希望对大家有所帮助.