Hello,
I’m trying to adapt this solution to my needs.
My structure looks like this:
All projects
- Projects A
-Subproject 1
-Subproject 2
- Projects B
-Subproject 1
-Subproject 2
- Projects C
-Subproject 1
-Subproject 2
In the All Projects template I would like to filter projects and sub-projects.
template: allprojects.php
<form id="filters" method="post" style="margin-top:20em;">
<select name="child" onchange="this.form.submit()">
<option selected value="">Select a child</option>
<?php foreach($child as $item): ?>
<?php if($item == "") continue ?>
<option<?php e(isset($data['child']) && $data['child'] == $item, ' selected') ?> value="<?php echo $item ?>"><?php
echo $item ?></option>
<?php endforeach ?>
</select>
<select name="grandchild" onchange="this.form.submit()">
<option selected value="">Select a grandchild</option>
<?php foreach($grandchild as $item): ?>
<?php if ($item == "") continue; ?>
<option<?php e(isset($data['grandchild']) && $data['grandchild'] == $item, ' selected') ?> value="<?php echo $item
?>"><?php echo $item ?></option>
<?php endforeach ?>
</select>
</form>
<?php if($allprojects->count()): ?>
<ul>
<?php foreach($allprojects->children()->limit(5) as $project): ?>
<li>
<h3><?= $project->parent()->title()->html() ?> <?= $project->title()->html() ?></h3>
<a href="<?= $project->url() ?>"><?= $project->parent()->title()->html() ?> - <?= $project->title()->html() ?></a>
</li>
<?php endforeach ?>
</ul>
<?php else: ?>
<p>No result :( </p>
<?php endif ?>
and the controller: allprojects.php:
<?php
return function($site, $pages, $page) {
$child = $page->children()->pluck('title', null, true);
$grandchild = $page->grandChildren()->pluck('title', null, true);
$keys = array('child', 'grandchild');
// return all children if nothing is selected
// $allprojects = $page->children()->visible();
// if there is a post request, filter the projects collection
if(r::is('POST') && $data = get()) {
$allprojects = $page->children()->visible()->filter(function($child) use($keys, $data) {
// loop through the post request
foreach($data as $key => $value) {
// only act if the value is not empty and the key is valid
if($value && in_array($key, $keys)) {
// return false if the child page's category and value don't match
if(!$match = $child->$key() == $value) {
return false;
}
}
}
// otherwise return the child page
return $child;
});
}
return compact('allprojects', 'child', 'grandchild', 'data');
};
Select field shows corrects datas, but result gives No results
Thanks for help.