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

About action buttons #7

Closed isurugit closed 10 years ago

isurugit commented 10 years ago

Hi,

Let me know, is there a possibility to add action buttons or links (add, edit, delete, view ) for each row of the table.?

Thanks.

JoseManuelAbarca commented 10 years ago

You could use the aoColumns property with fnRender function to add custom columns.The aoColumns property helps you to configure each columns, if you don't want to configure a particular column just pass null else you have to pass an object({}). The fnRender function helps you to create the links using the values of the other columns. You can use the oObj.aData to get the values of the other column like id to generate the links.

Example: aoColumns: [ null, // first column (RoleId) null, // second column (RoleName)
null, // third (UserId) null, // fourth (UserName)

                  {     // fifth column (Edit link)
                    "sName": "RoleId",
                    "bSearchable": false,
                    "bSortable": false,
                    "fnRender": function (oObj)                              
                    {
                        // oObj.aData[0] returns the RoleId
                        return "<a href='/Edit?id=" 
                            + oObj.aData[0] + "'>Edit</a>";
                    }
                   },

                   { }, // repeat the samething for the details link

                   { }  // repeat the samething for the delete link as well

               ]
isurugit commented 10 years ago

Thank you very much for your response. Meanwhile waiting for a response from your side, I wrote a small code like this. (though without proper exceptions handling) It is working for me, but I don't know the performance wise how far it will effect to the module. Anyway I'm putting it here, it may be helpful to some one :) .

 public function init()
{

    $this->getHeader('id')->getCell()->addDecorator('crud', 

    array(
        'active' => array(
            'url' => '/table/active/id/%s',
            'vars' => array('id'),
            'lable' => '<img src="http://zendapplication/table/inactive.png" onclick="return myFunction()">'
        ),  
        'inactive' => array(
            'url' => '/table/inactve/id/%s',
            'vars' => array('id'),
            'lable' => '<img src="http://zendapplication/table/inactive.png" onclick="return myFunction()">'
        ),      
        'read' => array(
            'url' => '/table/read/id/%s',
            'vars' => array('id'),
            'lable' => '<img src="http://zendapplication/table/active.png">'
        ),
        'update' => array(
            'url' => '/table/update/id/%s',
            'vars' => array('id'),
            'lable' => '<img src="http://zendapplication/table/inactive.png">'
        ),
        'delete' =>array(
            'url' => '/table/delete/id/%s',
            'vars' => array('id'),
            'lable' => '<img src="http://zendapplication/table/active.png">'
        )
    )

    )
    ;

}

Crud.php file

namespace ZfTable\Decorator\Cell;

use ZfTable\Decorator\Exception;

class Crud extends AbstractCellDecorator {

protected $crud_options;

/**
 * Constructor
 * @param array $options
 * @throws Exception\InvalidArgumentException
 */
public function __construct(array $options = array())
{

    $this->crud_options = $options;

}

public function crudRender($options){

    if (!isset($options['url'])) {
            throw new Exception\InvalidArgumentException('Url key in options argument required');
        }

        $url = $options['url'];
        $lable = $options['lable'];

        if (isset($options['vars'])) {
            $vars = is_array($options['vars']) ? $options['vars'] : array($options['vars']);
        }

        $values = array();
        if (count($vars)) {
            $actualRow = $this->getCell()->getActualRow();
            foreach ($vars as $var) {
                $values[] = $actualRow[$var];
            }
        }
        $urlfinal = vsprintf($url, $values);
        return sprintf('<a  href="%s">%s</a>', $urlfinal, $lable);

}

/**
 * Rendering decorator
 * @param string $context
 * @return string
 */
public function render($context)
{
    $output = '';

    if (isset($this->crud_options['active'])) {
        $output = $output . $this->crudRender($this->crud_options['active']);   
    }

    if (isset($this->crud_options['inactive'])) {
        $output = $output . $this->crudRender($this->crud_options['active']);   
    }

    if (isset($this->crud_options['read'])) {
        $output = $output . $this->crudRender($this->crud_options['read']);
    }

    if (isset($this->crud_options['update'])) {
        $output = $output . $this->crudRender($this->crud_options['update']);
    }       

    if (isset($this->crud_options['delete'])) {
        $output = $output . $this->crudRender($this->crud_options['delete']);
    } 

    return $output;
}

}

Thanks

dudapiotr commented 10 years ago

Hey, The best way is write own docorators like your example but always you can use template decorators:

$this->getHeader('actions')->getCell()->addDecorator('template', array(
            'template' => '<a href="/crud/edit/id/%s">Edit</a>'
                        . '<a href="/crud/delete/id/%s">Delete</a>',
            'vars' => array('id','id')
));