1. #1
    Sencha - Ext JS Dev Team
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    195
    Vote Rating
    38
    nohuhu has a spectacular aura about nohuhu has a spectacular aura about

      0  

    Default Ext.ux.grid.feature.RowFilter - Grid-level row filtering plugin

    Ext.ux.grid.feature.RowFilter - Grid-level row filtering plugin


    My first foray in Ext Grid plugins:

    Code:
    /*
    
    Row filtering plugin for ExtJS 4 Grid class.
    
    Version 1.1
    
    Released under MIT license:
    
    Copyright (c) 2011 Alexander Tokarev <Alexander.Tokarev@nohuhu.org>
    
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    
    */
    /**
     * @class Ext.ux.grid.feature.RowFilter
     * @extends Ext.grid.feature.Feature
     *
     * RowFilter feature plugin allows filtering Grid rows out of Data Store, by utilizing
     * standard Ext.util.Filter functionality. Only rows that match the filter will be shown,
     * Store data set will remain unaffected. This is unlike Store.filter() behavior and was
     * meant to supplement it in situations when several Grids display subsets of the same
     * data and changing it is undesirable.
     *
     * Example:
     *
     * Ext.create('Ext.grid.Panel', {
     *     // other options
     *     features: [{
     *         ftype: 'rowfilter',
     *         filterCond: {
     *             property: 'foo',
     *             value: '42'
     *         },
     *		   filterDistinct: true
     *     }]
     * });
     *
     * See {@link Ext.util.Filter} for more details on how to use filters.
     *
     * Set optional filterDistinct = true to allow only unique records to show in the Grid.
     * Shallow comparison is done on prepared Grid rows, not original data. Think of it as
     * analogous to SELECT DISTINCT construction in SQL. 
     *
     * Note that this feature changes the data returned to the view, and therefore is not
     * compatible to other features that do the same, like Chunking and Grouping. This is
     * due to ExtJS limitation of one data-changing feature per grid view.
     *
     * There are no events fired by this feature.
    */
    
    Ext.namespace('Ext.ux.grid.feature');
    
    Ext.define('Ext.ux.grid.feature.RowFilter', {
        extend: 'Ext.grid.feature.Feature',
        alias: 'feature.rowfilter',
    	requires: [ 'Ext.util.MixedCollection' ],
        
    	hasFeatureEvent: false,
    
    	collectData: function(records, preppedRecords, startIndex, fullWidth, orig) {
    		if (!this.filterCond) {
    			return orig;
    		};
    	
    		var
    			filterCond,
    			filterObj,
    			filteredData,
    			filteredIdx,
    			i = 0,
    			recordCount = orig.rows.length,
    			col = Ext.create('Ext.util.MixedCollection'),
    			o = {
    				fullWidth: orig.fullWidth,
    				rows: []
    			};
    		
    		for (; i < recordCount; i++) {
    			var data = Ext.clone(records[i].data);
    			data['__idx__'] = i;
    			col.add(data);
    		};
    		
    		filterObj = Ext.create('Ext.util.Filter', this.filterCond);
    		
    		if (!filterObj) {
    			Ext.Error.raise("Can't create filter object, check filterCond property");
    		};
    		
    		filteredData = col.filter(filterObj);
    		filteredIdx = filteredData.collect('__idx__');
    		
    		while (filteredIdx.length > 0) {
    			i = filteredIdx.shift();
    			o.rows.push(preppedRecords[i]);
    		};
    		
    		if (this.filterDistinct === true) {
    			var
    				drows = [],
    				uniq = function(obj, arr) {
    					var	i, enc = Ext.encode(obj);
    					
    					for (i = 0; i < arr.length; i++) {
    						if (enc === Ext.encode(arr[i])) {
    							return false;
    						};
    					}
    					
    					return true;
    				};
    
    			for (i = 0; i < o.rows.length; i++) {
    				if (uniq(o.rows[i], drows)) {
    					drows.push(o.rows[i]);
    				};
    			};
    			
    			o.rows = drows;
    		};
    		
    		return o;
    	}
    });
    Feedback is appreciated.

    Regards,
    Alex.
    Last edited by nohuhu; 22 Jul 2011 at 6:58 AM. Reason: new version

  2. #2
    Sencha - Ext JS Dev Team
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    195
    Vote Rating
    38
    nohuhu has a spectacular aura about nohuhu has a spectacular aura about

      0  

    Default


    Posted version 1.1 with a couple of bugs fixed.

  3. #3
    Sencha - Community Support Team edspencer's Avatar
    Join Date
    Jan 2009
    Location
    Palo Alto, California
    Posts
    1,939
    Vote Rating
    9
    edspencer is a jewel in the rough edspencer is a jewel in the rough edspencer is a jewel in the rough

      0  

    Default


    Do you have any live examples anywhere?
    Ext JS Senior Software Architect
    Personal Blog: http://edspencer.net
    Twitter: http://twitter.com/edspencer
    Github: http://github.com/edspencer

  4. #4
    Sencha User
    Join Date
    Mar 2011
    Posts
    208
    Vote Rating
    0
    bee is on a distinguished road

      0  

    Default


    Can send us link for example please ?

  5. #5
    Sencha - Ext JS Dev Team
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    195
    Vote Rating
    38
    nohuhu has a spectacular aura about nohuhu has a spectacular aura about

      0  

    Default


    There is no example available at this time; I am not that good at ExtJS yet and cannot spend half a day putting together an example application. Besides, filtering approach turned out to be rather more difficult that I thought at first and this feature can only be used for display-only grids as of now. Selection mechanism in Grid is bound to underlying data Store too tightly, I have not found a way to bypass it yet. Any ideas are welcome.

    Regards,
    Alex.

  6. #6
    Sencha User atian25's Avatar
    Join Date
    Oct 2008
    Location
    china
    Posts
    114
    Vote Rating
    2
    atian25 is on a distinguished road

      0  

    Default


    any example or snapshot?
    @from: china
    @web: http://atian25.iteye.com
    @extensions: (extjs 4.x)
    * Ext.ux.grid.plugin.RowEditing - add some usefull features (v1.4 updated 2011-09-11)
    * Ext.ux.button.AutoRefresher
    * Ext.ux.form.field.DateTime

  7. #7
    Sencha User
    Join Date
    Mar 2011
    Posts
    37
    Vote Rating
    0
    ShrutiRuparel is on a distinguished road

      0  

    Default


    i just tried grid row filter plugin, works as expected thnk u..

  8. #8
    Sencha User
    Join Date
    Mar 2011
    Posts
    37
    Vote Rating
    0
    ShrutiRuparel is on a distinguished road

      0  

    Default


    oops..i just realized row filter plugin clashes with my drag n drop from grid to tree..it does not allow my grid rec to get correctly dropped onto tree