ecshop后台分页浅析

既然是分页,道理都是一样的,不过ecshop前台分页的函数和后台分页的函数不同,后台分页函数为page_and_size(),在admin include
  既然是分页,道理都是一样的,不过ecshop前台分页的函数和后台分页的函数不同,后台分页函数为page_and_size(),在admin/includes/lib_main.php里。
都是用ajax来进行分页所需参数的传递,用到后台封装的js函数库listtable.js,下面就来说说ajax分页实现的步骤:
1,html页面:引入js{insert_scripts files="../js/utils.js,listtable.js"}
     将分页所需的参数用listTable 对象进行属性赋值,如下:
   <script type="text/javascript" language="JavaScript">
listTable.recordCount = {$record_count};
listTable.pageCount = {$page_count};
{foreach from=$filter item=item key=key}
listTable.filter.{$key} = '{$item}';
{/foreach}
{literal}
 
</script> 
2,php页面:分页的每个要素实现,给模板页面所需的参数赋值
   $card_list = card_list();  //求得目标数组的函数
    $smarty->assign('card_list',   $card_list['list']);
    $smarty->assign('filter',       $card_list['filter']);
    $smarty->assign('record_count', $card_list['record_count']);
    $smarty->assign('page_count',   $card_list['page_count']);
    $smarty->assign('full_page',        1);
   //函数如下,仅作为参考
  function card_list()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();
 
        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('card_number');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
 
        $filter = page_and_size($filter);
 
        /* 查询记录 */
        $sql = "SELECT *"."FROM ". $GLOBALS['ecs']->table('card_number') .
               ' WHERE card_id > 0 order by card_id ASC LIMIT ' . $filter['start'] . ',' . $filter['page_size'];
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
 
    $all = $GLOBALS['db']->getAll($sql);
 
    
 
    return array('list' => $all, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

 3,到这里,分页模板page.html里的函数就能进行参数传递,这些函数大致有
{literal}<a href="javascript:listTable.gotoPageFirst()">{/literal}{$lang.page_first}</a>
          {literal}<a href="javascript:listTable.gotoPagePrev()">{/literal}{$lang.page_prev}</a>
          {literal}<a href="javascript:listTable.gotoPageNext()">{/literal}{$lang.page_next}</a>
          {literal}<a href="javascript:listTable.gotoPageLast()">{/literal}{$lang.page_last}</a>
打开模板一看便知
4,最后一步
 上面的js函数实际上是这样的链接,
act=query&record_count=21&page=1&page_count=2&start=15
所以我们还需要在php页面有一个判断,和输出目标数组一样,只不过这是点击分页后的数组输出。 
  
elseif ($_REQUEST['act'] == 'query')
{
    $card_list = card_list();
 
    $smarty->assign('card_list',   $card_list['list']);
    $smarty->assign('filter',       $card_list['filter']);
    $smarty->assign('record_count', $card_list['record_count']);
    $smarty->assign('page_count',   $card_list['page_count']);
 
    make_json_result($smarty->fetch('card_list.htm'), '',
        array('filter' => $card_list['filter'], 'page_count' => $card_list['page_count']));
}

到此为止,分页的效果就实现了,这个觉得比前台分页要简单的多,原因是用js来进行参数传递。 
标签: