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.7 (12 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

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.
Image CAPTCHA
Enter the characters shown in the image.

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

 
Giới thiệu bạn đọc Drupal 8  và Composer

Giới thiệu bạn đọc Drupal 8 và Composer

You will also notice in composer.json under the “require” section is where drupal/token and drupal/devel are added

Tạo chiến dịch email cho hoạt động bán hàng trên Drupal Commerce

Tạo chiến dịch email cho hoạt động bán hàng trên Drupal Commerce

Having the ability to send a targeted email to all purchasers of ‘product X’ can be priceless.

Thi lấy chứng chỉ: Acquia Certified Developer Exam Blueprint

Thi lấy chứng chỉ: Acquia Certified Developer Exam Blueprint

The Acquia Certified Developer exam is intended for Acquia customers, partners, and employees who perform a Developer role. This exam validates a candidate’s ability to:

Tomdesgin.vn

 

Drupal Services