Ahmed Sabbour
Ahmed Sabbour

Reputation: 71

Joomla pagination List Limit value

I have a third party component which uses joomla's pagination system. How can I change the values of default items per page and limit box values for the front-end only, without changing anything in the back-end?

Upvotes: 5

Views: 20250

Answers (6)

John W. Benac
John W. Benac

Reputation: 61

This solution does not require changing the core or template files. If you have control over the component code, then use the following code directly in your model:

protected function populateState($ordering = null, $direction = null)
{
// Initialise variables.
$app = JFactory::getApplication();

// List state information
$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'));

Change the line:

$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'));

to this

$limit = 50;

or whatever number you want.

If you try to set the pagination property of the JPagination object in the view to something else, as was suggested in the answer from @IberoMedia, then the JPagination's properties will be changed, as verifiable through echo var_dump($this->pagination);, but the changes in limits will not have been made prior to the records being pulled from the database. Therefore, the database will still provide the limited list. That's why you have to hack the model itself and short-circuit the call to the global list limit for the app in the model before the database is queried.

There is probably a more thoughtful way to do this. Does anyone know a way to change the global value on the fly when the page is rendered and before the database table is queried? I should think that such a thing would be safer, reusable, and more versatile.

Upvotes: 0

Paolo
Paolo

Reputation: 17

Pay attentions, to the file permission after some joomla update could change and the modification couldn't be applied to the version!

Upvotes: -2

David H.
David H.

Reputation: 373

In several of our components we have created a pagination extension to override the default options, which are hard coded and cannot be overwritten otherwise.

class myCompPagination extends JPagination {

  public function getLimitBox() {
     .. Your Custom Renderer ..
  }

}

The base class is found under /libraries/cms/pagination in Joomla 2.5 and higher.

Include your custom class and use it just like you would otherwise. $pagination = new wbCompPagination( ... );

Upvotes: 1

IberoMedia
IberoMedia

Reputation: 2304

This complements seavers answer, which in my opinion is the correct answer.

I will code an example of how I limited the number of pages that I wanted displayed in the search's results page:

From a template file root/templates/mytemplate/html/com_search/search/default.php

// number of max pages you want in pagination
$mylimit = 3; 

// to prevent showing pages with no resutls
$limit = min($mylimit, $this->pagination->pagesTotal);

// set the limit
$this->pagination->pagesStop = $limit;

RENDERS:

enter image description here

Upvotes: 1

user2346520
user2346520

Reputation: 81

For J2.5 :

You can change the value form configuration.php directly:

public $list_limit = '20'; TO public $list_limit = '4'; for example.

-> But the problem is that when you will save any parametre on the backend, you will lose your config, so you need to change it angain and againt. (so it is not the best solution)

The best solution is to go to :

/administrator/components/com_config/models/forms/application.xml

Search for the field name="list_limit"

you will find the default configuration of joomla! on line 661 :

<field
            name="list_limit"
            type="list"
            default="20"
            label="COM_CONFIG_FIELD_DEFAULT_LIST_LIMIT_LABEL"
            description="COM_CONFIG_FIELD_DEFAULT_LIST_LIMIT_DESC"
            filter="integer">
            <option
                value="5">J5</option>
            <option
                value="10">J10</option>
            <option
                value="15">J15</option>
            <option
                value="20">J20</option>
            <option
                value="25">J25</option>
            <option
                value="30">J30</option>
            <option
                value="50">J50</option>
            <option
                value="100">J100</option>
        </field>

ADD an option like :

<option value="3">J3</option>

it become :

<field
            name="list_limit"
            type="list"
            default="20"
            label="COM_CONFIG_FIELD_DEFAULT_LIST_LIMIT_LABEL"
            description="COM_CONFIG_FIELD_DEFAULT_LIST_LIMIT_DESC"
            filter="integer">
            <option
                value="3">J3</option>
            <option
                value="5">J5</option>
            <option
                value="10">J10</option>
            <option
                value="15">J15</option>
            <option
                value="20">J20</option>
            <option
                value="25">J25</option>
            <option
                value="30">J30</option>
            <option
                value="50">J50</option>
            <option
                value="100">J100</option>
        </field>

Upvotes: 8

seavers
seavers

Reputation: 571

Have a look at the docs for Joomla pagination:

http://docs.joomla.org/Using_JPagination_in_your_component

You will need to find a vaiable that looks like:

$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');

It could be called $lim, $limit or anything else that the developer thinks is appropriate. It is usually found in the model, but it can be in the view. You need to change this variable to something like:

$limit = 20;

Where 20 is the value for the limit you would like to set.

Upvotes: 3

Related Questions