Quote from: BobRay at Sep 28, 2013, 10:50 PMOK, let's leave the filtering out of the picture for now. Worry about the JS after you get everything working the way you like it.
Try this using the submit button (untested).
<form action="[[~[[*id]]]]" method="POST">
<select name="sort_method" onchange="this.form.submit()">
<option value="itemno" selected="selected">Sort by Name</option>
<option value="height">Sort by Height</option>
<option value="width">Sort by Width</option>
</select>
<input value="Submit" type="submit">
</form>
<!--?php
$output = '';
$path = MODX_CORE_PATH . 'components/aeiproducts/';
$result = $modx--->addPackage('aeiproducts', $path .
'model/', 'aei_');
if (!$result) {
return 'failed to add package';
}
/* Set default sort field */
$sortby = 'itemno';
/* Use form value if there is one */
if (isset($_POST['sort_method']) && (! empty($_POST['sort_method']))) {
$_POST['sort_method'];
}
$c = $modx->newQuery('Product');
$c->sortby($sortby, 'ASC');
/* For future use: */
// if (isset($_POST['filter']) && (!empty($_POST['filter']))) {
// $c->where(array('cat' => $_POST['filter']));
// }
$products = $modx->getCollection('Product', $c);
foreach ($products as $product) {
$fields = $product->toArray();
$output .= $modx->getChunk('ShowProds', $fields);
}
return $output;
Thank you Bob!
Using "sort_method" like you suggested did the trick. It's almost (...) working now.
In the resource:
<form action="[[~[[*id]]]]" method="POST">
<select name="sort_method" onchange="this.form.submit()">
<option value="itemno" selected="selected">Sort by Name</option>
<option value="height">Sort by Height</option>
<option value="width">Sort by Width</option>
</select>
<input type="submit" value="Submit">
</form>
[[ShowProds]]
I took out
onchange="this.form.submit()"
because it's not necessary when you use a submit button, plus I had to call the snippet un-cached like this:
to have the page change.
In the snippet I added $sortby =
if (isset($_POST['sort_method']) && (! empty($_POST['sort_method']))) {
$_POST['sort_method'];
} like this
if (isset($_POST['sort_method']) && (! empty($_POST['sort_method']))) {
$sortby = $_POST['sort_method'];
}
I'm getting a drop down on my product page and it changes when I select one of the different sort options. So far so good. Now I would like to use
onchange="this.form.submit()"
and leave out the submit button. The problem is the drop down keeps going back to Sort by Name so I can't select it. By the way, this also happens in the version with the submit button.
Does anyone know why that happens? Thanks again for all your help!