Yii Framework Forum: CGridView CButtonColumn - Chanding the URL Parameter - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CGridView CButtonColumn - Chanding the URL Parameter Rate Topic: -----

#1 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 17 April 2012 - 10:09 AM

Hi,

The default Admin View shows a data grid, and each row has a VIEW / UPDATE / DELETE button.

I've recently changed my Primary Key, so for the Table concerned, its no longer 'ID' its now 'ID_LEAD'..

So if you click UPDATE, it tries to load

/index.php?r=leads/update&id=7840

But i need it to load

/index.php?r=leads/update&id_lead=7840

I've experimented with changing the CButtonColumn in the Admin View:

array
		(
			'class'=>'CButtonColumn',
			'template'=>'{view}{update}{delete}',
			'buttons'=>array
			(
				'update' => array
				(
					'url'=>'"index.php?r=leads/update&id_lead="',
				),
			),
		),


It shows the URL:

/index.php?r=leads/update&id_lead=

but I dont know how to get the value of 'id_lead' in there... also it feels abit too clunky to do this..

Can this be set sitewide - im guessing, its built into the Framework, and i obviously dont want to hack that..

:-X
0

#2 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 17 April 2012 - 10:25 AM

ok, i added this, and the URL works now, but there must be a better way...

array
  (
    'class'=>'CButtonColumn',
    'template'=>'{view}{update}{delete}',
    'buttons'=>array
    (
      'update' => array
        (
        'url'=>'$this->grid->controller->createUrl("/leads/update", array("id_lead"=>$data->primaryKey))',
	//'url'=>'"index.php?r=leads/update&id_lead="',
        ),
     ),
  ),

1

#3 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 17 April 2012 - 10:32 AM

You can use

'url'=>'CController::createUrl("/leads/update", array("id_lead"=>$data->primaryKey))'


EDIT: As the button column evals the code you really have to give it a string. So use CController to create the url. That should work
3

#4 User is offline   Sune 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 27-June 11
  • Location:Italy/ Bologna and Denmark/Copenhagen

Posted 17 April 2012 - 01:49 PM

Hi ThePaulius
If it can help you the default url are set in this line:

http://code.google.c...nColumn.php#189
         * @var string a PHP expression that is evaluated for every update button and whose result is used
         * as the URL for the update button. In this expression, the variable
         * <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;
         * and <code>$this</code> the column object.
         */
        public $updateButtonUrl='Yii::app()->controller->createUrl("update",array("id"=>$data->primaryKey))';



So, you need to configure it manually.

There is however another solution!:)
I was wondering why I never have met this problem. The reason is that I use User-friendly URLs . So that is a way to make the buttons create a valid url even if you changed the name of primaryKey. But I think the User Friendly Urls are good to use for other reasons as well, and quite easy to implement. See the link.
Good Luck
1

#5 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 18 April 2012 - 07:02 AM

Nice thank you, that worked, shorter too.

View PostHaensel, on 17 April 2012 - 10:32 AM, said:

You can use

'url'=>'CController::createUrl("/leads/update", array("id_lead"=>$data->primaryKey))'


EDIT: As the button column evals the code you really have to give it a string. So use CController to create the url. That should work

0

#6 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 18 April 2012 - 07:05 AM

Great thank you for finding were id= is created, i am very tempted to change it, but i wont because i know when i update the framework, i will forget that i changed that! lol

Also, thanks for the tip on friendly URLS, I guess that will fix the problem for sure. I do prefer friendly URLS (Hate ?id=xyz ..) I tried creating them early in the project, but reverted back becasue all urls still had index.php? which is really weird. I think I tried out something with htaccess rewrites, but gave up.

Cheers
p



View PostSune, on 17 April 2012 - 01:49 PM, said:

Hi ThePaulius
If it can help you the default url are set in this line:

http://code.google.c...nColumn.php#189
         * @var string a PHP expression that is evaluated for every update button and whose result is used
         * as the URL for the update button. In this expression, the variable
         * <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;
         * and <code>$this</code> the column object.
         */
        public $updateButtonUrl='Yii::app()->controller->createUrl("update",array("id"=>$data->primaryKey))';



So, you need to configure it manually.

There is however another solution!:)
I was wondering why I never have met this problem. The reason is that I use User-friendly URLs . So that is a way to make the buttons create a valid url even if you changed the name of primaryKey. But I think the User Friendly Urls are good to use for other reasons as well, and quite easy to implement. See the link.
Good Luck

0

#7 User is offline   nelsonyrg 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 29-January 13

Posted 30 January 2013 - 12:15 AM

View PostThePaulius, on 17 April 2012 - 10:25 AM, said:

ok, i added this, and the URL works now, but there must be a better way...

array
  (
    'class'=>'CButtonColumn',
    'template'=>'{view}{update}{delete}',
    'buttons'=>array
    (
      'update' => array
        (
        'url'=>'$this->grid->controller->createUrl("/leads/update", array("id_lead"=>$data->primaryKey))',
	//'url'=>'"index.php?r=leads/update&id_lead="',
        ),
     ),
  ),



Hi i m trying to do the same but a got the error notice "Trying to get property of non-object" even when i m takin off all the attributes of CButtonColumn.
If i take off the CButtonColumn array of the CGridView this is shown well (without the options of course).
the dataProvider is created with a CSqlDataProvider with a custimized quuery with a join with 2 tables, its possibly that result dont match with any model in the system and,this could be the error but, this can be fixed?

code the model

public static function myData($id = '' )
	{
		
		$count=Yii::app()->db->createCommand('SELECT COUNT(xx)
				FROM rr AS cs
				INNER JOIN zz AS c ON c.id = cs.rr_id
				WHERE cs.hh_id =\''.$id.'\'')->queryScalar();
		$sql='SELECT cs.id, aa, bb, cc
				FROM rr AS cs
				INNER JOIN zz AS c ON c.id = cs.rr_id
				WHERE cs.hh_id =\''.$id.'\'';
		$dataProvider=new CSqlDataProvider($sql, array(
				'totalItemCount'=>$count,
				'sort'=>array(
						'attributes'=>array(
								'id', 'aa', 'bb','cc',
						),
				),
				'pagination'=>array(
						'pageSize'=>30,
				),
		));
		return $dataProvider;
		// $dataProvider->getData() will return a list of arrays.
	}



code de view
<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'componentesSolicitud-grid',
    'dataProvider'=>$model->myData($model->id),
    //'filter'=>$model,
    'columns'=>array(
        'id',
        'aa',
        'bb',
        'cc',
        array('class'=>'CButtonColumn'),
    ),
));?>




thanks
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users