maandag 11 februari 2008

Adding a filter to scaffolded tables

I just wrote a start for a filter which I can easily add above scaffolded tables. Scaffolding is great, but I want a form above to limit the number on results based on my parameters. It is not finished, but I just post what I have, any tips, let me know.

Ok, it uses a helper and a component.


Suppose you have a tables projects and you use paginate in your index() function in your controller. Change it to:

$this->set('projects', $this->paginate(null, $this->Activeview->getConditions($this->params)));

This will use your parameters from the form in your view to fetch the records based on your limitations.

In your view, for example use:

echo $activeview->filter('projects', array(
"name" => __('Name', true),
"targetgroup" => __('Doelgroep', true)
));


or search in related tables:

echo $activeview->filter('communication', array(
"Person.surname" => __('Person', true),
"note" => __('Note', true)
));


Let me know what you think about this.

13 opmerkingen:

  1. [...] CakePHP Tips » Adding a filter to scaffolded tables Looks interesting… (tags: cakephp filter scaffold) [...]

    BeantwoordenVerwijderen
  2. sounds very useful. I also think it'd be handy to be able to pass a query string parameter (something like /images/index/user_id=3)as well to specify the filters

    BeantwoordenVerwijderen
  3. Hi Bryn,

    It looks for parameters in the URL, so you could try using like_user_id=3 in your url.

    BeantwoordenVerwijderen
  4. It is interesting, but unfortunately it does not work. I am new to cakephp and probably I have not properly installed or named your files. Here it is what I did.
    1)I put the helper code (filename active_view.php) in this directory:
    C:\www\cake_1_2\app\views\helpers\
    2)I put the component code (filename activeview.php) in this directory:
    C:\www\cake_1_2\app\controllers\components
    3)I put this line in the index() function of products_controller.php:
    $this->set('products', $this->paginate(null, $this->Activeview->getConditions($this->params)));
    4) I put, in the index.cpt view for products this line:
    echo $activeview->filter('products', array(
    "title" => __('Title', true)
    ));
    I get this error message:
    Notice (8): Undefined property: ProductsController::$Activeview [APP\controllers\products_controller.php, line 14]

    Code

    $this->Product->recursive = 0;

    //$this->set('products', $this->paginate());

    $this->set('products', $this->paginate(null, $this->Activeview->getConditions($this->params)));

    ProductsController::index() - APP\controllers\products_controller.php, line 14
    Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 268
    Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 240
    [main] - APP\webroot\index.php, line 84


    Fatal error: Call to a member function getConditions() on a non-object in C:\www\cake_1_2\app\controllers\products_controller.php on line 14
    Can you help me?
    Thanks in advance
    Greetings from Rome

    BeantwoordenVerwijderen
  5. Ok, it works now. I had forgotten to put :
    var $helpers = array('Html', 'Javascript', 'Form', 'Activeview');
    var $components = array('Auth', 'RequestHandler', 'Activeview');

    and also helper's name was different.

    Now, I want to try to extend this component (for example, I would like to associate a select box to fields whose values come from a list, or a calendar to fields of type "date".
    Another possible extension is to add a select box between label and input field so that users can select an comparison operator such as =, , <= and so on .
    Finally, it would be good if after filtering and view the detail of one of returned items, when you go back to the index page filters have been maintained (by session variable, i.e.).
    Thanks
    Stefano

    BeantwoordenVerwijderen
  6. Hi Stefano,

    Those improvements are very welcome. I wish I could have added them myself already, but haven't got time for it. Anyways, they could be very handy. It would make the filter more useful. What I created now is just a start.

    BeantwoordenVerwijderen
  7. Hi, I am trying to realize these improvements but as matter as I am quite new to cakephp I am experiencing some difficulties which I think depend from cakephp mechanism. I have realize the "base code" and it works, but I want, for obvious reasons, filters data to be sent by "post" instead of "get" method.
    Well, if I try to substitute the line
    echo $this->Form->create($controller, array('type' => 'get', 'action' => 'index'));
    with:
    echo $this->Form->create($controller, array('type' => 'post', 'action' => 'index'));

    in the activeview.php file under view/helpers directory, when I press "Search" button I get "Page not found".
    Any idea?
    Thanks for any help
    Stefano

    BeantwoordenVerwijderen
  8. I am still in trouble... I checked both the forms created setting type to get and setting type to post and I noticed all a relevant difference:
    -- FORM WITH TYPE GET




    Title

    uguale a
    contiene








    -- FORM WITH TYPE POST







    Title

    uguale a
    contiene








    that is in form created setting type to post the select box name is instead of, how it would be correct,


    It seems that cakephp, if you choose to create form with type post, add data[product] [select name] as name for select box.
    Any suggestion?
    Have a nice day
    Stefano

    BeantwoordenVerwijderen
  9. Ops, all html code of my post has been truncated..
    Well, what I noticed is that if I use
    echo $this->Form->create($controller, array(’type’ => ‘get’, ‘action’ => ‘index’));
    select box name is set, corretly to "_O_product_title", whilst if I use echo $this->Form->create($controller, array(’type’ => ‘post’, ‘action’ => ‘index’));
    select box name is set, wrongly, to "data[products][_O_product_title]" .
    Has cakephp any kind of automatism that produce this result?
    Thanks
    Stefano

    BeantwoordenVerwijderen
  10. Hello,
    I am new to Cake and I was wondering if someone could give me an example on how to use the above to produce a filter but use dropdowns instead of text boxes. What changes would I need ot make in the help and or component?

    Thanks!

    BeantwoordenVerwijderen
  11. Nice stuff.
    I was looking long time for something like that, so easy to use, so cake.
    But, indeed, will be need some improvements:
    1. using dropdowns
    2. remember selections (filters)
    I'm looking forward to improve this.
    Is a great starting point.

    BeantwoordenVerwijderen
  12. Hi Baker,

    Glad you liked my little start for this filter. I changed your comments a little bit, but the layout isn't perfect, will take a look at that later. If anyone can suggest a good plugin, let me know.

    I just did a little test of your enhancement, seems pretty nice!

    BeantwoordenVerwijderen
  13. Hi guys! Just register and sow, this is the best forum in you city! Sorry, for my bad english. How do you
    looking for exchanging links?

    [url=http://hkqroue.angelfire.com/free-anime-sex-pic.html]free anime sex pic[/url]
    [url=http://zfwauyu.angelfire.com/video-film-sex-porn-free.html]video film sex porn free[/url]
    [url=http://sluzsvo.angelfire.com/hardcore-info-remember-sex.html]hardcore info remember sex[/url]
    [url=http://sluzsvo.angelfire.com/devin-first-her-lesbian-sex.html]devin first her lesbian sex[/url]
    [url=http://zfwauyu.angelfire.com/anal-sex-video-free-preview.html]anal sex video free preview[/url]
    [url=http://sluzsvo.angelfire.com/yahoo-group-list-sex.html]yahoo group list sex[/url]
    [url=http://zfwauyu.angelfire.com/index9.html]free black gay sex clip[/url]
    [url=http://zfwauyu.angelfire.com/free-american-gay-sex-video.html]free american gay sex video[/url]
    [url=http://meuaeuy.angelfire.com/homemade-sex-video.html]homemade sex video[/url]
    [url=http://qgujhjg.angelfire.com/nude-sex-photo.html]nude sex photo[/url]


    See you later. Buy-buy.

    BeantwoordenVerwijderen