PDA

View Full Version : Unable to Retrieve Only Null Dates with Ext.ux.grid.GridFilters



sizemorew
12 Jan 2011, 9:12 AM
I'm working with a grid where I need a button to filter items that have been closed. These are flagged by the Closed Date which--if an item is still open--should be set to null.

I'm using the following code to try and set the filter but I found a rather bizarre bug where instead of filtering out the items that have a Closed Date, it's filtering to show only the items with a Closed Date.



filters.getFilter('closedDate').setValue({on: null});
filters.getFilter('closedDate').setActive(true);
grid.getStore().reload();


I've tried setting the value simply to null, but that does absolutely nothing but sit there. I also tried using a different filter to check for a null in the Closed By field we have in place but that also does nothing.

So my question is this: Is there a way to filter using GridFilters where I retrieve only those records with null dates?

I've already poked my nose through the User Extension Documentation and have been looking through the main Ext.ux.grid.GridFilters thread. I've also been hunting through the forums for the past two hours and will continue looking. Any and all assistance is much appreciated.

Additional Information: I also tried filtering after and before certain dates to see if the records with null dates would show up in the list. They didn't.

sizemorew
13 Jan 2011, 10:14 AM
Resolved the issue where I was unable to filter by empty dates. Posting code here in case someone else wants to do the same thing.

In DateFilter.js

Add to private variables:


/**
* @cfg {String} blankText
* Defaults to 'Blank'
*/
blankText: 'Blank',


Add to the init function:


var blankCheckbox = new Ext.menu.CheckItem({
text: this['blankText'],
listeners: {
scope: this,
checkchange: this.onCheckChange
},
itemId: 'blank'
});

this.fields['blank'] = blankCheckbox;
this.menu.add(blankCheckbox);


Replace onMenuSelect:


var fields = this.fields,
field = this.fields[menuItem.itemId];

field.setChecked(true);

if (field == fields.blank){
fields.before.setChecked(false, true);
fields.after.setChecked(false, true);
fields.on.setChecked(false,true);
}
else if (field == fields.on) {
fields.before.setChecked(false, true);
fields.after.setChecked(false, true);
fields.blank.setChecked(false, true);
} else {
fields.blank.setChecked(false,true);
fields.on.setChecked(false, true);
if (field == fields.after && fields.before.menu.picker.value < value) {
fields.before.setChecked(false, true);
} else if (field == fields.before && fields.after.menu.picker.value > value) {
fields.after.setChecked(false, true);
}
}
this.fireEvent('update', this);


Replace getValue:


var key, result = {};
for (key in this.fields) {
if (key == 'blank')
{
result[key] = this.fields[key].checked;
}
else if (this.fields[key].checked) {
result[key] = this.fields[key].menu.picker.getValue();
}
}
return result;


Replace setValue:


var key;
for (key in this.fields) {
if (key == 'blank')
{
this.fields[key].setChecked(value[key]);
}
else if(value[key]){
this.fields[key].menu.picker.setValue(value[key]);
this.fields[key].setChecked(true);
} else if (!preserve) {
this.fields[key].setChecked(false);
}
}
this.fireEvent('update', this);


Add to validateRecord before the check if val is a date:


if (this.fields['blank'].checked)
{
return (val == null);
}

sizemorew
21 Jan 2011, 1:23 PM
Fixed a few bugs that could cause the GridFilters object to crash.

In getSerialArgs, replace with this code:


var args = [];
for (var key in this.fields) {
if (key == 'blank')
{
if (this.fields[key].checked)
{
args.push({
type: 'boolean',
value: this.fields[key].checked
});
}
}
else
{
if(this.fields[key].checked){
args.push({
type: 'date',
comparison: this.compareMap[key],
value: this.getFieldValue(key).format(this.dateFormat)
});
}
}
}
return args;


In getFieldValue, replace with this code:


if (item == 'blank')
{
return this.fields[item].checked;
}
else
{
return this.fields[item].menu.picker.getValue();
}


In getPicker, wrap an if statement around the return statement like this:


if (item != 'blank')
{
return this.fields[item].menu.picker;
}


Sorry for any bugs that people had cropping up when using my modification. The little gremlin didn't show up until recently on my end.

darthwes
21 Jan 2011, 8:03 PM
1.) You can use a static constant (such as the string "null" or perhaps a string like "-" or "undef" or something) so that when the user puts that into the filter you can catch it on your backend and modify your filtering clause.

2.) You can use a list filter and use "Open" and/or "Closed" as list options, catch it on the backend and modify the filtering clause.

I don't think modifying the grid filters ux is a good idea because of maintenance costs (like what happens when you move to the next version of ext/grid filters, how does the next programmer know how this works? Etc. Just my .02....