I have a search form. I would like to pagination the result.
$criteria something like this:
if($_POST["tipus"] != 4){
$criteria->compare('t.tipus',$_POST["tipus"],true);
}
if($_POST["varos"] != 0){
$criteria->compare('`apartman`.`city`', $_POST["varos"], true);
}
if($_POST["ferohely"] != 0){
$criteria->compare('t.ferohely', $_POST["ferohely"], true);
}
My dataprovider:
$dataProvider= new CActiveDataProvider('UserAndApartman', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'t.id DESC',
),
'pagination'=>array(
'pageSize'=>2,
),
));}
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'kereses_eredmenyek_view',
));
So if i click on the second page, the post doesn't come again. How can i post it automatic?Can i get the $dataprovider with pagination?
You're using $_POST, the $_POST['tipus'] and other data is lost when
you're navigating to the next page, since you're not posting it to
page 2, 3 etc. You can do two things:
Store the $_POST['tipus'] in a session, so the next page 'knows' what year it has to use.
Store it in the url as a $_GET parameter, so instead of $_POST['tipus'] use $_GET['tipus']. When you navigate to page 2, the $_GET['tipus'] is available in the url on the next page.
The last one is the easiest one i think, and that's how i usually use it.
IF you use the get method, change the CActiveForm line to this:
<?php $this->beginWidget('CActiveForm', array(
'id'=>'fromid',
'method' => 'get'
)); ?>
Related
I'm using 'CListView' in my View page. I need to sort the CListView by external links like 'A-Z' or 'Z-A'. These links are present above the list.
Here is the code in my View page -
$model = new Whisky();
$dataProvider = $model->dashboard_whisky_list();
$this->widget('zii.widgets.CListView', array(
'id'=>'whisky-grid',
'dataProvider'=>$dataProvider,
'itemView'=>'_whisky_list',
'template'=>'{pager}{items}{pager}',
'ajaxUpdate'=>true,
'beforeAjaxUpdate' => 'js:function(id) {showLoader();}',
'afterAjaxUpdate' => 'js:function(id, data) {hideLoader();}',
));
My view contains 2 more CListViews.
Is it possible to sort by this way ? Also my pagination is not working by Ajax.
Click Here to see the screenshot.
Please help.
My issue is two-fold. First, I'm unable to correctly set the keyField for CArrayDataProvider, all I'm getting back is a string instead of a value. Second, I'm trying to use the keyField inside of CArrayDataProvider to set an id on the button in each row inside of CGridView. The reason I want to do this is so that I can pass the id value onward to an ajax function (if there's a better way to do this in Yii, I'm all ears). Any help would be much appreciated, thanks in advance!
I also posted this question once on Yii's forums. I normally wouldn't repost, but I have had a hard time getting answers there, as opposed to stack overflow, you guys are the best! Here's the link to my original post if anyone is interested.
Here is how I'm building the array that I'm using as my RAW data:
foreach ($items as $item) {
$tableRow = array("id"=>$item["Id"], "Organization"=>$item["Organization"], "Roles"=>$item["Roles"]);
$return_items[] = $tableRow;
}
Here is the CArrayDataProvider setup I'm using. I noticed that 'keyField' is not being given the id value, just the string 'id':
$dataProvider=new CArrayDataProvider($return_items, array(
'keyField'=>'id',
'sort'=>array(
'attributes'=>array(
'Organization',
'Roles',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
Lastly, here is the CGridView I'm trying to setup in the view. All that appears on the button is the id tag, but no value:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$authItems,
'columns'=>array(
'Organization',
'Roles',
array('name'=>'',
'type'=>'raw',
'htmlOptions'=>array('id'=>'id'),
'value'=>'CHtml::button("Edit Roles", array("data-toggle"=>"modal", "data-target"=>"#roles-modal"))'),
),
));
Try passing it via CHtml::button which you have already applied. E.g.
'value'=>'CHtml::button("Edit Roles", array(
"id"=>$data["id"],
"data-toggle"=>"modal",
"data-target"=>"#roles-modal"
))'),
I am trying to build in a "search" box on a results page in my cakephp app. The page uses the cakePHP pagination component to show and "page" results. This is perfect, but I am having difficulties to get the next part to work.
The desired outcome:
A cakephp form (post) with an input box and a couple of select boxes, including a date selector so that I can select between dates. The user should be able to populate these fields and submit
On submit, the user selection should change the cakePHP pagination conditions in the controller
In the view I want the pagination bar to keep record of the user selection, so that when I filter through different pages, it keeps the users search. I understand this can be achieved using $this->passedArgs, hence why I am using post and not get.
The code:
// Form:
<?php
echo $this->Form->create('search', array('class' => false));
echo $this->Form->input('searchFor');
echo $this->Form->input('dateFrom');
echo $this->Form->input('dateTo');
echo $this->Form->end();
?>
// Controller:
if($this->request->is("post")) {
$filters = $this->request->data["search"];
$this->passedArgs["searchFor"] = $filters["searchFor"];
$this->passedArgs["dateFrom"] = $filters["dateFrom"]." 00:00:00";
$this->passedArgs["dateTo"] = $filters["dateTo"]." 00:00:00";
// Assign search parameters:
if($this->passedArgs["searchFor"] != "") {
$conditions["Model.field LIKE"] = "%".$this->passedArgs["searchFor"]."%";
}
$conditions["Model.created >="] = $this->passedArgs["dateFrom"];
$conditions["Model.created <="] = $this->passedArgs["dateTo"];
} else {
$conditions = array("Result.status_id >=" => 12);
}
$this->paginate = array(
'conditions' => $conditions,
'order' => array('Result.created ASC'),
'limit' => 20
);
$this->set("results",$this->paginate("Model");
// The view file:
<?php
$this->Paginator->options(array('url' => $this->passedArgs));
?>
Where I am now:
The initial page loads with all of the results
When I populate the search boxes it does return my results
The problem:
I am convinced the way I am doing it is incorrect as I now need to do 2 checks, a) being if results has been posted and b) check if there is passedArgs available. I am 100% convinced this is not the right way of doing it.
Let's say I have 2 free form fields for search, say name and surname, if I leave surname blank my url would be written as below, and this does not look or appear to be correct. That means I have to assign default values to ensure the items below does not happen, which does not appear to be very dynamic.
http://localhost/site/controller/action/surname:0/name:John/date:0/
On refresh it says the page does not exist because the posted values is not available anylonger.
usually I proceed like this in the controller:
//transform POST into GET
if($this->request->is("post")) {
$url = array('action'=>'index');
$filters = array();
if(isset($this->data['searchFor']) && $this->data['searchFor']){
//maybe clean up user input here??? or urlencode??
$filters['searchFor'] = $this->data['searchFor'];
}
//redirect user to the index page including the selected filters
$this->redirect(array_merge($url,$filters));
}
$conditions = array();
//check filters on passedArgs
if(isset($this->passedArgs["searchFor"])){
$conditions["Model.field LIKE"] = "%".$this->passedArgs["searchFor"]."%";
}
//paginate as normal
$this->paginate = array(
'conditions' => $conditions,
'order' => array('Result.created ASC'),
'limit' => 20
);
The idea is to transform the POST sent by your form into GET. so you wont have problems with the paginator nor the refresh
Hope this helps
What you want can be done a lot more simple and DRY by using this search plugin.
It automates what you want more or less plus it already can do more than your code.
So I suggest you to use the plugin directly or take a look at it how it does the trick. :)
I have CListView and inside im redering a view that has a button, when clicked it opens a CJUIDialog.
But when i go through to the next page with the page controller. The CJUIDialog content loads to the page without clicking the button.
Any idea why it's coming like that?
It'll be great if anyone can help me out.
Thanks!
Ok, Yii generate the ids for lot of controls in an automatic way, so to avoid collision with events I recommend you to take out the interaction handling out of the item view in the following way:
In the page where the CListView is generated:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_post', // refers to the partial view named '_post'
'sortableAttributes'=>array(
'title',
'create_time'=>'Post Time',
),
));
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'dialog',
'options'=>array(
'title'=>'Dialog',
'autoOpen'=>false,
),
));
$this->endWidget('zii.widgets.jui.CJuiDialog');
In the item view page:
echo CHtml::htmlButton('Button',array('onclick' => '$("#dialog").dialog("open");'));
In case you need to do something with the data row (like using the id property of that data), you can create a custom javascript function that will receive the data when the button is clicked.
echo CHtml::htmlButton('Button',array('onclick' => 'myFunction('.$data->id.')'));
And the previous example then would be:
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_post', // refers to the partial view named '_post'
'sortableAttributes'=>array(
'title',
'create_time'=>'Post Time',
),
));
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'dialog',
'options'=>array(
'title'=>'Dialog',
'autoOpen'=>false,
),
));
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
<script type="text/javascript">
function myFunction(id) {
// you can put whatever you need inside the dialog
$("#dialog").html(id);
// open the dialog
$("#dialog").dialog("open");
}
</script>
I recently started to build a custom keyword search using Yii 1.1.x
The search works 100%. But as soon as I sort the columns and use pagination in the admin view the search gets lost and all results are shown. So with otherwords it's not filtering so that only the search results show. Somehow it resets it.
In my controller my code looks as follows
$builder=Messages::model()->getCommandBuilder();
//Table1 Columns
$columns1=array('0'=>'id','1'=>'to','2'=>'from','3'=>'message','4'=>'error_code','5'=>'date_send');
//Table 2 Columns
$columns2=array('0'=>'username');
//building the Keywords
$keywords = explode(' ',$_REQUEST['search']);
$count=0;
foreach($keywords as $key){
$kw[$count]=$key;
++$count;
}
$keywords=$kw;
$condition1=$builder->createSearchCondition(Messages::model()->tableName(),$columns1,$keywords,$prefix='t.');
$condition2=$builder->createSearchCondition(Users::model()->tableName(),$columns2,$keywords);
$condition = substr($condition1,0,-1) . " OR ".substr($condition2,1);
$condition = str_replace('AND','OR',$condition);
$dataProvider=new CActiveDataProvider('Messages', array(
'pagination'=>array(
'pageSize'=>self::PAGE_SIZE,
),
'criteria'=>array(
'with'=>'users',
'together'=>true,
'joinType'=>'LEFT JOIN',
'condition'=>$condition,
),
'sort'=>$sort,
));
$this->render('admin',array(
'dataProvider'=>$dataProvider,'keywords'=>implode(' ',$keywords),'sort'=>$sort
));
and my view looks like this
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
array(
'name'=>'user_id',
'value'=>'CHtml::encode(Users::model()->getReseller($data->user_id))',
'visible'=>Yii::app()->user->checkAccess('poweradministrator')
),
'to',
'from',
'message',
/*
'date_send',
*/
array(
'name'=>'error_code',
'value'=>'CHtml::encode($data->status($data->error_code))',
),
array(
'class'=>'CButtonColumn',
'template'=>'{view} {delete}',
),
),
));
I really do not know what do do anymore since I'm terribly lost, any help will be hihsly appreciated
You could set a user state for your search criteria and test for the state each time the controller loads your view.
Something along the lines of
if(isset($_REQUEST['search'])){
$keywords = explode(' ',$_REQUEST['search']);
Yii::app()->user->setState('keywords',$keywords);
}
else if(Yii::app()->user->hasState('keywords')){
$keywords=Yii::app()->user->getState('keywords');
}
The drawback here is the keywords state will be maintained for the length of the session.
that's why I dont' like these frameworks. In raw PHP I'd make it with just $link=http_build_query($_GET); and then use this link for both pagination and sorting. But you have to find a way to do the same using your framework ideology. I bet they have some example for such a commonplace task.