Get 'source' data from the database - for TbEditableColumn or EditableColumn ('type'=>'select' / dropdownlist)

You are viewing revision #6 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#7) »

If you want to edit data directly in the gridview in a 'dropdownlist' style, then you could use the 'type' => 'select' option of TbEditableColumn (YiiBooster) or EditableColumn (x-editable for Yii). This wiki shows how to get data from the db for the 'dropdownlist', by converting AR model data into an array that is passed in json format.

Database:

table1 has records of events in the country.

table1 has a foreign-key (table1_fk) to table2_id.

table2 is a list of all the towns in the country.

table2 attributes: table2_id, table2_town.

We want to update the table1_fk in the gridview by using a dropdownlist with the names of the towns.

In the view/form where you CRUD the table1 records, you have a GridView where you update table1_fk in one of the columns:

The easy way is this:

$criteria = new CDbCriteria;
$criteria->compare(bla bla bla);
...
array(
	'class' => 'bootstrap.widgets.TbEditableColumn',
	'name' => 'table1_fk',
	'editable' => array(
		'type' => 'select',
		'url' => $this->createUrl('update'),
		'source'=>editable::source(model_table2::model()->findAll($criteria),
			'table2_id', 'table2_town'),
	)
),

Note that in the above example, I am using TbEditableColumn from Yii Booster AND I am also using x-editable to be able to use 'source'=>editable::... I do this because I can't find a TbEditable.php file in the Booster/Bootstrap extension. Maybe TbEditableField.php could do the job.

If you want to work through the controller to maybe perform more tasks, check out example 4. Select on x-editable widgets.

If you want control of the data selection process in the model or if you don't want to use x-editable as well, then you can do this:

array(
	'class' => 'bootstrap.widgets.TbEditableColumn',
	'name' => 'table1_fk',
	'editable' => array(
		'type' => 'select',
		'url' => $this->createUrl('update'),
		'source' => $this->createUrl('getvalues'),
	)
),

In the Controller:

public function actionUpdate()
{
	Yii::import('bootstrap.widgets.TbEditableSaver');
	$es = new TbEditableSaver('model_table1');
	$es->update();
}

public function actionGetvalues()
{
	$model = new model_table2;
	$data = $model->getData();
	echo json_encode($data);
}

In model_table2:

public function getData()
{
	/* This function must return the data in exactly
	this format - using the same keys:
	
	$data = array(
		array('value' => 1, 'text' => 'Cape Town'),
		array('value' => 2, 'text' => 'Still Bay'),
		array('value' => 3, 'text' => 'Johannesburg'),
		array('value' => 4, 'text' => 'Port Elisabeth'),
	); */
	
	/* Get the towns */
	$criteria = new CDbCriteria;
	$criteria->compare(bla bla bla);
	$models = $this->findAll($criteria);

	/* Extract only the attributes that you need from the models
	   into a new array */
	$data = array_map(
		create_function('$m', 
			'return $m->getAttributes(array(\'table2_id\',\'table2_town\'));'), 
			$models);

	/* Replace the keys in the new array with 'value' and 'text' */
	foreach($data as $k => $v )
	{
	 	$data[$k]['value'] = $data[$k]['table2_id'];
		unset($data[$k]['table2_id']);
		
		$data[$k]['text'] = $data[$k]['table2_town'];
		unset($data[$k]['table2_town']);
	}
	return($data);
}
3 0
2 followers
Viewed: 23 647 times
Version: Unknown (update)
Category: How-tos
Written by: Gerhard Liebenberg
Last updated by: Gerhard Liebenberg
Created on: Jul 12, 2013
Last updated: 10 years ago
Update Article

Revisions

View all history