Coder Social home page Coder Social logo

zii-toggle-column's Introduction

Toggle Icon Column for CGridView

Can be used to display toggle icon in CGridView cell.

Readme

README RUS

Installation

Extract to protected/components.

Add following code to config/main.php:

[php]
'import'=>array(
	'application.components.DToggleColumn',
),

Usage example

[php]
$this->widget('zii.widgets.grid.CGridView', array(
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'date',
		'title', 
		// icon without link
		array(
			'class'=>'DToggleColumn',
			'name'=>'important',
			'onImageUrl'=>Yii::app()->request->baseUrl . '/images/important.png',
			'visible'=>$data->important',
            // remove default link
            'linkUrl'=>false;
		),
		// icon with ajax degault toggle link to toggleAction($id, $attribute) action of current controller
		array(
			'class'=>'DToggleColumn',
			// model attribute
			'name'=>'public',
			// title of column
			'header'=>'P',
			// confirmation (if needle)
			'confirmation'=>'Do you really want to change a status?',
			// filter
			'filter'=>array(1=>'Only published', 0=>'Only not published'),
			// icon alt attribute values 
			'titles'=>array(1=>'Published', 0=>'Not published'),
			// style
			'htmlOptions'=>array('style'=>'width:30px'),
		),
        // icon with custom link
		array(
			'class'=>'DToggleColumn',
			'name'=>'inhome',
			'header'=>'H',
			'filter'=>array(1=>'Only in homepage', 0=>'Only not in homepage'),
			'linkUrl'=>'Yii::app()->controller->createUrl("customToggle", array("id"=>$data->id, "attribute"=>"inhome"))',
			'htmlOptions'=>array('style'=>'width:30px'),
		),
		array(
			'class'=>'CButtonColumn',
		),
	),
));

Controller:

[php]
class PostController extends Controller
{
	// ...
 
	public function actionAdmin()
	{
		$model = new Post('search');
		$model->unsetAttributes();
		if(isset($_GET['Post']))
			$model->attributes=$_GET['Post'];
 
		if (Yii::app()->request->isAjaxRequest) {
			$this->renderPartial('_grid', array(
				'model'=>$model,
			));
		} else {
			$this->render('admin', array(
				'model'=>$model,
			));
		}
	}
	 
	public function actionToggle($id, $attribute)
	{
		if(!Yii::app()->request->isPostRequest)
			throw new CHttpException(400, 'Bad request');
	 
		if (!in_array($attribute, array('public', 'inhome')))
			throw new CHttpException(400, 'Bad request');
	 
		$model = $this->loadModel($id);
		$model->$attribute = $model->$attribute ? 0 : 1;
		$model->save();
 
		if (!Yii::app()->request->isAjaxRequest)
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	 
	// ...  
	 
	public function loadModel($id)
	{
		// ...
	}
}

zii-toggle-column's People

Contributors

elisdn avatar

Watchers

James Cloos avatar Vandro Neves dos Santos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.