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

 
Google+ đang đẩy Twitter tới đường cùng

Google+ đang đẩy Twitter tới đường cùng

Mạng xã hội này luôn bị “chê” trong việc cập nhật các tính năng mới để thu hút người sử dụng, tuy nhiên không vì thế mà Twittter không tạo ra áp lực cho các đối thủ Facebook và Google.

Drupal modules weights - để quản lý and execution order

Drupal modules weights - để quản lý and execution order

One of the good features that Drupal offers to the developer is the module execution order. Out of the box, Drupal has a mechanism to allow it to execute module's hooks in predefined order based on modules weight (order).

Barracuda Master Installer

Barracuda is a bash script to install and/or upgrade, tuned for high performance, Aegir Master Instance with all related system services. Barracuda is compatible with Octopus installer.

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

 

Diet con trung