auto compalate


        
                    $(document).ready(function() {
                        var nameTextField 
= document.getElementById("form1:textFieldName_field");
                        var idTextField 
= document.getElementById("form1:textFieldId_field");function formatItem(row) {
                                
return row[0+ " (<strong>Type ID: " + row[1+ "</strong>)";
                        }
                        
//设定最大显示数量
                        function changeOptions(){
                                var max 
= 50;
                                
if (max &gt; 0) {
                                        $(nameTextField).setOptions({
                                                max: max
                                        });
                                        $(idTextField).setOptions({
                                                max: max
                                        });
                                }
                        }
                        changeOptions();

                        $(nameTextField).autocomplete(
'/myServer/servlets/LocationAjaxServlet', {
                                width: 
400,
                                multiple: 
false,
                                matchContains: 
false,
                                formatItem: formatItem,
                                selectFirst:
true,
                                extraParams:({criteria:
"name",parenteneity:entityId})
                        });
                        
                        $(idTextField).autocomplete(
'/myServer/servlets/LocationAjaxServlet', {
                                width: 
400,
                                multiple: 
false,
                                matchContains: 
false,
                                formatItem: formatItem,
                                selectFirst:
true,
                                extraParams:({criteria:
"busunitid",parenteneity:entityId})
                        });

                        $(nameTextField).result(function(
event, data, formatted) {
                            $(idTextField).val(data[
1]);
                        });

                        $(idTextField).result(function(
event, data, formatted) {
                            $(nameTextField).val(data[
0]);
                            $(idTextField).val(data[
1]);
                        });




这是获取数据的Servlet 
Java code

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        
        String q 
= request.getParameter("q");
        String row 
= request.getParameter("row");
        String criteria 
= request.getParameter("criteria");
        String entityId 
= request.getParameter("parenteneity");
        
        OutputStream 
out = response.getOutputStream();
    
        
        
        StringBuffer sb 
= new StringBuffer();
        
        DataSourceBean dataSource 
= new DataSourceBean();
        
try {
            dataSource.connect();
            String query 
= "SELECT * FROM busunit WHERE longname LIKE '%" + q + "%' AND parent_entity = " + entityId;
            
            
if(criteria.equals("busunitid")) {
                query 
= "SELECT * FROM busunit WHERE busunit_id LIKE '%" + q + "%' AND parent_entity = " + entityId;
            }
            
            System.
out.println(query);
            
            ResultSet rs 
= dataSource.query(query);
            
while (rs.next()) {
                
                sb.append(
"\n");
                String longname 
= rs.getString("longname");
                
long id = rs.getLong("busunit_id");
                
                sb.append(longname);
                sb.append(
"|");
                sb.append(Long.toString(id));
                sb.append(
"|");
                sb.append(row);
                
            }
                
        }
        
catch (Exception e) {
            System.
out.println("Error while check batch coding ajax query");
        }
        
finally {
            
try {
                dataSource.close();
            } 
catch (SQLException ignored) {}
        }
        
        
out.write(sb.toString().getBytes());
        
        
out.close();
    }



在这段代码中,参数名criteria是搜索的规则,搜索name还是搜索id。parententity是表中的一列,因为我有两个表,一个busunit,一个entity,parent_entity是busunit中的fk,指相entity.pkid。比较特殊的是参数q,这是jquery默认的参数,代表你输入的数值。 

posted @ 2009-06-30 10:59  paymob  阅读(229)  评论(0编辑  收藏  举报