创建文章分页

时间:2013-04-06 13:39:45

标签: silverstripe

您好我正在使用silverstripe 2.4.7而且我很难让分页工作。我在page.php中创建了一个函数来获取最新的文章

function AllNewsPosts($num=1) { 
$news = DataObject::get_one("NewsHolder"); 
return ($news) ? DataObject::get("NewsEntry", "ParentID > 0", "Date DESC", "", $num) : false; 
}

然后,当我将此功能放入控件和分页标签时,一篇文章显示但是并发文章的链接不起作用 - 基本上分页不起作用,我不知道如何解决它

<% if AllNewsPosts %>
        <% control AllNewsPosts %>
          <div class="event">
           <h2>$MenuTitle |<span class="date"> $Date.Time $Date.Long</span></h2>
           <p>$Content.FirstParagraph</p>
            <a href="$Link">See more about this event</a>
          </div>
        <% end_control %> 
        <% else %>
        <div class="no-entry">'There are no entries'</div>
        <% end_if %>

        <% if AllNewsPosts.MoreThanOnePage %>
  <div id="PageNumbers">
    <p>
      <% if AllNewsPosts.NotFirstPage %>
        <a class="prev" href="$AllNewsPosts.PrevLink" title="View the previous page"><span class="yellow-background">Prev</span></a>
      <% end_if %>

      <span>
          <% control AllNewsPosts.PaginationSummary(0) %>
          <% if CurrentBool %>
            <span class="current">$PageNum</span>
          <% else %>
            <% if Link %>
              <a href="$Link" class="numbers" title="View page number $PageNum">$PageNum</a>
            <% else %>
              &hellip;
            <% end_if %>
          <% end_if %>
        <% end_control %>
      </span>

      <% if AllNewsPosts.NotLastPage %>
        <a class="next" href="$AllNewsPosts.NextLink" title="View the next page"><span class="yellow-background">Next</span></a>
      <% end_if %>
    </p>
  </div>
<% end_if %>

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

注意:以下答案适用于Silverstripe 2.4。这不应该用于Silverstripe 3.0+站点。从3.0开始,PaginatedList对象生成pagination much easier

您没有对查询中要检索的条目数或从哪里开始设置限制。

以下教程将介绍如何将分页应用于一组数据对象,与您尝试完全一样: http://www.ssbits.com/tutorials/2010/paginating-a-filtered-dataobjectset/

尝试更改您的功能以包含限制并根据分页需要启动:

<强> PHP

function AllNewsPosts() {
    if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) 
    {
        $_GET['start'] = 0;
    }

    $SQL_start = (int)$_GET['start'];

    $newsEntries = DataObject::get('NewsEntry', '', 'Date DESC');
    $doSet = new DataObjectSet();
    foreach ($newsEntries as $newsEntry) {
        if ($newsEntry->canView()) {
            $doSet->push($newsEntry);
        }
    }
    $doSet->setPageLimits($SQL_start, 10, $doSet->Count());
    return $doSet;
}

请注意,上面每页会显示10个项目。您可以将此更改为每页需要的内容。