Default value for datagrid filter

Login or register to post comments
12 replies [Last post]
Offline
Joined: 08/12/2009

Hi guys,

Is it possible to assign a default value to a column filter?

Thanks!

vakopian's picture
Offline
Joined: 04/08/2008

cmpscabral,

You can get access to the filter controls in the datagrid by using the GetFilterControl method:

$objFilterControl = $objDatagrid->GetFilterControl($objColumn);

Then you can set the default value:

$objFilterControl->Text = 'blah';

Offline
Joined: 08/12/2009

Vakopian, thanks for your reply

I tried your suggestion but:

1. it only works on text controls, on list controls won't do anything
2. it won't filter the data, just sets the value

I'm using 1.1.1, but I tried with 1.1.3 and 2.0.2 and got the same results.

Any idea why? Thanks!

vakopian's picture
Offline
Joined: 04/08/2008

1. For list controls you should use one of the list control properties like SelectedValue, SelectedName or SelectedIndex, for example:

$objFilter->SelectedIndex = 3;

2. You're right, it doesn't do it automatically (I think it should). One workaround is to call:

$this->dtg->btnFilter_Click(null, null, null);

after your datagrid is fully setup.

Offline
Joined: 08/12/2009

Hi Vakopian,

Yep, the btnFilter_Click did the trick, thanks! But, setting the initial value for list controls only works using:

<?php
$objFilter
->SelectedIndex = 1;
?>

Using "SelectedValue" doesn't work.

vakopian's picture
Offline
Joined: 04/08/2008

Are you sure you're using the correct value when setting SelectedValue?
When setting SelectedValue, you should use one of the strings you used as value when creating the list items. Similarly, to use SelectedName, you should use the name of one of the items.
If it really doesn't work for you, could you try it in 2.0.2 to check if it's a bug in the older version?

Offline
Joined: 08/12/2009

Yes, I'm using the correct values. I tried with 1.1.1, 1.1.3 and 2.0.2 with the same results.

I'd gladly post a patch, but can't really know where to begin and find the error...

vakopian's picture
Offline
Joined: 04/08/2008

cmpscabral,

This definitely works in 2.0.2 (and probably in the older versions too). The easiest way to test this is to try it on one of the examples that comes with QCubed. Here is what I suggest you can do: First check the "Advanced QDataGrid Filtering" example and note the "Type" filter dropdown in the first grid. Then edit the advanced_filtering.php file (it's in assets/_core/php/examples/datagrid/advanced_filtering.php), and add the following line right after line 45

$this->dtgProjects->GetFilterControl($colType)->SelectedValue = 'Cancelled';

Refresh the example page, and notice that the dropdown has "Cancelled" selected.
Same way you can try
$this->dtgProjects->GetFilterControl($colType)->SelectedName = 'Open';

Refresh again and see that now "Open" is selected.

Please let us know if this modified example doesn't work for you either.

Offline
Joined: 08/12/2009

Hi Vakopian,

yes, it works! The only difference I can see is that, on my project, I'm setting the filters after I add the columns to the datagrid; in advanced_filtering.php, the columns are added after the filters are set.

checking right now if it works, thanks again for your help!

Offline
Joined: 08/12/2009

ok, after 3 hours of debugging, I found the issue, it has nothing to do with my previous post.

when I setup a new filter this is what I do:

(...)
$col->FilterType = QFilterType::ListFilter;
foreach($arrObj as $itm) {
  $col->FilterAddListItem(
    $itm->Description,
    QQ::Equal(QQN::SomeObj()->SomeField,$itm->Id)
  );
}
(...)

When I tried

$dtg->GetFilterControl($col)->SelectedValue= 'some_value';

I thought 'some_value' was supposed to be the value in $itm->Id, but instead it must be the value in $itm->Description.

Does this make sense? Am I doing something wrong? I thought the SelectedValue would be compared to the QQCondition given in FilterAddListItem().

Thanks!

Offline
Joined: 03/31/2008

Going back to the original post, you need to activate the filter for the column.
So for list columns you do:

$col->FilterActivate($strText);

And for text columns you set the text, then call
$col->FilterActivate();

You shouldn't have to fake a button click.

vakopian's picture
Offline
Joined: 04/08/2008

cmpscabral,

When you use FilterAddListItem(), it sets the first argument of that function both as "name" and as "value" in the list box. That's why SelectedValue should be set to "$itm->Description". "$item->Id" is only used in the condition, the list box control itself knows nothing about it (nor the condition).

The solution VexedPanda proposed is probably the better approach. But you still need to pass "$item->Description" to FilterActivate().

Offline
Joined: 08/12/2009

Ok, got it - thanks both for your help.

Just for curiosity, any particular reason why FilterAddListItem() uses the same value for "name" and "value" and why it's different from the value QQ::Condition is aware of?

Edit: Just to clarify anyone with this issue:

For listbox filter:

$dtg->GetFilterControl($col)->SelectedValue = 'some_text';
$col->FilterActivate('some_text');

For textbox filter:
$dtg->GetFilterControl($col)->Text = 'some_text';
$col->FilterActivate();
$col->FilterSetOperand('some_text');