How can I call a view cell vith AJAX?

Hi,
I need your help!

Goal:
In a page I’m creating, I want to load a View Cell with AJAX. What is the best way to get the result?

Situation:
I have a drop-down list with some options.When I change the option, I need to:
1/ get the selected value
2/ load a view cell with the value from 1/ as a parameter.

Version:
3.2

Thanks for your help.
Antonio

Hmm, I don’t think what you’re trying to do makes sense, but I could be wrong, so if anyone else knows more please correct me.

Here’s my thinking: Cells are intended to be used as “elements with access to models” and you’re supposed to include them in your normal views. But you’re not really supposed to view them on their own.

So to actually view a cell, you need to create a normal controller method, and have its view display the cell.

That is to say, if you want to request something via ajax, there’s no way but to do that with a regular controller method (I assume you know how to add parameters to a method).
Since a controller has access to the model, there is no reason to use a cell whatsoever.

However, you could use an element to render the view data, or you could just have the controller method return json, which is usually better.

Hi,
thanks for your answer. I’ll give more information about what i’m doing so it may make more sense.

I have photos and series (with models and controllers). In my admin section, I have a list of images (/admin/images/index). I want to list them depending of the serie they belong to.
In my SELECT, I have the series list. Whent I change the value the list of all images is updated.

The list of images is displayed inside a cell. The cell is inside my page, not on itself.

If I make a request with a controller, there is no point of using a cell that would make the same request.

Antonio

In src/Controller/AppController.php :

` public function beforeFilter(Event $event)
{
if ($this->request->is(‘ajax’)) {
$this->data = array_merge($this->request->query,$this->request->data);
} elseif ($this->request->is(‘get’)) {
$this->data = $this->request->query;
} else {
$this->data = $this->request->data;
}

    parent::beforeFilter($event);
}
public function ajaxCell($cell, $method = 'display')
{
    if (!is_null($method)) {
        $cell =  $cell . '::' . $method;
    }
    $this->set('cell', $cell);
    $this->set('config', $this->data); //set in above beforeFilter();
    $this->render('/Cell/Utility/ajaxCell');
}`

Above creates method that you will call from cell with ajax- each call has to contain name of the cell and may contain it’s method’s name, for an example : http://cake/postController/ajaxCell/CellName/CellMethod - ajaxCell is name of method from appController - thus is accessible from any controller that inherits from AppController, all data submitted as post / get will be saved in $this->data and passed to the cell as $config.

In src/Cell/Utility/ajaxCell.ctp:

<?= $this->cell($cell,[$config]); ?>
Above renders cell passed to AppController.

Detecting in cell’s view if cell was requested by inline inclusion or by ajax call (pretty much standard way) :
<?php if (!$this->request->is('ajax')) : ?> content that will be displayed in case of inline inclusion <?php endif; ?>

Loading components into cells : in cell defintion add use Cake\Controller\Controller; :
namespace App\View\Cell; use Cake\View\Cell; use Cake\Controller\Controller;

than in cell’s method :

$C = new Controller(); return $C->loadComponent($componentName);