PDA

View Full Version : Ext.ux.grid.feature.RowFilter - Grid-level row filtering plugin



nohuhu
21 Jul 2011, 6:56 AM
My first foray in Ext Grid plugins:



/*

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.

nohuhu
22 Jul 2011, 6:58 AM
Posted version 1.1 with a couple of bugs fixed.

edspencer
29 Jul 2011, 4:40 PM
Do you have any live examples anywhere?

bee
18 Aug 2011, 11:32 AM
Can send us link for example please ?

nohuhu
19 Aug 2011, 10:40 AM
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.

atian25
21 Aug 2011, 1:25 AM
any example or snapshot?

ShrutiRuparel
7 Sep 2011, 10:19 PM
i just tried grid row filter plugin, works as expected :) thnk u..

ShrutiRuparel
7 Sep 2011, 11:18 PM
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 :(