小心,这里躺着龙。
我就是这样做的。首先,创建显示感兴趣内容的视图。对所有感兴趣的字段(计数、价格、标题等)添加排序不要担心它们不能很好地协同工作,您将在代码中动态地删除/更改它们。
return _mymodule_handle_sortables($view);
这将允许您快速/轻松地编辑函数的内容,而无需在每次迭代时编辑视图/保存视图/预览视图。请注意,我传递$\u GET变量。这不是绝对必要的,因为它应该在函数中可用。我这样做只是为了更容易阅读。
第一步,使用devel模块获取可排序字段的名称
function _mymodule_handle_sortables(&$view) {
dpm($view->sort);
}
function _mymodule_handle_sortables(&$view) {
switch ($_GET['sort']) {
case 'sell_price_asc':
unset($view->sort['title']);
$view->sort['sell_price']->options['order'] = 'ASC';
break;
case 'sell_price_desc':
unset($view->sort['title']);
$view->sort['sell_price']->options['order'] = 'DESC';
break;
case 'alpha_asc':
unset($view->sort['sell_price']);
$view->sort['title']->options['order'] = 'ASC';
break;
case 'alpha_desc':
unset($view->sort['sell_price']);
$view->sort['title']->options['order'] = 'DESC';
break;
}
return true;
}
在视图中添加一个PHP头并添加以下内容
<?php echo _mymodule_sortables($_GET); ?>
function _emunications_sortables($g) {
// Collect all the relevant GET parameters
$gopts = array();
foreach ($g as $k=>$v) {
if ($k == 'q') continue;
$gopts[$k] = $v;
}
$opts = http_build_query($gopts);
// Preserve the sort choice for selection
$s1 = $s2 = $s3 = $s4 = '';
switch ($gopts['sort']) {
case 'alpha_asc' : $s1 = 'selected="selected"';break;
case 'alpha_desc' : $s2 = 'selected="selected"';break;
case 'sell_price_asc' : $s3 = 'selected="selected"';break;
case 'sell_price_desc' : $s4 = 'selected="selected"';break;
}
// Unset the sort option so that it can be set in the url manually below
unset($gopts['sort']);
$opts_sort = http_build_query($gopts);
$output = "
<div class='product_index_header'>
<div class='view-selection'>
<span class='descript'>View: </span>
<a class='list' href='/products?$opts'> </a>
<span class='bar'>|</span>
<a class='grid' href='/products/grid/list?$opts'> </a>
</div>
<div class='sortable'>
<select name='droppy' class='droppy kitteh' onchange=\"window.location.href=$('select.droppy').val()\">
<option value='#'>Sort</option>
<option $s1 value='?sort=alpha_asc&$opts_sort'>a-z</option>
<option $s2 value='?sort=alpha_desc&$opts_sort'>z-a</option>
<option $s3 value='?sort=sell_price_asc&$opts_sort'>$ - $$</option>
<option $s4 value='?sort=sell_price_desc&$opts_sort'>$$ - $</option>
</select>
</div>
</div>
";
return $output;
}