Hướng dẫn tạo Pager trong Drupal 8 không cần DB_SELECT() và PAGERDEFAULT

Hướng dẫn tạo Pager trong Drupal 8 không cần DB_SELECT() và PAGERDEFAULT

Pager: no new to the dictionary of Web, plays an important role in reducing long scrolling and improvises the visual look of the site. One of the pages in my site lists statistics based details. It wasn't that appealing to go ahead with this page which had almost 100 plus items to display. At this point I had a sudden idea to split the statistics with the help of pager.

I stoked with this decision as I thought PagerDefault in Drupal can help me a hand. But later I recognized the same cannot be used in my case as it was not rendered with any query. What I had in my page was just array which was rendered by a theme_table(), to construct a pager for this proved my decision quite rugged. I leave no stones unturned to pick the best way to solve my need.

And finally I found a solution and created a pager. The below mentioned code proved worthy for a solution.

$per_page = 10;

// Initialize the pager

$current_page = pager_default_initialize(count($rows), $per_page);

// Split your list into page sized chunks

$chunks = array_chunk($rows, $per_page, TRUE);

// Show the appropriate items from the list

$output = theme('table', array('header' => $header, 'rows' => $chunks[$current_page]));

// Show the pager

$output .= theme('pager', array('quantity',count($rows)));

The Page_default_initialize() as mentioned initializes pager for a page. The first argument in my case was the total number of items that was to be paged. I broke this total items into array of size 10 using array_chunk(). And the rest is just a normal table theming.

The above function can be used in the corresponding theme function either in module level or in template level. Once done the pager must start working in your case too.

if ($type == 21) { 
  $query = new EntityFieldQuery(); 

  $entities = $query->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'product') 
    ->fieldCondition('field_razred', 'tid', array (607,608,609,610), 'IN')
    ->pager(10) // this is the bit you're missing
    ->execute(); 

  $nodes = node_load_multiple(array_keys($entities['node'])); 
  print render(node_view_multiple($nodes, 'teaser')) . theme('pager');
}
Bạn thấy bài viết này như thế nào?: 
Average: 7.1 (16 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Apple có chủ tịch mới

Apple có chủ tịch mới

Hơn 1 tháng sau ngày Steve Jobs mất, Apple đã tìm được người thay thế vị trí Chủ tịch HĐQT của ông.

 
Man City khát khao vô địch UEFA Champions League 2020

Man City khát khao vô địch UEFA Champions League 2020

Man City cùng các nhà tài trợ không ngại thao túng hợp đồng. Khi Chumillas hỏi Pearce rằng liệu họ có thể thay đổi thời gian ký hợp đồng tài trợ,

Cách tắt tính năng nhận diện khuôn mặt trên Facebook

Hướng dẫn tắt tính năng nhận diện khuôn mặt trên Facebook

Tính năng nhận diện khuôn mặt rất có giá trị đối với Facebook và cũng là một điểm thú vị đối với một bộ phận người dùng Facebook, nhưng nếu bạn không thích tính năng này bạn cũng có thể tắt đi.

Công ty diệt chuột T&C

 

Diet con trung