dudapiotr / ZfTable

*Not supported*ZfTable 3.1. Awesome ZF2 table / grid (and much much more) generator with huge possibilities of decorating and conditioning. Integrated with DataTables, Doctrine 2, Bootstrap 2.0 and 3.0.
http://dudapiotrek.laohost.net/
MIT License
75 stars 59 forks source link

Zend Url Helper in callable #63

Open hattingen opened 9 years ago

hattingen commented 9 years ago

I am using the callable decorator like this:

    $this->getHeader('id')->getCell()->addDecorator('callable', array(
        'callable' => function($context, $record){
            return '<a href="/admin/edit/something/'.$record->getUuid().'">'.$record->getId().'</a>';
        }
    ));

Is it possible to use the view helper and routes here?

olekhy commented 9 years ago

inject from service manager is necessary here. as rough example.

class Link extends AbstractTable
{

    /**
     * @var ServiceLocatorInterface
     */
    protected $viewHelperManager;

    protected $config = array(
        'name' => 'Link decorator',
        'showPagination' => true,
        'showQuickSearch' => false,
        'showItemPerPage' => true,
    );

    /**
     * @var array Definition of headers
     */
    protected $headers = array(
        'id' => array('title' => 'Id', 'width' => '50') ,
        //...
    );

    /**
     * @param ServiceLocatorInterface $viewHelperManager
     */
    public function setViewHelperManager(ServiceLocatorInterface $viewHelperManager)
    {
        $this->viewHelperManager = $viewHelperManager;
    }

    /**
     * @return ServiceLocatorInterface
     */
    public function getViewHelperManager()
    {
        return $this->viewHelperManager;
    }

    /**
     * @param $name
     * @param $args
     *
     * @return mixed
     */
    public function __call($name, $args)
    {
        return call_user_func_array($this->getViewHelperManager()->get($name), $args);
    }
    public function init()
    {
        $this->getHeader('id')->getCell()->addDecorator('callable', array(
            'callable' => function($context, $record){
                return '<a href="' . $this->url('your route name', [$record->getUuid()]) ' . '">'.$record->getId().'</a>';
            }
        ));
    }
}

$tblGrid = new Link(/*...*/);
$vhm = $di->get('viewHelperManager');
$tblGrid->setViewHelperManager($vhm);
hattingen commented 9 years ago

Thx. Have it working now. My tries were similar, but I missed the last part.