0

I have a "Model_Product" with data and a "statut" field. I would like to display my data on a grid an add an action button adapted to the statut :

$grid->addcolumn(Button, /* function of the statut field*/ )

By now I have :

$grid->addcolumn(Button, 'Action1');
$grid->addcolumn(Button, 'Action2');
$grid->addcolumn(Button, 'Action3');
$grid->addcolumn(Button, 'Action4');
$grid->addcolumn(Button, 'Action5');

And just 1 over the 5 can be trigered function of the "statut" field

Adrien
  • 3
  • 2

2 Answers2

0

I guess you can simply add one button and check which function you need to execute when you receive "button click" on server side. That should be rather easy. Other way would be to do something with JavaScript on client side, but that's not reliable and I think such solution would be not so nice and simple.

For example (untested):

$grid = $page->add('Grid');
$grid->setModel('Model_Product');

$b_name = 'details';
$b = $grid->addColumn('Button', $b_name);

if ($id = $_GET[$grid->name].'_'.$b_name) {
    $m = $this->add('Model_Product');
    $m->load($id);

    switch($m['status']) {
        case 1:
            // do something if status=1
            break;
        case 2:
            // do something if status=2
            break;
        case 3:
            // do something if status=3
            break;
    }

    // send some kind of javascript response
    $grid->js()->univ()->execute();
}
DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • An other way I was thinking about is to use servel icones and to hide the un-usable one. The one button solution will be confusing for my user, I will still need to change the name of the button. But then I don't know how to hide a button. – Adrien Jun 22 '13 at 23:47
  • You can overwrite $grid->formatRow() method too. Or create custom grid column formatter! – DarkSide Jun 25 '13 at 07:32
  • I was afraid to do it but at the end it was not so terrible ;) – Adrien Jul 16 '13 at 23:46
0

A solution I have used is to add a format type for Grid : into lib/Grid/advenced.php
I've added a smart button:

function init_smartButton($field){
    @$this->columns[$field]['thparam'].=' style="width: 40px; text-align: center"';
    $this->js(true)->find('.button_'.$field)->button();
}

function format_smartButton($field){
        $product = $this->add('Model_Product')->load($this->current_id);
        if($product->acceptButton($field))
        {
        $this->current_row_html[$field]='<button type="button" class="'.$this->columns[$field]['button_class'].'button_'.$field.'" '.
            'onclick="$(this).univ().ajaxec(\''.$this->api->url(null,
            array($field=>$this->current_id,$this->name.'_'.$field=>$this->current_id)).'\')">'.
                (isset($this->columns[$field]['icon'])?$this->columns[$field]['icon']:'').
                $this->columns[$field]['descr'].'</button>';
        }else{$this->current_row_html[$field]='';}
        $product->destroy();
    }

It's just like a normal button but it create the button only if my Product allows it.

then in my grid :

$grid->setModel('Model_Product')->addCondition('buyer_id','=',$userID); 
$grid->addColumn('smartButton' ,'1');
$grid->addColumn('smartButton' ,'2');
$grid->addColumn('smartButton' ,'3');
$grid->addColumn('smartButton' ,'4');
$grid->addColumn('smartButton' ,'5');
$grid->addPaginator(5);

It's not perfect but for now it will be ok! If you have something better to suggest I will be pleased to improve it.

Adrien
  • 3
  • 2