3

Hi I have a page in which I have added 2 blocks in both blocks I have paging,but when ever i click any page number of either a block both the page change simultaneously in Drupal7

$output .= theme('pager', array('tags' => array())); This is generating pager.

Pls help

2 Answers 2

3

You can use the element parameter:

An optional integer to distinguish between multiple pagers on one page.

e.g.

First call:

$output .= theme('pager', array('tags' => array(), 'element' => 0));

Second call:

$output .= theme('pager', array('tags' => array(), 'element' => 1));
0
2

I've implemented the multipage on a single page in Drupal 8 Note:- The $element value is the key here. It should be same for both query extend and pager element.

100% working solution. Please find the exact solution for the same below:-

public function multiplePagers() {
    $build['pager_table_0'] = $this->buildTestTable(0);
    $build['pager_pager_0'] = $this->getPagerElement(0);

    $build['pager_table_1'] = $this->buildTestTable(1);
    $build['pager_pager_1'] = $this->getPagerElement(1);

    $build['pager_table_2'] = $this->buildTestTable(2);
    $build['pager_pager_2'] = $this->getPagerElement(2);

    $build['pager_table_3'] = $this->buildTestTable(3);
    $build['pager_pager_3'] = $this->getPagerElement(3);


    //render your table and pager respectively in you template 
    return [
        '#theme' => 'your_theme_name',
        '#data' => $build
    ]
}


public static function getPagerElement($element){
    return [
        '#type' => 'container',
        '#attributes' => ['class' => ['test-pager-'.$element]],
        '#use-ajax' => TRUE,
        'pager' => [
            '#type' => 'pager',
            '#element' => $element,
        ],
    ];
}


protected function buildTestTable($element) {
    $header = [
        ['data' => 'wid'],
        ['data' => 'type'],
        ['data' => 'timestamp'],
    ];
    $query = Database::getConnection()->select('watchdog', 'd')->extend(PagerSelectExtender::class)->element($element);
    $result = $query
        ->fields('d', ['wid', 'type', 'timestamp'])
        ->limit(20)
        ->orderBy('d.wid')
        ->execute();
    $rows = [];
    foreach ($result as $row) {
        $rows[] = ['data' => (array) $row];
    }
    return [
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#empty' => $this->t("There are no watchdog records found in the db"),
    ];
}

Feel free to thank me later :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.