我要做自动补全的搜索提示,向从数据库中提取关键字
情况:
1,多个表(因为是不同的文章类型),表的结构中都有,title和keyboard字段
例如  title      keyboard
     2008奥运会  2008,奥运会现在的问题是
1,我要怎么取这个关键词的列表,是用title字段取,还是keyboard,可是board又不准
2,因为有多个表,还会涉及多表查询是不是说要单独建立一个关键词表,这样更好高人提意见,如何取表

解决方案 »

  1.   

    笼统得回答你的问题吧。jquery有一个auto-complete的模块,使用起来非常方便。数据源可以是任何服务器端程序,ajax的数据读取模式,速度很快,而且设计得非常简捷漂亮。假设你有两个textfield,在任意一个里面输入关键字就自动把两个都填充。
                        
                        $(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]);
                            });这是获取数据的Servletprotected 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默认的参数,代表你输入的数值。从上面这个例子你可以看出,你可以使用任何东西作为关键字,并不需要另建关键字表。在客户端,jquery autocomplete只是发送一个Ajax请求到服务器,主要的工作都是服务器在做。你想用什么样的关键字和你的SQL有关。在这个例子中,在你输入字符的过程中,textfield下方会弹出提示,只要你把能区分相同结果的东西,像pkid加到输出的字符串里,用户就可以自己选定搜索结果了,这样就不要求你的搜索结果唯一。