Ext.form.ComboBox 远程模式的基本定义

本实例主要实现输入一个关键字,及时显示查询结果列表,类似于GOOGLE搜索框,先看下效果。

本实例包含两段代码,一个html页面展示前台效果,一个servlet后台处理程序

注意:相关路径和名字还请各位自己变通下。

-----------------------------------index.html-------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>index.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
   <!-- EXT JS LIBRARY -->
<link rel="stylesheet" type="text/css" href="/knowledge/EXT/resources/css/ext-all.css" />

<script src="/knowledge/EXT/adapter/ext/ext-base.js"></script>
<script src="/knowledge/EXT/ext-all.js"></script>

</head>

<body>
   <script type="text/javascript">
   Ext.onReady(                    
                                
       function()
       {
          // //author.json页面:    {author : [{text : "t1" , value : "v1" } , {text : "t2" , value : "v2" }]}
         
           var remoteComboBox= new Ext.form.ComboBox({
                                         fieldLabel : '部门', //UI标签名称
                                         name : 'keyname',   //作为form提交时传送的参数名
                                         allowBlank : false, //是否允许为空
                                         mode : "remote",      //数据模式为远程模式, 也可不设置,即默认值也为remote
                                         readOnly : false,     //是否只读
                                         triggerAction : 'all', //显示所有下列数.必须指定为'all'
                                         anchor : '90%',
                                         emptyText:'请选择...',   //没有默认值时,显示的字符串
                                         store : new Ext.data.JsonStore({ //填充的数据
                                                           url : "/knowledge/NounComboBoxServlet",
                                                           fields : new Ext.data.Record.create( ['text', 'value']),   //也可直接为["text","value"]
                                                           root : 'datalist'
                                         }),
                                         value:'v2', //设置当前选中的值, 也可用作初始化时的默认值, 默认为空
                                         valueField : 'value', //传送的值
                                         displayField : 'text' //UI列表显示的文本
                                 }) ;
                                
                            
       var w = new Ext.Window({
    title : 'ComboBox',
    closable : true,
    width : 800,
    items : [remoteComboBox]
   });
   w.show(this)    
  
     }     
   );
  
</script>

</body>
</html>
-----------------------------------------------------servlet.java--------------------------------------------------------

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {  

   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
  
   out.print("{datalist:[{text:'测试一,value:'838'},{text:'测试二',value:'33'},]}");
  
   out.flush();
   out.close();
}

猜你喜欢

转载自blog.csdn.net/ssdate/article/details/5686472