Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Display Next & Previous record buttons in a form

1
by LukeTowers, last modified on June 8th, 2024

If you would like to display "Previous" and "Next" buttons in the backend forms of your Winter CMS project you can do the following. This trick assumes you are implementing the FormController and ListController behaviors.

In your Controller, override the action method you would like to see the buttons in to include the following:

public function preview($id)
    {
        $record = $this->formFindModelObject($id);

        // Initialize the ListController
        $this->makeLists();

        // Get the position of the current record within the current list set
        $listQuery = $this->listGetWidget()->prepareQuery();
        $totalRecords = $listQuery->count();
        \DB::statement(\DB::raw('set @row_num=0'));
        $listQuery->selectRaw('@row_num:= @row_num + 1 as `record_position`');

        $previousId = 0;
        $nextId = 0;
        $currentIndex = null;
                // Note, if you have few records overall but massive sizes per record you can use
                // $listQuery->cursor() to make one DB query per record instead of loading them all at once
        foreach ($listQuery->get() as $listRecord) {
            if ($listRecord->getKey() === $record->getKey()) {
                $currentIndex = $listRecord->record_position;
                continue;
            } elseif ($currentIndex) {
                $nextId = $listRecord->getKey();
                break;
            } else {
                $previousId = $listRecord->getKey();
            }
        }

        $this->vars = array_merge($this->vars, [
            'previousUrl'  => $previousId ? Backend::url('myauthor/myplugin/mycontroller/preview/' . $previousId) : '',
            'nextUrl'      => $nextId ? Backend::url('myauthor/myplugin/mycontroller/' . $nextId) : '',
            'currentIndex' => $currentIndex,
            'totalRecords' => $totalRecords,
        ]);

        if (!$record->is_read) {
            $record->markRead();
        }

        return $this->asExtension('FormController')->preview($id);
    }

And then in your action view file (i.e. preview.htm), you can include the following:

        <p>
            Record <strong><?= e($currentIndex); ?></strong> of <strong><?= e($totalRecords); ?></strong>. <a href="<?= \Backend::url('myauthor/myplugin/mycontroller/'); ?>">View all</a>
        </p>
        <p>
            <button
                type="button"
                class="btn btn-primary wn-icon-chevron-left"
                onClick="window.location.assign('<?= e($previousUrl); ?>');"
                <?php if (!$previousUrl) : ?>disabled="disabled"<?php endif; ?>
            >Previous</button>
            <button
                type="button"
                class="btn btn-primary wn-icon-chevron-right"
                onClick="window.location.assign('<?= e($nextUrl); ?>');"
                <?php if (!$nextUrl) : ?>disabled="disabled"<?php endif; ?>
            >Next</button>
        </p>

This will take into account the currently applied column orders and list filters present on the index page for your controller.

Discussion

1 comment

0
quendi
Post on December 23rd, 2025 12:37 PM

If you need to manage a large number of records, you should consider proceeding as follows:

<?php

// Initializes the ListController
$this->makeLists();

// Prepare list query (current sorting and filters kept)
$listWidget = $this->listGetWidget();
$primaryKey = $model->getKeyName();

// Optimized function to find neighbor (previous or next)
// Use cursor() to iterate without loading all records into memory
$resolveNeighbor = function (string $direction) use ($listWidget, $primaryKey, $model) {
    $isPrev = $direction === 'previous';
    $currentKey = $model->getKey();
    $previousRecord = null;
    $foundCurrent = false;

    // Get the full query with all filters and sorting
    $listQueryFull = $listWidget->prepareQuery();

    // Iterate with cursor() to avoid loading all records into memory
    foreach ($listQueryFull->cursor() as $record) {
        $recordKey = $record->{$primaryKey} ?? null;

        if ($recordKey === null) {
            continue;
        }

        // If we find the current record
        if ($recordKey == $currentKey) {
            $foundCurrent = true;
            // For "previous", we have already stored the previous
            if ($isPrev) {
                return $previousRecord ? ($previousRecord->{$primaryKey} ?? 0) : 0;
            }
            // For "next", we continue to find the next one
            continue;
        }

        // If we found the current record and are looking for the next one
        if ($foundCurrent && !$isPrev) {
            return $recordKey;
        }

        // Stores the previous record for the "previous" case
        if (!$foundCurrent && $isPrev) {
            $previousRecord = $record;
        }
    }

    // No neighbors found
    return 0;
};

$previousId = $resolveNeighbor('previous');
$nextId = $resolveNeighbor('next');
We use cookies to measure the performance of this website. Do you want to accept these cookies?