cakephp 2.0即时分类

时间:2014-07-10 16:10:47

标签: cakephp-2.0

所以我将搜索分页并提交给最终用户就好了。我想通过选择框给用户一个排序选项,即:

by cheapest desc
by cheapest asc
by newest desc
by newest asc

但我无法在cakephp网站上找到任何有助于此的帮助。

有人能建议吗?

2 个答案:

答案 0 :(得分:0)

您只需要创建默认的ORDER权限。然后,如果您使用Cake Views,则排序选项是任何列的默认选项。要将其默认排序为最便宜的DESC - 请使用以下代码:

$this->Paginator->settings = array(
    'order'     => array('cheapest' => 'DESC')
);
$this->set('yourData', $this->Paginator->paginate());

使订单动态,发布或获取选择框数据并将字段和顺序设置为2个变量并设置如下:

if(!empty($this->request->data['ModelName']['fieldChoosen']) {
    $field = $this->request->data['ModelName']['fieldChoosen'];
    $order = $this->request->data['ModelName']['orderChoosen'];
} else {
    $field = 'cheapest';
    $order = 'DESC'
}

$this->Paginator->settings = array(
    'order'     => array($field => $order)
);
$this->set('yourData', $this->Paginator->paginate());

答案 1 :(得分:0)

最后我编写了一个解决我的问题的jQuery例程

    $('#paginationorder').on('change', function(){
    //get new sort instructions
    var temp = $(this).val();
    var temp = temp.split('.');
    var col = temp[0];
    var ord = temp[1];

    //check that you have new instructions then get and edit url
    if(col.length >0 && ord.length >0){
        var url = window.location.href;

        //establish if sort: is in url
        if(url.toLowerCase().indexOf('sort') >=0){
            var start = url.toLowerCase().indexOf('sort');
            var end = url.toLowerCase().indexOf('?');

            //assemble new string
            var sort = url.substring(0,start)+'sort:Properties.'+col+'/direction:'+ord+url.substring(end,url.length);
            $(location).attr('href',sort);
        }else{
            var start = url.toLowerCase().indexOf('?');

            var sort = url.substring(0,start)+'/sort:Properties.'+col+'/direction:'+ord+'?'+url.substring(start+1,url.length);
            $(location).attr('href',sort);
        }
    }else{
        alert('Oops, something went wrong, we have been unable to change the sort order. please try again');
    }
});