0

I am using Agile Toolkit. I have a drop-down field in my CRUD.

How can I make the "New" button display different set of values in this drop-down to when the "Edit" button is clicked?

Here is my code:

 class page_things extends Page {
    function init(){
        parent::init();
        $p = $this;

        $f = $p->add('Form');

        $idCat = ($f->get('idCat')?$f->get('idCat'):$this->api->getConfig('idCat','MASP2U03'));


        $dpUE = $f->addField('dropdown', 'Category');
        $dpUE->setModel('Category');
        $dpUE->js('change',$f->js()->submit());
        $dpUE->set($idCat);

        $f->addSubmit('OK');

        $c = $f->add('CRUD');

        $c->setModel('things',array('name', 'field1', 'field2', 'field3'))->setMasterField('idCat',$idCat);


        if($f->isSubmitted()){

            $c->js(true)->show()->execute()->reload()->execute();

            }

    }
}

Thank you for help !!

romaninsh
  • 10,606
  • 4
  • 50
  • 70

2 Answers2

0

use

$crud=$this->add('CRUD',array('allow_add'=>false));

to disable default Add button, then add your own button:

if($crud->grid)$crud->grid->addButton('Add')->js('click')
    ->frameURL('Add',$this->api->url('./new'));

After this you'll need to create a new page

class page_things_new extends Page {

and inside this page define the form the way you want it to appear no the "add" click. I don't fully understand your question, but with these instruction you can have a different page appear when adding new entries to your crud.

romaninsh
  • 10,606
  • 4
  • 50
  • 70
  • Thank you a lot ! With your code and sources, I try this solution : `if($crud->form) { $thefield = $c->form->getElement('idCat')->set($idCat)->js(true)->hide(); }` It works ! – user1514794 Jul 12 '12 at 15:28
  • Great. I've edited your initial question to be useful for others too, please review it. – romaninsh Jul 15 '12 at 11:24
0

Here's an alternative to Romans that I've tried. It uses $this->api->memorize to store a GET variable chosen in the drop down list in a session variable. Then in the Form, you can set default the chosen value by using recall in the model.

Something like this

in page/things

 // load the javascript function (see later)
 $this->js()->_load('your_univ');

 /*****************************************************************/
 /* Code to populate drop down lists - amend where as required*/
            $catList=$this->api->db->dsql()->table('category c')
                     ->field('c.id')
                     ->field('c.name')
                     ->where('c.type',$cat_type)
                     ->order('c.id')
                     ->do_getAssoc();

    // Check if one is set on URL or default from config and memorize the value
    if ($_GET['cat']){
      $cat=$_GET['cat'];
    } else {
      $cat=$this->api-getConfig('idCat');
    }
    $this->api->memorize('category',$cat);


    $f=$p->add('Form',null,null,array('form_empty'))
         ->setFormClass('horizontal bottom-padded');
    $l1=$f->addField('dropdown','category')->setValueList($catList)->set($cat);

    // calls a bit of javascript described later to reload with the parameter
    $l1->js('change')->univ()->yourfunc($p->api->getDestinationURL(null), $l1);

    .. rest of your page code goes here ..

Then in /lib/Model/Category.php

Add the following recall for the field

  $this->addField('idCat')->system(true)->visible(false)
       ->defaultValue($this->api->recall('category'));

Note the system(true) and visible(False) means it wont show up and wont be changeable on the CRUD but you can play around with the options so it shows in the CRUD grid but not in the form.

Finally, the little bit of javascript to make the reload work (Romans might advise a better way to do this). Make sure yourfunc matches in the page and in the js.

in /templates/js/your_univ.js add the following

    $.each({
        yourfunc: function(url, name){
              document.location.href=url+'&cat='+$(name).val();
        },
    },$.univ._import);

I've got code similar to this working in my own pages. You can probably make it work as a POST as well as the drop down is a form if you dont want the cat to show on the URL.

Trevor North
  • 2,286
  • 1
  • 16
  • 19