Add class to pagination links at CakePHP 2.2 - cakephp

I can add a class ("myclass") to the link (previous or next) which does not contain a link.(current page) I just add the class following the default ones:
echo $this->Paginator->prev('< ', array(), null, array('class' => 'prev disabled myClass'));
And it generates something like this:
<span class="prev myClass disabled ">prev</span>
But, when the previous or the next links have a link, then i the class is not added and it generates something like this:
<span class="prev">
prev
</span>
How can i add a class in this cases to this pagination links?
Thanks.

API Says the second parameter is an array for the link options so I guess you could do that:
echo $this->Paginator->prev('< ', array('class' => 'myLinkClass'), null, array('class' => 'prev disabled myClass'));

Related

cakephp two paginations on one controller and one view

I have a controller that has two paginations which is reflected in my view but anytime I want to click on page 2, the controller doesnt know which page it is clicking on and I get an error.
class AsController extends AppController
{
public function view ($id = null)
{
$data1 = $this->paginate('Post','Post.foriegn_id' => $id);
$data1 = $this->paginate('Author','Author,foreign_id' => $id);
// rest of the code goes here
}
}
I have two sections in my view page
<div class="post view"> and div class="author view"
And in each of tehse sections I have the following code:
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
Please assist me in any way to rectify this issue
Cheers
It looks like its more of a structure/design issue. You are using the same id to get posts and authors. Do you have a relationship between posts and authors you can just paginate through one of the models? From what I know the pagination built into cake is meant to work with one model per request. Especially due to how the pagination helper uses the url params. Do you have a case where you want to show page 1 of posts and page 2 of authors? Also in your view action you have your second paginate() saving to the same $data1 var which is overwriting the previous. Not sure if this is a typo.
I would suggest only using one main section of pagination per page. If the data of the authors relates to the posts I would suggest containing and displaying it in the same paginated section.

cakePHP paginator not passing passedargs

I am using cakePHP and I am trying to get the paginator component to pass the get variables, or passedargs, when you click through to different pages. I have a variety of different search input selectors which "filters" the results returned. This works on first view, but the moment I click on a different page, it shows all of the results.
I have the following setup for my paginator:
// In my controller class:
public $paginate = array('maxLimit' => 10, 'paramType' => 'querystring');
// Within my action method:
$this->paginate = array('conditions' => array(...),
order => array('Model.field ASC'),
'limit' => 20
);
// Calling the paginator:
$results = $this->paginate('Model');
$this->set(compact('results'));
In my view file:
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
EDIT:
From my understanding it's better to use the passedArgs, but I am a little unsure as to how to do this. My $this->passedArgs returns no results, so I am creating the passed parameters within my controller example. I also changed my form from Get to Post:
$this->passedArgs["searchfield"] = $_POST["value"];
It passes the passedArgs now correctly in the pagination strip, but I am unsure as to how to build the paging conditions array now. In most cases users will not select default values example, one of the filters is date from and date to, and then a search input box, if I leave the dates it will still created the argumens and not return any results so in essence my url would be something like:
http://localhost/site/controller/action/page:3/datefrom:0/dateto:0/searchFor:survey
Any assistance?
You can pass by all parameters in the view with:
$this->Paginator->options(array('url' => $this->passedArgs));
or assign the params manually:
$this->Paginator->options(array('url' => array("0", "1")));
befor echoing the paginator
See the CakePHP Cookbook for further Examples

CakePHP: variables are cleared when next/privous is clicked

I have an action from a controller that provides a result variable via $this->set('found_products', $data);. The view page products.ctp is divided into two sections:
On top, a form where a user enters a string to search $found_products is set for the view.
Below it, paginated results that is displayed only if $found_products is set. i.e. if (isset($found_products)) is true.
When if (isset($found_products)) is true, I get the first page displayed below the form with the search string already in the text box. The URL for this is myapp/controller/products.
The problem occurs when I move into the next pages. The URL becomes myapp/controller/action/products:2 and none of the variables used under myapp/controller/products exist. It looks like moving onto a new page clears all variables.. Below is the code I'm using for paging, and I have no reroute rules written for this. How do I solve this issue?
<div class="paging">
<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
<?php echo $this->Paginator->numbers();?>
<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
</div>
I've tried to work around this using $_SESSION in the action (products) and setting this for the view, but when I did this, `$this->Paginator' no longer worked.
You can use the $this->Paginator->options to preserve the passed arguments in the pagination links. Try this code
<div class="paging">
<?php $this->Paginator->options(array('url' => $this->passedArgs)); ?>
<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
<?php echo $this->Paginator->numbers();?>
<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
</div>

CakePHP Pagination with HTML Entities not working

Could anyone riddle me this.
The following code produces this-> http://www.evernote.com/shard/s29/sh/87fce2c2-c323-461b-a5ac-1ccc6d2ba3ad/32b87ce0602a33dfda59c4b9e69be54b
<?php echo $this->Paginator->prev("‹", array('escape' => false), null, array('class' => 'pagination_disabled')); ?>
<?php echo $this->Paginator->numbers(array('separator' => '')); ?>
<?php echo $this->Paginator->next("›", array('escape' => false), null, array('class' => 'pagination_disabled')); ?>
I have cleared the cache and tried replacing the prev with an entity number and with the exact same code used in next, with the same result.
edit
Heh, just looked at it in IE, same issue but the buttons are reversed, the prev button renders fine but the next button prints the reference. Odd.
I had this exact problem and it drove me nuts.
The below appears to work fine:
echo $this->Paginator->prev('« ', array('escape'=>false), '« ', array('escape'=>false, 'class' => 'disabled'));
echo $this->Paginator->numbers(array('separator'=>' '));
echo $this->Paginator->next(' »', array('escape'=>false), ' »', array('escape'=>false, 'class' => 'disabled'));
I believe this behaviour is intended, but it doesn't seem very logical to me at least - let me know how you get on.
I'm not sure on the specifics, but both next and prev have a third parameter. In your code you have null - in my code I have the » - and my version displays fine.
string $disabledTitle optional NULL Title when the link is disabled.
NB in my app; my class="disabled" hid the &raquo from view - you might not want this.
The PaginatorComponent is expecting to receive 4 inputs:
Title when button is active;
Options when is active;
Title when button is disabled;
Options when is disabled;
You can put the 3rd element to null and it will take the title from when the button is active but you still need to specify it's options. So, you just need to add the option to disable escape on disabled button options, like this:
<?php echo $this->Paginator->prev("‹", array('escape' => false), null, array('class' => 'pagination_disabled', 'escape' => false)); ?>
<?php echo $this->Paginator->numbers(array('separator' => '')); ?>
<?php echo $this->Paginator->next("›", array('escape' => false), null, array('class' => 'pagination_disabled','escape' => false)); ?>

How do I get PassedArgs to work in Pagination in CakePHP?

I have a page which returns results that are filtered on an organization_id. The URL looks like this:
nameofview/organization_id:1/page:2/
I'm using the built in Paginator controls and I pass $this->PassedArgs into it like this:
<div class="paging">
<?php echo $paginator->prev('<< '.__('previous', true), array('url' => $this->PassedArgs), null, array('class'=>'disabled'));?>
| <?php echo $paginator->numbers(array('url' => $this->passedArgs));?>
<?php echo $paginator->next(__('next', true).' >>', array('url' => $this->passedArgs), null, array('class'=>'disabled'));?>
</div>
The links look good for the "numbers" but don't work for Next and Previous. The links for both are taking me back to the same page. I think it is because it is passing the "Page" param.
Anyone have an idea how I can pass the correct args to $paginator->numbers?
I tried $this->passedArgs['organization_id'] but that returns errors.
Try this at the top of your view:
$paginator->options(array('url' => $this->passedArgs));
That way, you can drop array('url' => $this->passedArgs) from your prev/next/numbers lines, it should work just fine.

Resources