BootSelGridView.php 3.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
<?php
                 
/**
* BootSelGridView v2.3
* 
* for usage with twitter bootstrap Yii components
* 
* BootSelGridView remembers selected rows of gridview when sorting or navigating by pages. Also it can select particular rows by GET param when page is loading
*
* to highlignh selection add this code to your css:
* 
* table.table tr.selected td, table.table tr.selected:hover td {
*    background: none repeat scroll 0 0 #BCE774;
* }
* 
*/

Yii::import('bootstrap.widgets.TbGridView');

class BootSelGridView extends TbGridView
{
    /**
    * GET param name to pass selected row(s)
    * 
    * @var mixed
    */
    public $selVar;

    public $selBaseScriptUrl;

    public function init()
    {
        if($this->selectableRows > 0) {
            //generate name of variable for selection from request
            if(empty($this->selVar)) {
                $id = $this->dataProvider->getId();
                if(empty($id)) $id = $this->id;
                $this->selVar = (empty($id)) ? 'sel' : $id.'_sel';
            }
            //publish required assets
            if($this->selBaseScriptUrl===null)
                $this->selBaseScriptUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.selgridview.assets'));

        }
        parent::init();
    }


    /**
    * Creates column objects and initializes them. 
    * Automatically add hidden checkbox column.
    */
    protected function initColumns()
    {
        parent::initColumns();

        if($this->selectableRows == 0) return;

        //define primaryKey expression
        if($this->dataProvider instanceOf CActiveDataProvider) {
            $primaryKey = '$data->primaryKey';
        } else {
            $primaryKey = '$data["'.$this->dataProvider->keyField.'"]';
        }

        $checkedExpression = 'isset($_GET["'.$this->selVar.'"]) ? in_array('.$primaryKey.', is_array($_GET["'.$this->selVar.'"]) ? $_GET["'.$this->selVar.'"] : array($_GET["'.$this->selVar.'"])) : false;';

        //if gridview already has user defined Checkbox column --> set "checked" and exit
        //thanks to Horacio Segura http://www.yiiframework.com/extension/selgridview/#c7346
        foreach($this->columns as $col) {
            if($col instanceof CCheckBoxColumn) {
                $col->checked = $checkedExpression;
                $col->init();
                return;
            }
        }

        //creating hidden checkbox column
        $checkboxColumn = new CCheckBoxColumn($this);   
        $checkboxColumn->checked = $checkedExpression;
        $checkboxColumn->htmlOptions = array('style'=>'display:none');
        $checkboxColumn->headerHtmlOptions = array('style'=>'display:none');
        $checkboxColumn->init(); 

        $this->columns[] = $checkboxColumn;
    }

    /**
    * Registers necessary client scripts. 
    * Automaticlly prepend user's beforeajaxUpdate with needed code that will modify GET params when navigating and sorting
    */
    public function registerClientScript()
    {
        parent::registerClientScript(); 

        if($this->selectableRows > 0) {
            $id = $this->getId();

            $options=array(
            'selVar'=>$this->selVar,
            );

            $options=CJavaScript::encode($options);

            $cs=Yii::app()->getClientScript();
            $cs->registerScriptFile($this->selBaseScriptUrl.'/jquery.selgridview.js',CClientScript::POS_END);
            $cs->registerScript(__CLASS__.'#'.$id.'-sel',"jQuery('#$id').selGridView($options);");
        }
    }
}