`
andrew.yulong
  • 浏览: 169773 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

extJs控件之客户端查询

阅读更多

  和大家分享一个我在www.extjs.com上找的插件,该插件可以执行客户端查询,说白了就是只能在本页做查询动作,没有和服务器交互.代码如下

// vim: ts=4:sw=4:nu:fdc=4:nospell

/**

 * Search plugin for Ext.grid.GridPanel, Ext.grid.EditorGrid ver. 2.x or subclasses of them

 *

 * @author    Ing. Jozef Sakalos

 * @copyright (c) 2008, by Ing. Jozef Sakalos

 * @date      17. January 2008

 * @version   $Id: Ext.ux.grid.Search.js 120 2008-03-31 00:09:05Z jozo $

 *

 * @license Ext.ux.grid.Search is licensed under the terms of

 * the Open Source LGPL 3.0 license.  Commercial use is permitted to the extent

 * that the code/component(s) do NOT become part of another Open Source or Commercially

 * licensed development library or toolkit without explicit permission.

 * 

 * License details: http://www.gnu.org/licenses/lgpl.html

 */



/*global Ext */



Ext.ns('Ext.ux.grid');



/**

 * @class Ext.ux.grid.Search

 * @extends Ext.util.Observable

 * @param {Object} config configuration object

 * @constructor

 */

Ext.ux.grid.Search = function(config) {

    Ext.apply(this, config);

    Ext.ux.grid.Search.superclass.constructor.call(this);

}; // eo constructor



Ext.extend(Ext.ux.grid.Search, Ext.util.Observable, {

    /**

     * @cfg {String} searchText Text to display on menu button

     */

     searchText:'Search'



    /**

     * @cfg {String} searchTipText Text to display as input tooltip. Set to '' for no tooltip

     */ 

    ,searchTipText:'Type a text to search and press Enter'



    /**

     * @cfg {String} selectAllText Text to display on menu item that selects all fields

     */

    ,selectAllText:'Select All'



    /**

     * @cfg {String} position Where to display the search controls. Valid values are top and bottom (defaults to bottom)

     * Corresponding toolbar has to exist at least with mimimum configuration tbar:[] for position:top or bbar:[]

     * for position bottom. Plugin does NOT create any toolbar.

     */

    ,position:'bottom'



    /**

     * @cfg {String} iconCls Icon class for menu button (defaults to icon-magnifier)

     */

    ,iconCls:'icon-magnifier'



    /**

     * @cfg {String/Array} checkIndexes Which indexes to check by default. Can be either 'all' for all indexes

     * or array of dataIndex names, e.g. ['persFirstName', 'persLastName']

     */

    ,checkIndexes:'all'



    /**

     * @cfg {Array} disableIndexes Array of index names to disable (not show in the menu), e.g. ['persTitle', 'persTitle2']

     */

    ,disableIndexes:[]



    /**

     * @cfg {String} dateFormat how to format date values. If undefined (the default) 

     * date is formatted as configured in colummn model

     */

    ,dateFormat:undefined



    /**

     * @cfg {Boolean} showSelectAll Select All item is shown in menu if true (defaults to true)

     */

    ,showSelectAll:true



    /**

     * @cfg {String} mode Use 'remote' for remote stores or 'local' for local stores. If mode is local

     * no data requests are sent to server the grid's store is filtered instead (defaults to 'remote')

     */

    ,mode:'remote'



    /**

     * @cfg {Number} width Width of input field in pixels (defaults to 100)

     */

    ,width:100



    /**

     * @cfg {String} xtype xtype is usually not used to instantiate this plugin but you have a chance to identify it

     */

    ,xtype:'gridsearch'



    /**

     * @cfg {Object} paramNames Params name map (defaults to {fields:'fields', query:'query'}

     */

    ,paramNames: {

         fields:'fields'

        ,query:'query'

    }



    /**

     * @cfg {String} shortcutKey Key to fucus the input field (defaults to r = Sea_r_ch). Empty string disables shortcut

     */

    ,shortcutKey:'r'



    /**

     * @cfg {String} shortcutModifier Modifier for shortcutKey. Valid values: alt, ctrl, shift (defaults to alt)

     */

    ,shortcutModifier:'alt'



    /**

     * @cfg {String} align 'left' or 'right' (defaults to 'left')

     */



    /**

     * @cfg {Number} minLength force user to type this many character before he can make a search

     */



    /**

     * @cfg {Ext.Panel/String} toolbarContainer Panel (or id of the panel) which contains toolbar we want to render

     * search controls to (defaults to this.grid, the grid this plugin is plugged-in into)

     */

    

    // {{{

    /**

     * private

     * @param {Ext.grid.GridPanel/Ext.grid.EditorGrid} grid reference to grid this plugin is used for

     */

    ,init:function(grid) {

        this.grid = grid;



        // setup toolbar container if id was given

        if('string' === typeof this.toolbarContainer) {

            this.toolbarContainer = Ext.getCmp(this.toolbarContainer);

        }



        // do our processing after grid render and reconfigure

        grid.onRender = grid.onRender.createSequence(this.onRender, this);

        grid.reconfigure = grid.reconfigure.createSequence(this.reconfigure, this);

    } // eo function init

    // }}}

    // {{{

    /**

     * private add plugin controls to <b>existing</b> toolbar and calls reconfigure

     */

    ,onRender:function() {

        var panel = this.toolbarContainer || this.grid;

        var tb = 'bottom' === this.position ? panel.bottomToolbar : panel.topToolbar;



        // add menu

        this.menu = new Ext.menu.Menu();



        // handle position

        if('right' === this.align) {

            tb.addFill();

        }

        else {

            tb.addSeparator();

        }



        // add menu button

        tb.add({

             text:this.searchText

            ,menu:this.menu

            ,iconCls:this.iconCls

        });



        // add input field (TwinTriggerField in fact)

        this.field = new Ext.form.TwinTriggerField({

             width:this.width

            ,selectOnFocus:undefined === this.selectOnFocus ? true : this.selectOnFocus

            ,trigger1Class:'x-form-clear-trigger'

            ,trigger2Class:'x-form-search-trigger'

            ,onTrigger1Click:this.onTriggerClear.createDelegate(this)

            ,onTrigger2Click:this.onTriggerSearch.createDelegate(this)

            ,minLength:this.minLength

        });



        // install event handlers on input field

        this.field.on('render', function() {

            this.field.el.dom.qtip = this.searchTipText;



            // install key map

            var map = new Ext.KeyMap(this.field.el, [{

                 key:Ext.EventObject.ENTER

                ,scope:this

                ,fn:this.onTriggerSearch

            },{

                 key:Ext.EventObject.ESC

                ,scope:this

                ,fn:this.onTriggerClear

            }]);

            map.stopEvent = true;

        }, this, {single:true});



        tb.add(this.field);



        // reconfigure

        this.reconfigure();



        // keyMap

        if(this.shortcutKey && this.shortcutModifier) {

            var shortcutEl = this.grid.getEl();

            var shortcutCfg = [{

                 key:this.shortcutKey

                ,scope:this

                ,stopEvent:true

                ,fn:function() {

                    this.field.focus();

                }

            }];

            shortcutCfg[0][this.shortcutModifier] = true;

            this.keymap = new Ext.KeyMap(shortcutEl, shortcutCfg);

        }

    } // eo function onRender

    // }}}

    // {{{

    /**

     * private Clear Trigger click handler

     */

    ,onTriggerClear:function() {

        this.field.setValue('');

        this.field.focus();

        this.onTriggerSearch();

    } // eo function onTriggerClear

    // }}}

    // {{{

    /**

     * private Search Trigger click handler (executes the search, local or remote)

     */

    ,onTriggerSearch:function() {

        if(!this.field.isValid()) {

            return;

        }

        var val = this.field.getValue();

        var store = this.grid.store;



        // grid's store filter

        if('local' === this.mode) {

            store.clearFilter();

            if(val) {

                store.filterBy(function(r) {

                    var retval = false;

                    this.menu.items.each(function(item) {

                        if(!item.checked || retval) {

                            return;

                        }

                        var rv = r.get(item.dataIndex);

                        rv = rv instanceof Date ? rv.format(this.dateFormat || r.fields.get(item.dataIndex).dateFormat) : rv;

                        var re = new RegExp(val, 'gi');

                        retval = re.test(rv);

                    }, this);

                    if(retval) {

                        return true;

                    }

                    return retval;

                }, this);

            }

            else {

            }

        }

        // ask server to filter records

        else {

            // clear start (necessary if we have paging)

            if(store.lastOptions && store.lastOptions.params) {

                store.lastOptions.params[store.paramNames.start] = 0;

            }



            // get fields to search array

            var fields = [];

            this.menu.items.each(function(item) {

                if(item.checked) {

                    fields.push(item.dataIndex);

                }

            });



            // add fields and query to baseParams of store

            delete(store.baseParams[this.paramNames.fields]);

            delete(store.baseParams[this.paramNames.query]);

            if (store.lastOptions && store.lastOptions.params) {

                delete(store.lastOptions.params[this.paramNames.fields]);

                delete(store.lastOptions.params[this.paramNames.query]);

            }

            if(fields.length) {

                store.baseParams[this.paramNames.fields] = Ext.encode(fields);

                store.baseParams[this.paramNames.query] = val;

            }



            // reload store

            store.reload();

        }



    } // eo function onTriggerSearch

    // }}}

    // {{{

    /**

     * @param {Boolean} true to disable search (TwinTriggerField), false to enable

     */

    ,setDisabled:function() {

        this.field.setDisabled.apply(this.field, arguments);

    } // eo function setDisabled

    // }}}

    // {{{

    /**

     * Enable search (TwinTriggerField)

     */

    ,enable:function() {

        this.setDisabled(false);

    } // eo function enable

    // }}}

    // {{{

    /**

     * Enable search (TwinTriggerField)

     */

    ,disable:function() {

        this.setDisabled(true);

    } // eo function disable

    // }}}

    // {{{

    /**

     * private (re)configures the plugin, creates menu items from column model

     */

    ,reconfigure:function() {



        // {{{

        // remove old items

        var menu = this.menu;

        menu.removeAll();



        // add Select All item plus separator

        if(this.showSelectAll) {

            menu.add(new Ext.menu.CheckItem({

                 text:this.selectAllText

                ,checked:!(this.checkIndexes instanceof Array)

                ,hideOnClick:false

                ,handler:function(item) {

                    var checked = ! item.checked;

                    item.parentMenu.items.each(function(i) {

                        if(item !== i && i.setChecked) {

                            i.setChecked(checked);

                        }

                    });

                }

            }),'-');

        }



        // }}}

        // {{{

        // add new items

        var cm = this.grid.colModel;

        Ext.each(cm.config, function(config) {

            var disable = false;

            if(config.header && config.dataIndex) {

                Ext.each(this.disableIndexes, function(item) {

                    disable = disable ? disable : item === config.dataIndex;

                });

                if(!disable) {

                    menu.add(new Ext.menu.CheckItem({

                         text:config.header

                        ,hideOnClick:false

                        ,checked:'all' === this.checkIndexes

                        ,dataIndex:config.dataIndex

                    }));

                }

            }

        }, this);

        // }}}

        // {{{

        // check items

        if(this.checkIndexes instanceof Array) {

            Ext.each(this.checkIndexes, function(di) {

                var item = menu.items.find(function(itm) {

                    return itm.dataIndex === di;

                });

                if(item) {

                    item.setChecked(true, true);

                }

            }, this);

        }

        // }}}



    } // eo function reconfigure

    // }}}



}); // eo extend



// eof
例子:
var grid = new Ext.grid.GridPanel({

        store: store,

        cm: cm,

		sm: sm,

    	collapsible : true,// 是否可以展开

        animCollapse : false,// 展开时是否有动画效果

        renderTo: 'editor-grid',

        width: 800,

        height: 600,

               bbar: new Ext.PagingToolbar({

			pageSize: 15,

            store: store,

            displayInfo: true,

            displayMsg: "显示第 {0} 条到 {1} 条记录,一共 {2} 条",

            emptyMsg: "没有记录"

        }),

            plugins:[new Ext.ux.grid.Search({

                mode:'local'

               ,iconCls:false

               ,dateFormat:'m/d/Y'

            ,  minLength:2

//            ,align:'right'

        })]

    });
分享到:
评论

相关推荐

    extjs网页控件开发

    ExtJS是一种基于JavaScript的富客户端应用框架,专用于构建交互式和数据驱动的Web应用程序。在"extjs网页控件开发"中,我们主要关注的是使用ExtJS来创建高效的网页组件,如图表和多级联动下拉列表框。这些控件能够...

    ExtJs实例,富客户端技术经典

    5. **表单处理**:ExtJS的表单组件功能强大,支持各种输入控件、验证和数据提交。表单可以与数据存储结合,实现动态的数据绑定和验证。 6. **图表和图形**:通过ExtJS的Charts模块,开发者可以创建各种数据可视化...

    Extjs UI控件 for Asp.net3.5

    在ASP.NET 3.5框架下,ExtJS控件能帮助开发出具有桌面应用般体验的网页应用。 在标题"Extjs UI控件 for Asp.net3.5"中,我们关注的是ExtJS这个UI库如何应用于ASP.NET 3.5的项目中。ExtJS提供了多种可复用的组件,如...

    ExtJs:收集基于ExtJs扩展的一些控件

    这个文件可能是对一些自定义或第三方开发的ExtJs控件的概述,它可能包括了各种特定用途的组件,如日历控件、拖放功能、树形视图、图表插件等。这些控件通常是为了满足特定项目需求或者优化原有组件性能而创建的。...

    extjs 微调控件,时间微调控件,微调,javascript 微调

    ExtJS 是一个强大的JavaScript 库,专用于构建富客户端应用程序。在ExtJS中,微调控件(Spinner)是一种用户界面组件,它允许用户通过点击向上或向下的箭头以微小增量增加或减少数值,常见于输入框旁边,提供一种...

    extjs日期+时间控件

    5. **事件处理**:ExtJS控件通常支持多种事件,如`select`事件(当用户选择一个日期或时间时触发),开发者可以绑定回调函数来响应这些事件,进行进一步的数据处理或验证。 6. **本地化**:对于国际化的应用,日期...

    EXTJS时间控件年月日时分秒

    EXTJS是一款基于JavaScript的富客户端应用框架,它提供了一系列丰富的组件,用于构建功能强大的Web应用程序。在EXTJS中,时间控件是常见的交互元素,允许用户选择特定的时间,包括年、月、日、时、分和秒。这篇文档...

    ExtJS时间控件、IP输入控件【控件】

    ExtJS是一种广泛使用的JavaScript库,专门用于构建富客户端的Web应用程序。它提供了丰富的用户界面组件,包括各种各样的控件,如时间控件和IP输入控件,这些在Web开发中非常实用。在这个名为"ExtJS时间控件、IP输入...

    extjs C#控件全示例

    "很经典的"意味着这些示例在当时可能是非常流行和实用的,对于学习和掌握ExtJS控件的用法有很高的参考价值。作者提到"希望对需要的朋友有帮助",显示出分享精神,尽管他可能积分不足,无法提供更多的支持。 【标签...

    ExtJs通用筛选查询控件

    在ExtJs中,"通用筛选查询控件"是一个常见且实用的功能,它允许用户在数据网格中进行多维度的筛选和查询,以快速定位到所需的数据。下面我们将详细探讨这个主题。 1. **筛选查询控件的基本概念** 筛选查询控件是...

    ExtJs搜索控件、插件

    ExtJS是一个强大的JavaScript框架,主要用于构建富客户端应用。在ExtJS中,搜索控件和插件是提升用户体验、实现高效数据查找的关键元素。本篇将深入探讨这些知识点,以帮助你更好地理解和应用。 首先,我们要了解`...

    extjs扩展年度控件,EXTJS里的时间控件的年度重写

    EXTJS是一种广泛应用于Web开发的JavaScript库,特别适合构建富客户端应用。在EXTJS中,时间控件(Date Picker)是常见的元素,用于选择日期或时间。然而,有时开发人员可能需要对这些控件进行自定义,以满足特定业务...

    extjs3.2、3.3 时间控件 日期控件扩展

    ExtJS是一款基于JavaScript的富客户端应用开发框架,它提供了丰富的组件库,用于构建复杂的Web应用程序。在3.2和3.3版本中,ExtJS的时间控件(DateTimeField)和日期控件(DateField)是开发者常用的功能组件,用于...

    extjs 时间控件

    ExtJS 是一个强大的JavaScript库,专门用于构建富客户端应用程序。在ExtJS中,时间控件是一种用户界面元素,允许用户选择或输入特定的时间值。它提供了丰富的交互性和自定义选项,使得时间选择过程更加直观和高效。...

    extjs 多文件上传控件

    ExtJS 是一个强大的JavaScript 框架,专用于构建富客户端Web应用程序。它提供了一整套组件化的用户界面元素和丰富的交互功能。在ExtJS中,多文件上传控件是开发人员常用的一种功能,用于在网页上实现批量上传多个...

    ExtJs DataGridView控件实例

    ExtJs 是一个强大的JavaScript库,专门用于构建富客户端的Web应用程序。它提供了丰富的组件库,包括数据网格(DataGridView)控件,这个控件在Web应用中广泛用于展示和操作大量结构化数据。在"ExtJs DataGridView...

    extjs3.0 日期时间控件

    ExtJS是一个强大的JavaScript框架,主要用于构建富客户端应用。在3.0版本中,它提供了丰富的UI组件,包括日期时间控件。这个控件是开发人员在网页应用中处理日期和时间输入的重要工具,尤其适用于需要用户输入特定...

    ExtJS下拉列表树控件

    在ExtJS中,下拉列表树控件(ComboBox Tree)是常见的组件之一,它结合了下拉列表和树结构,提供了更丰富的用户界面。这个控件允许用户从一个层级化的数据结构中进行选择,非常适合于展示有层次关系的数据。 在创建...

    extjs4.2 datetime控件

    ExtJS是一个强大的JavaScript框架,主要用于构建富客户端应用。在ExtJS 4.2版本中,开发者可以使用DateTime控件来处理日期和时间的选择与显示,这尤其适用于需要精确到时分秒的场景。这个控件是ExtJS对基础日期选择...

    extjs 4.0 日期时间控件

    ExtJS 4.0 是一个流行的JavaScript框架,用于构建富客户端Web应用程序。它提供了一系列强大的组件,包括日期和时间选择器,使得用户界面更加友好和交互。在标题中提到的"extjs 4.0 日期时间控件",是指ExtJS 4.0框架...

Global site tag (gtag.js) - Google Analytics