[SOLVED] Problems with filter, sort and pagination of a list

I am playing around with a little application to manage buildings and rooms.

In the actionAdmin() of my RoomController I want to show a list of (all) rooms. Each room belongs to a building, having a particular level. In the view I show a list of the room names with the corresponding building name and level.

Example:

Room 1		Building 1	1st Floor


Room 2		Building 1	1st Floor


Room 3		Building 1	2nd Floor


Room 4		Building 2	1st Floor

As shown in the Cookbook Article, I introduced a DropDownList in order to be able to filter the rooms list by their building:

<?php echo CHtml::form('','get'); ?>


Building:


<?php echo CHtml::dropDownList('building',


    isset($_GET['building'])?(int)$_GET['building']:0,


    CHtml::listData($buildings,'idBuilding','Name'),


    array('empty'=>'All Buildings', 'submit'=>'')); ?>


</form>

My first problem is, that the form submits to e.g. index.php?building=1 instead of index.php?r=room/admin&building=1. What may I have done wrong?

My second problem is, that when I filtered a list (working already if adding the parameter to the URL), this filter is not used again when pressing one of the pagination buttons or changing the sort of the table by pressing a column header. So with selecting another page or changing sort, the filter (although still displayed in the DropDownList) will not be applied any more. How can I make the filter work even with paginator's page changes or sort changes?

Does anybody have a code snippet showing the cooperation of filter, sort and pagination?

Thank you and best regards

Thomas

Some additions, things I found out so far:

The empty 'submit' parameter of the DropDownList generates a correct looking URL for the form. Even the sort and pagination parameters are included.

Here is a part of the page source:

/feweon/index.php?r=room/admin&amp;sort=Building

And this is - for comparison - the working link generated by the yiiPager for page #2:

/feweon/index.php?r=room/admin&amp;sort=Building&amp;page=2

So the links look quite good, the parameters for sort and pagination seem to be OK, the only thing still not working is submitting the form. Although the URL seems correct, it goes to the default start page.

Any hints and help appreciated  :)

Greets

Thomas

This is by the design of HTML. If you are using GET form submission, the GET parameters in the form action will be ignored.

Hello Qiang,

Quote

If you are using GET form submission, the GET parameters in the form action will be ignored.

Thank you for that hint. I wasn't familiar with that issue. I now added a few lines to the view (inside the form) and now everything is working!



    if (isset($_GET['r'])) {


        echo CHtml::hiddenField('r',$_GET['r']);


    }


    if (isset($_GET['sort'])) {


        echo CHtml::hiddenField('sort',$_GET['sort']);


    }


    if (isset($_GET['page'])) {


        echo CHtml::hiddenField('page',$_GET['page']);


    }

Is this a good way, or is there an even better or easier one? Should this matter somehow been added to the Cookbook article?

Thanks and Greets from Germany

Thomas

Yes, it could be a cookbook article. The problem is to find a good example to show.