前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tp3.2.3实现支持点击排序

tp3.2.3实现支持点击排序

作者头像
lestat
发布2018-04-17 17:19:58
1.1K0
发布2018-04-17 17:19:58
举报
文章被收录于专栏:lestat's bloglestat's blog

假设:

Admin/Home/Controller/BaseController.class.php是一个基础控制器

$current_params$in是两个在Admin/Home/Controller/BaseController.class.php中用来保存接收参数的属性,并且已经在构造函数中对其赋值

Admin/Tpl/Index/footer.html是布局中的公共部分

Public/Model/BaseModel.class.php是公共模型

请求中用来表示模块,控制器,操作的参数名称分别为:m,c,a

#searchForm#excelForm分别为条件搜索form和excel表单导出请求提交时用来临时保存和传递筛选条件的form

全局改动

Admin/Home/Controller/BaseController.class.php改动:

新增protected $current_params属性 _initialize方法尾部新增以下代码(用于处理传入的有效参数):

代码语言:javascript
复制
unset($this->current_params['m']);
unset($this->current_params['c']);
unset($this->current_params['a']);
$this->assign('page_url', U(CONTROLLER_NAME.'/'.ACTION_NAME,$this->current_params));

控制器尾部新增以下方法:

代码语言:javascript
复制
/*
 * todo:处理排序请求
 * @param $sort string 用来mysql排序的字符串
 * @param $column string 用来指定排序字段名称
 * @param $value int 排序值,1:顺序;2:倒序
 * @param $table string 需要排序字段在当前sql语句中表的别名
 * @return string 返回处理后的$order
 * */
protected function assembleSort($sort,$column,$value,$table){
    $order = urldecode($sort);
    $v_sort[$column] = $value == 1 ? 2 : 1;
    $v_sort['param_sort'] = $sort;
    $v_sort['param_column'] = $column;
    $v_sort['param_value'] = $v_sort[$column];
    $v_sort['param_table'] = $table;
    $this->assign('sort', $v_sort);
    return $order;
}

Admin/Tpl/Index/footer.html改动:

</body>标签前新增以下代码: html(保存当前页面并携带除排序相关参数的url):

代码语言:javascript
复制
<input type="hidden" id="page_url" value="{{$page_url}}">

js:

代码语言:javascript
复制
/*
    * todo:全局监听并处理点击排序操作
    * */
    $(function(){
        var form = $('#searchForm');
        if(typeof(form) !== 'undefined'){
            form.append("<input class='excel' type='hidden' name='sort' value='{{$sort.param_sort}}'/><input class='excel' type='hidden' name='column' value='{{$sort.param_column}}'/><input class='excel' type='hidden' name='value' value='{{$sort.param_value}}'/><input class='excel' type='hidden' name='table' value='{{$sort.param_table}}'/>");
        }
        $('th[data-sort=1]').attr('class','sortable').append('?');
        $('th[data-sort=2]').attr('class','sortable').append('?');
        $('tr').delegate('th','click',function(){
            var sort = $(this).attr('data-sort');
            var param = ``;
            if(typeof(sort) !== 'undefined'){
                var order = ``;
                var column = $(this).attr('data-column');
                var table = $(this).attr('data-table') ? $(this).attr('data-table') + '.' : '';
                if(sort == 1){
                    order = 'asc'
                }else if(sort == 2){
                    order = 'desc';
                }
                param = `${table}${column} ${order}`;
                var url = $('#page_url').val() + '&sort=' + param + '&column=' + column + '&value=' + sort + '&table=' + table;
                location.href = url;
            }
        });
    });
    /*
    * todo:处理导出excel操作
    * */
    if(typeof($('#excel')) !== 'undefined'){
        $('#excel').click(function () {
            ui.confirm('确定导出吗', function () {
                var eform = $('#excelform');
                if ($('#data').find('tr').length < 2) {
                    ui.error('没有可以导出的数据');
                    return false;
                }
                eform.html($('.excel').clone());
                eform.submit();
            });
        });
    }

Public/Model/BaseModel.class.php改动:

修改getPagegetExcel方法如下:

代码语言:javascript
复制
/**
 * todo:获取列表记录并返回分页数据
 * @param $map array 筛选条件
 * @param $order string 排序规则
 */
public function getPage($map, $order = '')
{
    $count = $this->where($map)->count();
    $page = classPage($count);
    $row['info'] = $this->where($map)->order($order)->limit($page->firstRow, $page->listRows)->select();
    $row['page'] = $page->show();
    return $row;
}

/**
 * todo:获取导出到excel的数据集合
 * @param $map array 筛选条件
 * @param $order string 排序规则
 */
public function getExcel($map,$order = '')
{
    $data = $this->where($map)->order($order)->select();
    return $data;
}

Admin/root/static/css/shop_manager.css改动:

在尾部新增(用于控制可点击th样式):

代码语言:javascript
复制
.sortable{
    cursor: pointer;
    color:orangered;
}

局部改动

controlle

在相关controller中的列表方法(通常是index)中,新增一行:

代码语言:javascript
复制
$order = $this->assembleSort($this->in['sort'], $this->in['column'], $this->in['value'], $this->in['table']);

并将之前

代码语言:javascript
复制
$data = $model->getPage($where);

改为:

代码语言:javascript
复制
$data = $model->getPage($where, $order);

在相关控制器中的导出到excel方法(通常是excel)中,做上述相同处理

view

在相关view中,修改需要排序的字段的th标签如下:

代码语言:javascript
复制
<th width="180px" data-sort="{{$sort.create_time|default=1}}" data-column="create_time" data-table="A">添加时间</th>

参数备注:

代码语言:javascript
复制
<th width="180px" data-sort="{{$sort.需要排序的字段名称|default=默认值1:当前为倒序,2:当前为顺序}}" data-column="需要排序的字段名称" data-table="如果当前列表需要通过join查询,此处为该字段所属的表别名">添加时间</th>

其它备注:

如果当前页面需要接收参数,如:配送点下面的配送元员列表,则需要在#searchForm的form中新增一个input标签:<input type="hidden" class="excel" name="point_id(参数名称)" value="(参数值)">

在所有重写了BaseModel的getPage或getExcel方法的模型中都需要做相应修改

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-09-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 全局改动
    • Admin/Home/Controller/BaseController.class.php改动:
      • Admin/Tpl/Index/footer.html改动:
        • Public/Model/BaseModel.class.php改动:
          • Admin/root/static/css/shop_manager.css改动:
          • 局部改动
            • controlle
              • view
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档