EDataTables extension

CGridView replacement.

Resources

Demo

NameDateRateName
Gold2012/01/121,653US Dollar
Gold2012/01/111,641US Dollar
Gold2012/01/101,627US Dollar
Gold2012/01/091,618US Dollar
Gold2012/01/061,621US Dollar
Gold2012/01/051,615US Dollar
Gold2012/01/041,603US Dollar
Gold2012/01/031,590US Dollar
Gold2011/12/301,575US Dollar
Gold2011/12/291,538US Dollar
Gold2011/12/281,584US Dollar
Gold2011/12/231,608US Dollar
Gold2011/12/221,609US Dollar
Gold2011/12/211,638US Dollar
Gold2011/12/201,605US Dollar
Gold2011/12/191,593US Dollar
Gold2011/12/161,590US Dollar
Gold2011/12/151,590US Dollar
Gold2011/12/141,635US Dollar
Gold2011/12/131,665US Dollar
Gold2011/12/121,680US Dollar
Gold2011/12/091,712US Dollar
Gold2011/12/081,739US Dollar
Gold2011/12/071,731US Dollar
Gold2011/12/061,720US Dollar

Controller

<?php
 
// if EDataTables would be used more commonly, this should be in the 'import' section of config/main.php
Yii::import('vendor.nineinchnick.edatatables.*');
 
class EdatatablesController extends Controller
{
    public function actions() {
        return array(
            'export' => array(
                'class'        => 'ext.exporter.ExportAction',
                'modelClass'=> 'PreciousMetalFixing',
                'columns'    => $this->columns(),
                'widget'    => array('filename' => 'goldFixings.csv'),
            ),
        );
    }
 
    protected function columns()
    {
        return array(
            'preciousMetal.name:text',
            'date:date',
            array(
                'name'=>'rate',
                'type'=>'number',
                'value'=>'$data->rate/100.0',
            ),
            'currency.name:text',
        );
    }
 
    /**
     * Most basic EDataTables example, reading data from a database.
     */
    public function actionIndex()
    {
        $model = new PreciousMetalFixing('search');
        $model->unsetAttributes();
 
        $columns = $this->columns();
        /**
         * @var $widget EDataTables
         */
        $widget=$this->createWidget('vendor.nineinchnick.edatatables.EDataTables', array(
            'id'            => 'goldFixing',
            'dataProvider'  => $model->search($columns),
            'ajaxUrl'       => $this->createUrl($this->getAction()->getId()),
            'columns'       => $columns,
        ));
        if (Yii::app()->getRequest()->getIsAjaxRequest()) {
            echo json_encode($widget->getFormattedData(intval($_GET['sEcho'])));
            Yii::app()->end();
            return;
        }
        $this->render('index', array('widget' => $widget,));
    }
 
    /**
     * Same as index action, just added the 'buttons' option and if 'print' is true, disables pagination and renders without menu.
     * @param mixed $print if specified, will disable pagination and main layout
     */
    public function actionToolbar($print=null)
    {
        $model = new PreciousMetalFixing('search');
        $model->setAttributes(array_fill_keys($model->attributeNames(), null));
 
        $columns = $this->columns();
        $dataProvider = $model->search($columns);
        if ($print !== null)
            $dataProvider->setPagination(false);
        /**
         * @var $widget EDataTables
         */
        $widget=$this->createWidget('vendor.nineinchnick.edatatables.EDataTables', array(
            'id'            => 'goldFixing',
            'dataProvider'  => $dataProvider,
            'ajaxUrl'       => $this->createUrl($this->getAction()->getId()),
            'columns'       => $columns,
            'buttons'        => array(
                'print' => array(
                    'label' => Yii::t('app','Print'),
                    'text' => false,
                    'htmlClass' => '',
                    'icon' => Yii::app()->theme!==null&&Yii::app()->theme->name=='bootstrap' ? 'icon-print' : 'ui-icon-print',
                    'url' => $this->createUrl('toolbar', array('print'=>true)),
                ),
                'export' => array(
                    'label' => Yii::t('app','Save as CSV'),
                    'text' => false,
                    'htmlClass' => '',
                    'icon' => Yii::app()->theme!==null&&Yii::app()->theme->name=='bootstrap' ? 'icon-download-alt' : 'ui-icon-disk',
                    'url' => $this->createUrl('export'),
                ),
            ),
        ));
        if (Yii::app()->getRequest()->getIsAjaxRequest()) {
            echo json_encode($widget->getFormattedData(intval($_GET['sEcho'])));
            Yii::app()->end();
            return;
        }
        if ($print !== null)
            $this->renderPartial('toolbar', array('widget' => $widget));
        else
            $this->render('toolbar', array('widget' => $widget,));
    }
 
    public function actionReport()
    {
 
    }
}

Model

<?php
 
Yii::import('vendor.nineinchnick.edatatables.*');
 
/**
 * This is the model class for table "{{precious_metal_fixings}}".
 *
 * The followings are the available columns in table '{{precious_metal_fixings}}':
 * @property integer $id
 * @property string $date
 * @property integer $rate
 * @property integer $currency_id
 * @property integer $precious_metal_id
 *
 * The followings are the available model relations:
 * @property PreciousMetals $preciousMetal
 * @property Currencies $currency
 */
class PreciousMetalFixing extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{precious_metal_fixings}}';
    }
 
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('date, rate, currency_id, precious_metal_id', 'required'),
            array('rate, currency_id, precious_metal_id', 'numerical', 'integerOnly'=>true),
            // The following rule is used by search().
            array('id, date, rate, currency_id, precious_metal_id', 'safe', 'on'=>'search'),
        );
    }
 
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array(
            'preciousMetal' => array(self::BELONGS_TO, 'PreciousMetal', 'precious_metal_id'),
            'currency' => array(self::BELONGS_TO, 'Currency', 'currency_id'),
        );
    }
 
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => Yii::t('models','ID'),
            'date' => Yii::t('models','Date'),
            'rate' => Yii::t('models','Rate'),
            'currency_id' => Yii::t('models','Currency'),
            'precious_metal_id' => Yii::t('models','Precious Metal'),
        );
    }
 
    /**
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     * @param array $columns columns definition used in CGridView
     */
    public function search(array $columns)
    {
        $criteria=new CDbCriteria;
 
        if (isset($_GET['sSearch'])) {
            $criteria->compare('date',$_GET['sSearch'],true,'OR');
            $criteria->compare('rate',$_GET['sSearch'],false,'OR');
            //$criteria->compare('currency_id',$_GET['sSearch'],true,'OR');
        }
 
        $criteria->compare('id',$this->id);
        $criteria->compare('date',$this->date,true);
        $criteria->compare('rate',$this->rate);
        $criteria->compare('currency_id',$this->currency_id);
        $criteria->compare('precious_metal_id',$this->precious_metal_id);
 
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'sort'=>new EDTSort(__CLASS__, $columns),
            'pagination'=>new EDTPagination,
        ));
    }
 
    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return PreciousMetalFixing the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

View

The only relevant line is: <?php $widget->run(); ?>