Load by structure fields limit

Hello

I have correctly followed the doc about loading pages dynamically with ajax from the tutorial and it’s working.
Now I want to load my content not by pages, but by structure field limit.

My controller, JS, and template are exactly the same from the doc, I just added a structure like this

  entree:
    label: Artistes
    type: structure
    entry: >
      {{artiste}}<br />
      {{nom}} {{prenom}}
    fields:
      artiste:
        label: Artiste
        type: text
      nom :
        label: Nom
        type: text
      prenom:
        label: Prenom
        type: text

So each pages from $projects = page('projects')->children()->visible(); contains multiple entries, and I would like to load 3 structures from all structures by pages each time we click on “load more”.
I tried $projects = page('projets')->children()->visible()->toStructure(); but it returns an error on $count = $projects->count();
Any ideas ?

If I get this right, You want your load more button to load 3 entries from all entries of all the subpages?

That would mean that you first get all entries of all projects and adapt the code accordingly. In another thread I have posted a method how to collect all structure fields of all pages. You can then take it from there.

The toStructure() method can only be used on a structure field, not on a collection of subpages, therefore your code fails.

Yes, exactly.

I already found this function on this page. I think I have quite understand the goal of it, so I have integreted it into my controller.
The thing now is that I don’t have anny error, but the page doesn’t display any structure, and when I click to “load-more”, no entries is displayed. Where am I wrong ?

function createNewStructure($pages, $field) {
    $structure = new Structure();
    $key = 0;
    foreach($pages as $p) {
        foreach($p->$field()->toStructure() as $item) {
            $structure->append($key, $item);
            $key++;
        }
    }
    return $structure;
}

$projects = page('projects')->children()->visible();
$entries = createNewStructure($projects, 'entree');
$count    = $entries->count();

if(r::ajax() && get('offset') && get('limit')) {  
    $offset = intval(get('offset'));
    $limit  = intval(get('limit'));
    $entries = $entries->offset($offset)->limit($limit);
    $more = $count > $offset + 1;
} else {
    $offset   = 0;
    $limit    = 1;
    $entries = $entries->limit($limit);
} 
return compact('offset', 'limit', 'projects', 'more', 'entries');

then in the page with this :

 <?php foreach($entries as $project): ?>
                <?php snippet('project', compact('project')) ?>
                <?php endforeach ?> 

and the snippet

<?php foreach($project->entree()->toStructure() as $field): ?>
...
<?php endforeach ?>

The snippet does not make sense. It should output a single entry item, e.g.

<li class="project"><?php echo $project->nom() ?></li>

Have you made sure to change the projects.json template as well:

<?php
$html = '';

foreach($entries as $project) {

  $html .= snippet('project', compact('project'), true);

}

$data['html'] = $html;
$data['more'] = $more;

echo json_encode($data);
1 Like

Sorry, I have not copied the correct code into my file. It works very well now.

Thank you very much !!!