Manage pagination with cell in cakephp 4 - cakephp

With Cakephp 4 I would like to display some data. As there is too much data I would like to use pagination only I don't want to reload the whole page, just the part that displays this data. So I created a cell like this:
fileCell.php
public function display() {
$results = $paginator->paginate(
$query,
$this->request->getQueryParams(),
[
'limit' => 20
]
);
}
$paging = $paginator->getPagingParams() + (array)$this->request->getParam('paging');
$this->request = $this->request->withParam('paging', $paging);
$this->set('res', $results);
and I display the pagination in my view like this:
<div class="instances index content">
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?></p>
</div>
</div>
Except that by doing so I reload the whole page while I would like to reload only the part concerning the cell, can someone help me?

I am just a beginner at Cake, but I wonder if a finder function in your Controller could take a page parameter?
I don't have an example, but I will soon be doing something similar.

I made a standard ajax call, I create function in my controller. I create view associate corresponding to my element.
In my main view (where I will place my element), I add div :
<div id="test">
</div>
And, in my ajax response I add :
$('#test').html(response);
After that, I manage the pagination of my element normally with cakephp pagination.

Related

Use PaginatorHelper in Cells CakePhp 3

Hi I'm trying to use Paginator within a Cell the Paginator works rigth but the PaginatorHelper in the Template of the cell just doesn't work.
I'm using CakePhp 3.5
Here is my code
src/View/Cell/FinderCell.php
namespace App\View\Cell;
use Cake\View\Cell;
use Cake\Datasource\Paginator;
class FinderCell extends Cell {
public function display() {
$this->loadModel('Pokemon');
$paginador = new Paginator();
$pokemons = $paginador->paginate(
$this->Pokemon->find()
);
$this->set(compact('pokemons'));
}
}
src/Template/Cell/Finder/display.ctp
<?php
foreach ($pokemons as $pokemon) :
?>
<div class="tipo form large-2 medium-2 columns content">
<?php echo $this->Html->image($pokemon->pokemon_image) ?>
</div>
<?php endforeach; ?>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->prev('<< Anterior') ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next('Siguiente >>') ?>
</ul>
<p><?= $this->Paginator->counter() ?></p>
</div>
I get this
If you want to use the helper, then you need to populate either the request object with the pagination parameters, which is what the paginator component would normally do for you:
// ...
$this->request = $this->request->withParam(
'paging',
$paginator->getPagingParams() + (array)$this->request->getParam('paging')
);
$this->set(compact('pokemons'));
or the paginator helper, which in turn sets the parameters on the request object:
// ...
$pagingParams = $paginator->getPagingParams();
$this->set(compact('pokemons', 'pagingParams'));
$this->Paginator->options(['paging' => $pagingParams]);
See also
API > \Cake\Datasource\PaginatorInterface::getPagingParams()
API > \Cake\View\Helper\PaginatorHelper::options()

CAKEPHP 3: Set a value for the default layout. This becomes undefined when fetching content, why?

First let me say thanks in advance for your help. I am new to CakePHP framework and I am trying to learn this fast due to a project I’ve been thrown into.
I think my problem might be due to not fully understanding how the views are working. This is how I think it works. (please correct me if I am wrong): The default layout is a template. $this->Fetch() is kind of like ajax where by only the div containing the element is re-rendered/updated?
my basic html default template is:
<!DOCTYPE html>
<html>
<head>
<?= $this->Html->charset() ?>
<?= $this->fetch('meta') ?>
<?= $this->fetch('css') ?>
<?= $this->fetch('script') ?>
</head>
<body>
<nav class="top-bar expanded" data-topbar role="navigation">
<ul class="title-area large-3 medium-4 columns">
<li class="name">
<h1><?= $this->fetch('title') ?></h1>
</li>
</ul>
<div class="top-bar-section">
<?= $this->Element('Navbar/navbar'); ?>
<?= $this->fetch($navigation); ?>
</div>
</nav>
<?= $this->Flash->render() ?>
<div class="container clearfix">
<?= $this->fetch('content') ?>
</div>
</body>
</html>
in the default layout:
<div class="top-bar-section">
<?= $this->Element('Navbar/navbar'); ?>
<?= $this->fetch($navigation); ?>
</div>
I set the value of $navigation in my UsersController to:
$this->set('navigation', 'navbar2');
This calls the element (Element/Navbar/navbar.ctp)
<?php $this->start('navbar2'); ?>
<ul class="nav navbar-nav">
<li><?= $this->Html->link(__('Session history'), ['controller' => 'Sessions', 'action' => 'index']) ?></li>
</ul>
<?php $this->end(); ?>
the result is (web page view):
navbar
Which is what I wanted.
However, when I click on Session history from /users/home to /sessions this happens:
undefind var
So either two things are happening, somehow the fetch(‘content’) is causing $navigation to fade out of scope and become undefined or I’m completely wrong about $this-fetch() and what is really happening is the whole default page is reloaded fresh.
I guess my first question is, which one is correct? Ajax style fetch content without refreshing the whole page, or is it refreshed every time and $navigation is a completely new variable that has not been assigned.
In either case, how do I set the default layout values that will only change when an event occurs? In my example, navbar2 is for non-admin, while navbar1 is for admin. I only need to check this once when logging in.

Drupal 7 displaying content issue

How to hide a specific field of basic page content type? Not from admin pannel Structure>content type>basic page> manage display.. I want to hide it in my page template then showing separatly so that i can customize it?
In your template, you can use the hide function to prevent fields from being rendered.
Here's an exampled from the Garland theme's node template:
<div class="content clearfix"<?php print $content_attributes; ?>>
<?php
// We hide the comments and links now so that we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);
?>
</div>
<div class="clearfix">
<?php if (!empty($content['links'])): ?>
<div class="links"><?php print render($content['links']); ?></div>
<?php endif; ?>
<?php print render($content['comments']); ?>
</div>

Render views with actions from one controller into a view of a different controller in CakePHP

I'm trying to render a view that with actions from one view, into another controllers view.
I have a a view called stocks.ctp (using StocksController) that I want to render in home.ctp (HomeController) I thought I could just do <?php echo $this->fetch('../Stocks/stocks'); ?> in home.ctp, but that isn't working.
I've also tried putting <?PHP $this->extend('stocks'); ?> in home.ctp but that just overwrites the entire view and only renders stocks.ctp.
How can I display stocks.ctp inside home.ctp?
This is just an an example: Here is the
<?php $this->start('stocks');?>
<div>
content
</div>
<?php $this->end(); ?>
And here is just and example of the home.ctp view.
<div class="col-3">
<h1>STOCK MARKET WOOOOO</h1>
<?php echo $this->fetch('stocks'); ?>
</div>
<div class="col-3">
<h1>Opinion</h1>
<hr></hr>
</div>
How can I render this properly? I've tried a bunch of solutions that I've found here but nothing has worked yet and I've tried the using cookbook's documentation but no luck. I can't figure out what I'm doing wrong.
You should read about how to create elements in CakePHP documentation.
After create elements, you should be able to use them wherever you want in your application.
Create the element inside View/elements, ex: /View/elements/stocks.ctp
Request it in your home.ctp.
For example:
<?php
echo $this->element('stocks');
?>
The element should be rendered.
Read about it here: Elements
You should put this in your Homecontroller..
$this->render('Stocks/stocks');
In your controllers action do
$this->autoRender = false;
and at the end of your controller action do
$this->render('Stocks/stocks');

CakePHP 2: Passing Buttons in an element depending on view

Id like to pass buttons from the cakePHP 2 (note: I'm using Twitter Bootstrap 2.2.1) into an Element in app/View/Elements/Toolbar.ctp
<div class="actions">
<?php echo $this->Html->link(__('New User'), array('action' => 'add'), array('class' => 'btn btn-small',)); ?>
</div>
Above is an example of the button.
Depending on the view, I'd like to pass in different Buttons with different actions.
How do I do that? With Elements, Blocks, requestaction() or what?
The cakephp way doing would look like this
In view
<?= $this->element('toolbar', array('button' => $button_type)) ?>
in element there will be variable called $button available. Note that $button_type variable is also available in element.
That should do it

Resources