yii display the form and view in the same page - php

This is my first time working with Yii Framework.
I have a model called apartment.
Is it possible to combine the edit form and display view of the apartment in one page?
Here is the code in the view controller
public $modelName = 'Apartment';
public function actionView($id = 0) {
Yii::app()->bootstrap->plugins['tooltip'] = array(
'selector'=>' ',
'options'=>array(
'placement'=>'top',
),
);
$this->render('view', array(
'model' => $this->loadModelWith(array('windowTo', 'objType', 'city')),
'statistics' => Apartment::getApartmentVisitCount($id),
));
}

In your view file add something like -
$this->renderPartial( '_form', array( 'data' => $yourmodel ) )
_form being your edit subview.
Please post more of your code so stack overflow users can help with your specific problem.
look at this for more detail -
http://www.yiiframework.com/doc/api/1.1/CController#renderPartial-detail
and this -
http://www.yiiframework.com/forum/index.php/topic/23012-understanding-render-and-renderpartial/

Related

Two paginations not working independently in one page - CakePHP

I have one web page and in this page I need total 2 paginations for different different 2 tables.
I have tried with different pagination but it's dependent with each other.
For ex., If I select second page of first table then second table automatically changed with 2nd page.
Here is my controller code code :
$page = $this->pageForPagination('User');
$this->paginate = array(
'User' => array(
'fields' => array(
'User.*',
),
'conditions' => $userConditions,
'page' => $page,
),
);
$this->set('users', $this->paginate('User'));
$page1 = $this->pageForPagination('Game');
$this->paginate = array(
'Game' => array(
'fields' => array(
'Game.*',
),
'conditions' => $conditions,
'page' => $page1,
),
);
$this->set('games', $this->paginate('Game'));
Here is the function that is used in above code :
function pageForPagination($model) {
$page = 1;
$sameModel = isset($this->params['named']['model']) && $this->params['named']['model'] == $model;
$pageInUrl = isset($this->params['named']['page']);
if ($sameModel && $pageInUrl) {
$page = $this->params['named']['page'];
}
$this->passedArgs['page'] = $page;
return $page;
}
Here is my view code :
echo $this->element('paging', array('model' => 'User'));
echo $this->element('paging', array('model' => 'Game'));
I have referred above code from this url : http://debuggable.com/posts/how-to-have-multiple-paginated-widgets-on-the-same-page-with-cakephp:48ad241e-b018-4532-a748-0ec74834cda3
Can any one help me because I didn't get any solution still?
The solution in the link you mentioned is to override the default pagination urls and doesn't display both models at the same time. You haven't implemented the solution completely.
You have to extract the page number manually for each model like this:
function pageForPagination($model) {
if (isset($this->params['named']['pagefor'.$model])
return $this->params['named']['pagefor'.$model];
else
return 1;
}
The code below:
echo $this->element('paging', array('model' => 'User'));
echo $this->element('paging', array('model' => 'Game'));
causes the cakephp to use the default pagination view and the urls in the default pagination are the same for both models. You have to use custom pagination view and specify the model for the page link.
For the edited function above you can use a url pattern like this:
/Controller/Action/pageforUser:215/pageforGame:35
for each page link in your view.
I think best practice to use this url pattern is to override the pagination helper so that the links that are created form the following pattern:
'/Controller/Action/pagefor[Model]:215'.[other parameters]

Yii - Pass model ID into a view from CButtonColumn image

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

Maximum amount of items in one Page of Yii

I am developing this yii application and in the index i am displaying a "preview" of these items and currently it is display 8 items then it will pageinate itself. I would like make it page every 4 items and i have searched online and found that i could use the CPagination. I have followed the example in the documentation http://www.yiiframework.com/doc/api/1.1/CPagination and it is working but i don't know how to display the model i have the following code in the index.
foreach($models as $model):
endforeach;
$this->widget('CLinkPager', array(
'pages' => $pages,
));
Also the data that i want to display is Title, Content, Image and id and can i use the _view since i have everying set from there regarding css.
Also by defult the yii application display the index with a CListView is there a way where i can set the item limit there instead of using CPagination
If you want to just display 4 items on the index page, edit the index action like this:
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('{Model}',
array(
'criteria' => array(
'select' => 'title_field,content_field,image_field',
'limit' => {Limit},
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
This code will return {Limit} models from the database, if you want to sort them by any field you will need to add 'order' to the criteria array lik this:
'criteria' => array(
'select' => 'title_field,content_field,image_field',
'order' => 'download_id DESC',
'limit' => {Limit},
)
In your view, use foreach and render partial to render the _view.
foreach($models as $model):
$this->renderPartial('//{Model}/_view', $model);
endforeach;
Hope that helps.

Pagination For Search Results using Yii

I have a search form, which returns a bunch of images. Because their number can be high, I would prefer having the results displayed using pagination. However, I cannot seem to get it to work properly. I have moved everything to the view to keep things as simple as possible.
Here is my code snippet for the search and pagination:
$searchData = $model->search(); // this is the result of the search
$pages = new CPagination($searchData->itemCount);
$searchData->setPagination($pages);
$pages->applyLimit($searchData->criteria);
$pages->pageSize = 2;
Then, I display the results using a "foreach" statement:
foreach($searchData->getData() as $data)
{
// results displayed here
}
Finally, I have the CLinkPager widget:
$this->widget('CLinkPager', array(
'pages' => $pages,
'header' => 'Отиди на страница: ',
'nextPageLabel' => 'Следваща',
'prevPageLabel' => 'Предишна',
)); `
The problem is: there is not a relation between the widget and the displayed results.
For instance, if I get 4 results, the widget correctly shows that there are 2 pages. However, the results are not displayed in two pages, all are shown in one and clicking the CLinkPager links does nothing.
I have done pagination in the past successfully before, but for all the data using
model()->findAll().
How do I state the relation between the pagination widget and the search results... ?
Thank you for your time.
I think you are possibly overcomplicating the process. You could more easily use CListView widget to display your results with pagination.
Your controller may have this;
public function actionIndex(){
$model = new Model;
$this->render('index', array('model' => $model);
}
Your model needs to have a search method that returns an instance of CActiveDataProvider, for example
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('title', $this->title, true);
$criteria->order = 'sortOrder';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Then in your view file you can use this code;
$this->widget('widgets.CListView', array(
'dataProvider' => $model->search(),
'itemView' => '_list',
'template' => '{pager}{items}',
'pager' => array(
'nextPageLabel' => 'Следваща',
'prevPageLabel' => 'Предишна',
)
));
You can extend the CListView by putting this in your extensions folder, and change the page size there.
<?php
Yii::import('zii.widgets.CListView');
class CustomListView extends CListView {
public function init() {
if ($this->dataProvider !== null)
$this->dataProvider->pagination->pageSize = 30;
$this->dataProvider->pagination->nextPageLabel => 'Следваща';
$this->dataProvider->pagination->prevPageLabel => 'Предишна';
parent::init();
}
}
Then, use the CustomListView as follows:
<?php $this->widget('CustomListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
You do not need to use CLinkPager widget directly this way.

Adding a custom filter to views in Drupal 7

im using Drupal 7 and I want to add a new filter in views.
I have a custom table "clicks" with two fields; nid and clicks_left.
The filter should just contain a checkbox "Only display nodes with clicks left". So the filter should join node and clicks on nid..
I have read like thousands of pages of custom filters but can't get it to work =)
Please, could someone show me a working example so I understand?
I have come so far that the filter is displayed under filters but what do I need to add to do the join and get the checkbox? The relevant code below:
FILE clicks_views.inc:
function clicks_views_data() {
$data = array();
$data['clicks']['clicks_filter'] = array(
'group' => t('Clicks'),
'title' => t('Clicks left'),
'help' => t('Filter any Views based on clicks left'),
'filter' => array(
'field' => 'clicks_left',
'handler' => 'clicks_handler_filter',
),
);
return $data;
}
FILE clicks_handler_filter.inc:
<?php
class clicks_handler_filter extends views_handler_filter {
???
};
I know both functions are wrong ;)
Ok, I've found a solution. For anyone who needs it:
In clicks.module
function clicks_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'clicks') . '/includes'
);
}
In clicks.views.inc
function clicks_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'clicks') . '/includes', // path to view files
),
'handlers' => array(
// register our custom filter, with the class/file name and parent class
'clicks_handler_filter' => array(
'parent' => 'views_handler_filter',
)
),
);
}
function clicks_views_data() {
$data = array();
if(module_exists('clicks')) {
$data['node']['clicks'] = array(
'group' => t('Clicks'),
'title' => t('Clicks left'),
'help' => t('Filter any Views based on clicks left'),
'filter' => array(
'handler' => 'clicks_handler_filter',
),
);
}
return $data;
}
In clicks_handler_filter.inc
class clicks_handler_filter extends views_handler_filter {
function admin_summary() { }
function operator_form() { }
function query() {
$table = $this->ensure_my_table();
$join = new views_join();
$join->construct('clicks', $this->table_alias, 'nid', 'nid');
$this->query->ensure_table('clicks', $this->relationship, $join);
$this->query->add_where($this->options['group'], "clicks.clicks_left", 0, ">");
}
}
This gives me a possibility to add a filter "clicks" that if enabled hides all results that doesn't have clicks left (clicks_left > 0)
Actually, if your values in your tables clicks are numeric you don't need to create your own handler, you can use the default from Views views_handler_filter_numeric.
You can see all handlers that already exists in the Views handlers.

Categories