`

自己封装的Extjs组件combox

 
阅读更多

http://fjza1168.blog.163.com/blog/static/3666003620110104647573/  

原文地址:自己封装的Extjs组件combox(2    原文作者:chy2z

效果图:以摸板形式绑定数据

自己封装的Extjs组件combox(2) - chy2z - 黑暗行动


 

效果图:及联查询

自己封装的Extjs组件combox(2) - chy2z - 黑暗行动

效果图:绑定本地数据,默认选种第一个

自己封装的Extjs组件combox(2) - chy2z - 黑暗行动


 

源代码:

Ext.namespace("Ext.tet");
Ext.tet.ComboBox=new Ext.extend(Ext.form.ComboBox,{
         DataSource:{tableName:null,cols:[],relationValue:null,query:null,search:null,where:null,orderBy:null,direction:null},
         ajaxUrl:"/ExtProject/pagination/comBoxPagination.ashx",
         typeAhead:true,        
         forceSelection: true,
         triggerAction: 'all',                                   //all 不起用 autocomplete功能,  query 起用autocomplete功能
         emptyText:'请选择信息',
         selectOnFocus:true,
         anchor:"97%",         
         blankText:'请选择一个',
         readOnly:true,
         minChars:1,
         lazyRender:true,
         selectFirstRow:false,                                   //默认不选种第一项
         lazyInit:true,
         initComboBox:function(){
              if(this.DataSource){   
                 if(Ext.type(this.DataSource)=="object"){        //服务器
                       this.mode="remote";
                       if(this.pageSize){                        //分页,回掉数据
                                 triggerAction="query";                            
                       }                      
                 } 
                 else if(Ext.type(this.DataSource)=="array"){    //本地数据
                       this.mode="local";
                 }
              }
              else Ext.Msg.alert("提示","DataSource为空");
         },
         createStore:function(){             
              if(this.DataSource){     //[valueField不能有重复值]                 
                       if(!this.displayField) this.displayField=this.valueField;
                       if(!this.valueField) this.valueField=this.displayField;
                       if(this.mode=="local"){
                            this.store=new Ext.data.SimpleStore({
              fields:[this.displayField,this.valueField],
              data:this.DataSource
             });
                       }
                       else{
                             this.store=new Ext.data.Store({
                                       proxy:new Ext.data.HttpProxy(
                                       {
                                           
url:this.ajaxUrl,
                                            method:"POST"
                                       }),
                                       reader:new Ext.data.JsonReader(
                                       {
                                                    fields:this.DataSource["cols"],
                                                    root:"data",
                                                    totalProperty:"totalCount"                      
                                       }),
                                       remoteSort:true,
                                       sortInfo: {field:this.DataSource["orderBy"]||this.displayField,direction:this.DataSource["direction"]||"Desc"},
                                       listeners:{
                                          "beforeload":function(ds,option){
                                                 if(ds.baseParams["relationValue"]==null){                             //级联查询
                                                        if(ds.baseParams["query"]!=null||ds.baseParams["query"]==""){
                                                                  ds.baseParams["where"]=ds.baseParams["search"]+" like '%" + ds.baseParams["query"] + "%'";
                                                                  ds.baseParams["query"]=null;
                                                        }
                                                 }
                                                 else{  
                                                        ds.baseParams["where"]=ds.baseParams["search"]+" like '%" + ds.baseParams["relationValue"] + "%'";
                                                 } 
                                           }
                                       },
                                       baseParams:{start:0,limit:this.pageSize||1000,search:this.DataSource["search"]||this.displayField,relationValue:this.DataSource["relationValue"]||null,tableName:this.DataSource["tableName"],key:this.valueField,columns:this.DataSource["cols"].join(","),where:this.DataSource["where"]}
                               });
                               this.store.load();
                      } 
             }                  
         },
         render:function(comb){
                Ext.tet.ComboBox.superclass.render.apply(this, arguments);
                if(this.selectFirstRow){
                       this.setValue(this.getStore().getAt(0).data[this.valueField]);
                       this.fireEvent("select",this,this.getStore().getAt(0),0); 
                }                      
         },
         constructor:function(options){                              
                Ext.apply(this,options);
                this.initComboBox();
                this.createStore();                                                           
                Ext.tet.ComboBox.superclass.constructor.call(this);
         }
});

Ext.reg('combobox',Ext.tet.ComboBox);


 

调用方法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页</title>
<link rel="stylesheet" type="text/css" href="../../Client/Ext/resources/css/ext-all.css" />
<script type="text/javascript" src="../../Client/Ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../Client/Ext/ext-all.js"></script>
<script type="text/javascript" src="../class/ComboBox.js"></script>
</head>
<script language=javascript>
Ext.BLANK_IMAGE_URL="../../Client/Images/s.gif";   
window.onload=function(){
                    new Ext.Panel({
                                title:"本地",
                                frame:true,
                                width:375,
                       height:140,
                       plain:true,
                       layout:"form",
                       defaultType:"textfield",
                       labelWidth:60,
                       defaults:{anchor:"95%",msgTarget:"side"},
                       buttonAlign:"center",
                       bodyStyle:"padding:0 0 0 0",
                                renderTo:"panel1",
                                items:[                              
                              {
                                            xtype:"combobox",
                                            fieldLabel:"性别",
                                            valueField:"value",
                                            DataSource:[["男"],["女"]],
                                            selectFirstRow:true
                              },
                              new Ext.tet.ComboBox({
                                            fieldLabel:"性别",
                                            valueField:"value",
                                            DataSource:[["男"],["女"]]
                              }),
                              new Ext.tet.ComboBox({
                                            id:"testcomb",
                                            readOnly:false,
                                            fieldLabel:"工资级别",
                                            displayField:"text",
                                            valueField:"value",
                                            DataSource:[["工程师","10000"],["项目经理","6000"],["程序员","4000"]], 
                                            selectFirstRow:true,                                          
                                            listeners:{
                                                        "select":function(combo,rec,index)
                                                        {
                                                                  alert(combo.getValue());
                                                        }
                                                     }
                              })
                                ]    
                    });
                   
                    //Ext.getCmp("testcomb").setValue(Ext.getCmp("testcomb").getStore().getAt(0).data.value);
                   
                    //alert(Ext.getCmp("testcomb").getValue());
                   
                    //alert(Ext.getCmp("testcomb").valueField);
                   
                    new Ext.Panel({
                                title:"服务器",
                                frame:true,
                                width:375,
                       height:200,
                       plain:true,
                       layout:"form",
                       defaultType:"textfield",
                       labelWidth:60,
                       defaults:{anchor:"95%",msgTarget:"side"},
                       buttonAlign:"center",
                       bodyStyle:"padding:0 0 0 0",
                                renderTo:"panel2",
                                items:[

                              new Ext.tet.ComboBox({ 
                                            tpl: '<tpl for="."><div ext:qtip="{BorrowNo}" class="x-combo-list-item">姓名:{LoginName},编号:{BorrowNo}</div></tpl>',
                                            readOnly:false,                                          
                                            fieldLabel:"图书编号",
                                            displayField:"BorrowNo",  
                                            DataSource:{tableName:"Borrow_View",cols:["BorrowNo","LoginName"]},
                                            listeners:{
                                                        "select":function(combo,rec,index)
                                                        {
                                                                  alert(combo.getValue());
                                                        }
                                                     }
                              }),
                              new Ext.tet.ComboBox({ 
                                            tpl: '<tpl for="."><div ext:qtip="{Name}" class="x-combo-list-item">姓名:{Name},登陆名:{LoginName}<br/>身份证:{IdentityCard}</div></tpl>',
                                            readOnly:false,                                          
                                            fieldLabel:"用户姓名",
                                            displayField:"Name", 
                                            valueField:"LoginName", 
                                            pageSize:10, 
                                            resizable:true,
                                            DataSource:{tableName:"Users",cols:["Name","LoginName"]},
                                            listeners:{
                                                        "select":function(combo,rec,index)
                                                        {
                                                                  alert(combo.getValue());
                                                        }
                                                     }
                             })     
                                ]    
                    });
                   
                                              
                    new Ext.Panel({
                                title:"及联查询",
                                frame:true,
                                width:375,
                       height:200,
                       plain:true,
                       layout:"form",
                       defaultType:"textfield",
                       labelWidth:70,
                       defaults:{anchor:"95%",msgTarget:"side"},
                       buttonAlign:"center",
                       bodyStyle:"padding:0 0 0 0",
                                renderTo:"panel3",
                                items:[
                                        {
                                          xtype:"combobox",
                                          fieldLabel:"身份证(1)",
                                          valueField:"value",
                                          DataSource:[["0238"],["0236"],["0237"]],
                                          selectFirstRow:true,
                                          listeners:{
                                                "select":function(combo,rec,index)
                                                {                                                       
                                                      Ext.getCmp("user").reset();
                                                      Ext.getCmp("user").lastQuery=combo.getValue();                       //清除上一次查询 
                                                      Ext.getCmp("user").getStore().baseParams["search"]="IdentityCard";   //查询字段
                                                      Ext.getCmp("user").getStore().baseParams["relationValue"]=combo.getValue();//查询值
                                                }
                                             }
                              },
                              new Ext.tet.ComboBox({ 
                                          id:"user",
                                          tpl: '<tpl for="."><div ext:qtip="{Name}" class="x-combo-list-item">姓名:{Name},登陆名:{LoginName}<br/>身份证:{IdentityCard}</div></tpl>',
                                          readOnly:true,                                          
                                          fieldLabel:"用户姓名(2)",
                                          displayField:"Name", 
                                          valueField:"LoginName", 
                                          pageSize:10, 
                                          resizable:true,
                                          DataSource:{tableName:"Users",cols:["Name","LoginName","IdentityCard"]},                                         
                                          listeners:{
                                                "select":function(combo,rec,index)
                                                {
                                                          alert(combo.getValue());
                                                }
                                             }
                              })
                                ]
                          });
}
</script>
<body>
<div>
<div id="panel1"></div>
<div id="panel2"></div>
<div id="panel3"></div>
</div>
</body>
</html>


 

comBoxPagination.ashx 代码:

<%@ WebHandler Language="C#" Class="comBoxPagination" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class comBoxPagination : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        string tableName = context.Request.Form["tableName"];
        string key = context.Request.Form["key"];
        string starts = context.Request.Form["start"];
        string limits = context.Request.Form["limit"];
        string sort = context.Request.Form["sort"];
        string dir = context.Request.Form["dir"];
        string where = context.Request.Form["where"];
        string columns = context.Request.Form["columns"];
        string JSON = "";
        if (starts != null && limits != null)
        {
            int start = int.Parse(starts);
            int limit = int.Parse(limits);
            if (where != null)
                if (where == "null" || where.Trim() == "") where = null;
            JSON = GetPaginationJson(tableName, key, start, limit, sort, dir, where, columns);
        }
        else
        {
            context.Response.Write("{success:'false'}");
        }

        context.Response.Write(JSON);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public string GetPaginationJson(string tableName, string key, int start, int limit, string sort, string dir, string where, string columns)
    {
        JSONHelper json = new JSONHelper();
        json.success = true;
        DataSet ds = GetInfo(tableName, key, start, limit, sort, dir, where, columns);
        string[] fields = columns.Split(',');
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            foreach (string col in fields)
            {
                json.AddItem(col, dr[col].ToString());
            }
            json.ItemOk();
        }
        json.totlalCount = GetInfoCount(tableName, where);
        return json.ToString();
    }

    /// <summary>
    /// 得到总记录数
    /// </summary>
    /// <returns></returns>
    public int GetInfoCount(string tableName, string where)
    {
        string sql = "";
        if (where == null)
            sql = "select count(*) from " + tableName;

        else
            sql = "select count(*) from " + tableName + " where " + where;
        return int.Parse(CHY.DbHelperSQL.ExecuteScalar(sql).ToString());
    }

    public DataSet GetInfo(string tableName, string key, int start, int limit, string sort, string dir, string where, string columns)
    {
        string sql;
        if (sort == null)
            sort = key;
        if (dir == null)
            dir = "DESC";
        if (columns == null)
            columns = "*";
        if (where == null)
            sql = "select distinct top " + limit + " " + columns + " from " + tableName + " where " + key + " not in(select top " + start + " " + key + " from " + tableName + " order by " + sort + " " + dir + ") order by " + sort + " " + dir;
        else
            sql = "select distinct top " + limit + " " + columns + " from " + tableName + " where " + where + " and " + key + " not in(select top " + start + " " + key + " from " + tableName + " where " + where + " order by " + sort + " " + dir + ") order by " + sort + " " + dir;

        return CHY.DbHelperSQL.Query(sql);
    }

}

分享到:
评论

相关推荐

    Extjs之旅-combox之远程加载数据

    本篇我们将深入探讨“Extjs之旅”中的一个关键组件——Combox(组合框),特别是其远程加载数据的特性。 Combox在ExtJS中是一个非常灵活的控件,它可以看作是下拉列表和文本输入框的结合体,用户可以输入文本搜索,...

    Extjs4 Combox tree

    ExtJS4中的ComboxTree是一种将下拉列表与树形结构结合的组件,它扩展了标准的ComboBox,提供了更丰富的用户交互体验。这种组件在数据展示和选择时特别有用,尤其是在处理具有层级关系的数据时,比如部门结构、地区...

    ExtJS 组件扩展

    为了更好地适应特定业务需求,ExtJS支持自定义组件的扩展与封装。本文将重点探讨ExtJS中组件扩展的两种主要层次及其实践要点。 #### 二、常见错误及原因分析 在使用ExtJS进行组件扩展时,开发人员常常会遇到一些...

    EXTjs组件.pdf

    EXTjs组件是构建EXTJS应用程序的核心元素,它们是基于Ext.Component的子类,具备自动化的生命周期管理,包括创建、渲染、尺寸调整、定位和销毁等关键功能。组件是EXTJS中可交互的部件,可以是按钮、表格、面板等,...

    Extjs 下拉菜单实现拼音输入进行检索

    Extjs 下拉菜单实现拼音输入进行检索

    EXTjs组件解释文档

    包括从基础到深入的EXTjs组件解释,EXTjs实例,EXT核心API详解,Ext.DomQuery类

    extjs 3.1 组件 使用

    在本篇文章中,我们将深入探讨如何在实际项目中使用ExtJS 3.1的组件。 首先,`css`目录包含了ExtJS 3.1的样式文件。这些CSS文件用于定义组件的外观和布局,包括颜色、字体、边距、边框等。在使用过程中,我们需要...

    ExtJS日期多选组件源码

    通过研究和理解"ExtJS日期多选组件源码",开发者可以深入学习ExtJS组件设计、事件处理、数据绑定等核心概念,并能进一步定制适合自己项目需求的日期选择组件。这样的组件对于提高开发效率和用户体验具有积极的意义。

    extjs的一套封装管理界面

    下载的一套extjs的一套封装管理界面,修改了几个bug,倒是挺好用的。

    extjs入门之组件学习

    extjs入门学习,各个组件的使用,包括Observable、Observable、BoxComponent、Container、Panel、Viewport及Window...

    Ext编辑组件,方便编写extjs

    Ext编辑组件是基于ExtJS框架的一个扩展库,它为开发者提供了更加便捷的方式来创建和管理界面中的可编辑元素。ExtJS是一个强大的JavaScript库,用于构建富客户端的Web应用程序,尤其适用于构建数据驱动、交互性强的...

    Extjs树分页组件扩展

    在ExtJS中,树形(Tree)组件是一种用于展示层级数据的强大工具,它允许用户以树状结构浏览和操作数据。然而,在处理大量数据时,一次性加载所有节点可能导致页面响应变慢,这时就需要引入分页功能。"Extjs树分页...

    ExtJs各组件简单应用例子导入到eclipse即可

    1. **下载ExtJS组件示例**:首先,你需要从官方或者其他可靠的源获取ExtJS的组件示例代码。这些示例通常包括HTML文件、JavaScript文件和可能的CSS文件,它们展示了如何在实际项目中使用各种组件。 2. **创建Eclipse...

    Extjs3.0 常用组件介绍及怎么安装开发利器Spket

    **ExtJS 3.0 常用组件介绍** ExtJS 是一款强大的JavaScript库,用于构建富客户端Web应用。在3.0版本中,它提供了一系列丰富的组件,这些组件可以帮助开发者构建功能丰富的用户界面。以下是一些ExtJS 3.0中的常用...

    extjs GuiDesigner extjs组件生成器

    非常不错的ext ui的工具,可以通过拖曳的方式来生成ext的组件。代码可以用于学习

    Extjs 年月日时分秒组件

    通过查看这些文件,你可以学习如何在自己的项目中集成并定制这个组件。一般来说,开发者会参考提供的示例代码,理解其工作原理,然后根据项目需求进行调整。 总之,"Extjs 年月日时分秒组件"是ExtJS框架中一个实用...

    extjs组件的dll文件

    丰富的用户级组件,功能使用很强大,丰富的用户级组件,功能使用很强大,丰富的用户级组件,功能使用很强大,丰富的用户级组件,功能使用很强大,丰富的用户级组件,功能使用很强大,丰富的用户级组件,功能使用很强大,...

    ExtJS开发插件及Ext包

    - 插件是ExtJS中扩展组件功能的一种方式,允许开发者添加自定义行为或功能到现有的组件上。 - 开发插件通常涉及创建一个新的JavaScript文件,定义一个类并扩展自`Ext.util.Plugin`或`Ext.grid.Panel`等特定组件的...

    用XSL将ExtJS封装成标签

    标题“用XSL将ExtJS封装成标签”指的是利用XSL(XML Stylesheet Language)技术,将ExtJS库中的组件和功能转化为自定义HTML标签,以便于在网页开发中更方便地使用和管理ExtJS代码。这个过程可以提高代码的可读性、可...

Global site tag (gtag.js) - Google Analytics