Checkbox in p4a_table

Developers
Dmitry
2009-07-30
2013-05-15
  • Dmitry

    Dmitry - 2009-07-30

    Hi All!

    I've implement some functionality I'd like to share with other p4a developers.
    Sometimes it is useful to select multiple rows in a table for some butch processing. Following class  utilize 'action' column feature to make rows selectable.
    You can use this code as workaround  until this feature be implemented in base P4A_Table
    .
    Any extensions of this code are welcome. :)

    <?php

    class SelectableRowsTable extends P4A_Table

    {

        protected $selected_rows = array();

        protected $pk= null;

       

        /**

         * @param string $name Mnemonic identifier for the object

         */

        public function __construct($name)

        {

            parent::__construct($name);

            $this->addCSSClass("p4a_table");

        }

        public function addSelectCol(){

            $column_name='selectaction';

           

            $this->pk = $this->data->getPk();

            if ($this->pk !== null) {

                $this->cols->build('p4a_table_col', $column_name);

           

                if (!empty($this->_cols_order)) {

                    $this->_cols_order[] = $column_name;

                }

                $this->cols->$column_name->setType('action');

                    $this->cols->$column_name->addAction('onclick');

               

                $this->intercept($this->cols->$column_name,'afterClick','invertSelection');

            }

            return $this;

        }

           

        public function isRowSelected($pk){

            return isset($this->selected_rows[$pk]);

        }

       

        public function getSelectedPks(){

            return $this->selected_rows;

        }

       

        public function getRows($num_page, $rows)

        {

            $tmprows=$this->rows->getRows($num_page, $rows);

           

            $id = $this->getID();

            foreach($tmprows as $i => $row){

                foreach($row['cells'] as $j=>$cell){

                if($cell['value']=='Selectaction'){

                    $pkvalue=$rows[$i][$this->pk];

                    $tmprows[$i]['cells'][$j]['value']= "<input type='checkbox' id='{$id}input' name='test' value='{$pkvalue}' ".(isset($this->selected_rows[$pkvalue])? "checked/>":"/>");

                    $tmprows[$i]['cells'][$j]['action']= $this->cols->selectaction->composeStringActions( array($i, 'Selectaction', $pkvalue));

                }

                }

            }

            return $tmprows;

        }

       

        public function invertSelection($param1,$param2){

            if(isset($this->selected_rows[$param2[2]])){

            unset($this->selected_rows[$param2[2]]);

            } else $this->selected_rows[$param2[2]]=true;       

        }

    }

     
  • Anonymous - 2011-01-17

    This is very useful,
    BTW, any trick to move the checkbox to first column. Thanks

     

Log in to post a comment.