• 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默认的参数,代表你输入的数值。 

  • 相关阅读:
    对于对象的要求:高内聚、低耦合,这样容易拼装成为一个系统
    为什么要使用面向对象
    什么是对象:EVERYTHING IS OBJECT(万物皆对象)
    文件 I/O 问题
    如果可能的话,使用 PC-Lint、LogiScope 等工具进行代码审查
    把编译器的选择项设置为最严格状态
    尽量不要使用与具体硬件或软件环境关系密切的变量
    尽量使用标准库函数
    如果原有的代码质量比较好,尽量复用它
    不要设计面面俱到、非常灵活的数据结构
  • 原文地址:https://www.cnblogs.com/duwamish/p/1513782.html
Copyright © 2020-2023  润新知