zoukankan      html  css  js  c++  java
  • 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', {
                                     
    400,
                                    multiple: 
    false,
                                    matchContains: 
    false,
                                    formatItem: formatItem,
                                    selectFirst:
    true,
                                    extraParams:({criteria:
    "name",parenteneity:entityId})
                            });
                            
                            $(idTextField).autocomplete(
    '/myServer/servlets/LocationAjaxServlet', {
                                     
    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默认的参数,代表你输入的数值。 

  • 相关阅读:
    Android 传感器应用
    WebStrom9 体验nodejs
    Web前端框架 小记
    接入淘宝API(PHP版本)
    Android SDK 国内镜像
    Ubuntu14.04 搭建 node.js 环境(Binaries方式)
    C# 异常类型及对应异常类
    .net中序列化读写xml方法的总结
    ASp.NET Core Centos7运行环境搭建
    Linux Centos 常用命令
  • 原文地址:https://www.cnblogs.com/duwamish/p/1513782.html
Copyright © 2011-2022 走看看