Each model id has a corresponding static page e.g id = 1 of my table has a static page in my views as 1.php. So when i click the link button (implemented in ClistView) it respective static page should be displayed.
How can i implement this functionality?
<?php
echo CHtml::link('View Detail', array('$data->id.php'),
// i want 1.php to be displayed for $data->id =1 and 2.php for $data->id= 2
array('id'=>'mylink','class'=>'btnPrint btn btn-danger',
'target'=>'_blank',
));
?>
<?php
echo CHtml::link(
'View Detail',
$this->createUrl('site/static', array('id' => $data->id)),
array(
'id' => 'mylink',
'class' => 'btnPrint btn btn-danger',
'target' => '_blank',
)
);
And in your controller you can create an action as follows:
public function actionStatic($id)
{
$this->render($id);
}
Related
I am working on yii TbExtendedGridView. I have written code for pagination from drop down and it is working fine but problem is on page reload or on change gridview I want to reset pageSize = default page size.
Here is my code,
View Page code
//pagination dropdown start
$pageSize=Yii::app()->user->getState('pageSizeUserList',Yii::app()->params['defaultPageSize']);
//pagination dropdown end
$this->widget('booster.widgets.TbExtendedGridView', array(
'id' => 'UserList',
'type' => 'striped',
'dataProvider'=>$model->search(),
'filter' => $model,
//pagination dropdown start
'summaryText'=>'Rows per page '.
CHtml::dropDownList('pageSizeUserList', $pageSize, Yii::app()->params['pageSizeOptions'],
array('class'=>'change-pageSize',
'onchange'=>"$.fn.yiiGridView.update('UserList',{ data:{ pageSizeUserList: $(this).val() }})")).
' Displaying {start}-{end} of {count} result(s)',
//pagination dropdown end
'template' => "{summary}{items}{pager}",
'ajaxVar' => 'ajax',
'ajaxUpdate' => 'UserList',
'columns'=>array(
array(
'name' => 'user_name',
'type' => 'raw',
'value' => 'CHtml::link(CHtml::encode(($data->profile && $data->profile->name()) ? $data->profile->name() : $data->username), array("/user/user/view", "id" => $data->id))',
'filter' => CHtml::activeTextField($model, 'candidate_name', ['class'=>'form-control small']),
),
)));
Model page code
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSizeUserList',Yii::app()->params['defaultPageSize']),
),
Controller page code
//code for pagination start
public function actionAdmin($renderPartial = NULL) {
if (isset($_GET['pageSizeUserList'])) {
Yii::app()->user->setState('pageSizeUserList',(int)$_GET['pageSizeUserList']);
unset($_GET['pageSizeUserList']);
}
}
//code for pagination end
Have you tried this in controller page code:
//code for pagination start
public function actionAdmin($renderPartial = NULL) {
Yii::app()->user->setState('pageSizeUserList', Yii::app()->params['defaultPageSize']);
if (isset($_GET['pageSizeUserList'])) {
Yii::app()->user->setState('pageSizeUserList',(int)$_GET['pageSizeUserList']);
}
}
//code for pagination end
In the model paging code there's a semicolon before the last comma in line which should be removed
Alright, still new to Yii I am attempting to create a button in a column of CGridView. This button, when clicked, will take the user to the "view.php" page and display the information of the node they clicked on by passing in its ID.
I am frustrated that I cannot figure out how to simply add a link to the image that will direct my users. Here are some snippets of the code I have been working on for the past couple of days.
index.php
<?php echo CHtml::beginForm(); ?>
<?php
$pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'nodes-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'class' => 'CButtonColumn',
'template' => '{restart}',
'buttons' => array
(
'restart' => array
(
'label'=>'Restart this node',
'imageUrl'=>Yii::app()->baseUrl.'/images/refresh.png',
'options'=>array('id'=>'NB_bounce_Button'),
'url'=>array('view', 'id'=>$model->id),
)
),
),
/* 'id', */
'name',
'url',
'description',
'node_type',
'last_bounced',
//..
NodeBouncerController.php (View action)
/**
* Displays a particular model.
* #param integer $id the ID of the model to be displayed
*/
public function actionView($id) {
$this->render('view', array(
'model' => $this->loadModel($id),
));
}
The button is on the left. (Refresh green arrow)
What am I doing wrong? More specifically, am I using the buttons of CButtonColumn incorrectly? If so, how may I fix this?
My company uses: Yii v1.1.8.r3324
EDIT: [8/10/15]
It seems I may have been overcomplicating it. What I needed was to simply have an image-link that when clicked went to the view for that particular node that was clicked. Well, Gii auto-generates the particular view I needed and the code associated with it (as seen above).
My fix was to do away with the over complicated mess that I had and keep it simple. Use the template already provided for "view" and just alter it to my needs.
Like so:
/* 'id', */
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
'buttons' => array
(
'view' => array
(
'label'=>'Restart this node',
'imageUrl'=>Yii::app()->baseUrl.'/images/refresh.png',
'options'=>array
(
'id'=>'NB_bounce_Button'
)
)
),
),
'name',
'url',
'description',
'node_type',
'last_bounced',
//....
It would be nice to know how to do this manually but I needed something to simply work for now and expand on it later for work.
I think the problem is url of the button. Because you did not include controllerID in the url. You should use this:
'url' => 'Yii::app()->createUrl("nodeBouncer/view", "id"=>$model->id)';
For creating url in yii, usually you should follow this pattern:
contollerID/actionID
In the index page of my site I have floating div-block with CGridView inside.
I need to use sort, filter, paging etc. options, but only via updating this only div and not refreshing the whole page.
Data rendering as it should, i'm stuck only with updating grid contents - all <a>'s have href, that sending user to specified view.
Here's the view for grid: (it's the only content of my views.users.usersGrid.php)
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'ajaxUrl' => 'modules/subcntrl/usersGrid',
'ajaxUpdate' => 'users-grid',
'columns' => array(
array(
'name' => 'name',
'type' => 'raw',
'value' => 'CHtml::encode($data->name)'
),
array(
'class'=>'CButtonColumn',
),
),
));
It's called from views.users.users.php: <?php $this->actionUsersGrid(); ?>
Controller:
public function actionUsers() {
$this->renderPartial('users');
}
public function actionUsersGrid() {
if(!Yii::app()->request->isAjaxRequest) die('Url should be requested via ajax only');
$model = new Users();
$this->renderPartial('usersGrid',array(
'model' => $model,
));
}
Would appreciate any help
please use the third and fouth parameter in renderPartial method like this,
$this->renderPartial('users',array(),false,true);
it will solve your problem (setting the processOutput=true ) in the fourth parameter
please use the third and fouth parameter in renderPartial method like this,
$this->renderPartial('users',array(),false,true);
it will solve your problem (setting the processOutput=true ) in the fourth parameter
this is not working in my code. i am using like this in ma controler
$this->renderPartial('_searchQuote', array('model' => $model, 'month' => $month, 'user' => $user), false, $processOutput=false );
I want to pass a php variable to modal window , what i am doing is opening a modal window using this link , but i want to pass a variable to this link and get same variable in modal window , i try to to do this to append a text in some div but it return html that i am unable to get in query
echo CHtml::link(
'Set Recipe', '', array(
'class' => 'testclass',
'id' => $finalDate,
'data-toggle' => 'modal',
'data-target' => '#myModal',
'fahadVar' => $finalDate
));
and when i click this button i got modal window how to get variable set in button
Here is simple modal code of yiibooster
<div class="modal-body">
<p>One fine body...</p>
</div>
<div class="modal-footer">
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'type' => 'primary',
'label' => 'Save changes',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'Close',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
thanks in advance
You should create a Widget.
Note: I copied below from another post. It works like charm.
First Create a new widget. Let say the name is CategoryWidget. Put this widget under components directory protected/components.
class CategoryWidget extends CWidget {
public function run() {
$models = Category::model()->findAll();
$this->render('category', array(
'models'=>$models
));
}
}
Then create a view for this widget. The file name is category.php. Put it under protected/components/views
category.php
<?php if($models != null): ?>
<ul>
<?php foreach($models as $model): ?>
<li><?php echo $model->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Then call this widget from your main layout.
main.php
// your code ...
<?php $this->widget('CategoryWidget') ?>
I am using Yii CGridView. There is delete buttons in a column:
array(
'class'=>'CButtonColumn',
'template' => '{update}{delete}',
'buttons' => array(
'delete' => array(
'imageUrl' => false,
'options' => array( 'class'=>'btn btn-danger btn-mini delete-button' ),
)
),
)
After clicking delete button i see ajax request and directly after that I got js error
"URL is undefined" from jquery.ba-bbq.js # 257 line (matches = url
.match( is_fragment ? /^([^#]*)\#?(.*)$/ : /^([^#?]*)\??([^#]*)(#?.*)/ );).
Does anyone have any idea how to get rid of that error?
P.s. entry is deleted, only js error.
Ok, i found solution here :
Overwrite CGridView registerClientScript with this one:
public function registerClientScript(){
//if ajaxUrl not set, default to the current action
if(!isset($this->ajaxUrl))
$this->ajaxUrl = Yii::app()->controller->createUrl("");
//call parent function
parent::registerClientScript();
}