# Pjax

> Pjax is a widget integrating the [pjax](https://github.com/yiisoft/jquery-pjax) jQuery plugin.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [widgets](/docs/api/yii/widgets/index.md) › Pjax

| class | `yii\widgets\Pjax` |
| --- | --- |
| Extends | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\ViewContextInterface`](/docs/api/yii/base/viewcontextinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/widgets/Pjax.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/widgets/Pjax.php#L46) |

Pjax is a widget integrating the [pjax](https://github.com/yiisoft/jquery-pjax) jQuery plugin.

Pjax only deals with the content enclosed between its [`begin()`](/docs/api/yii/base/widget/index.md#method-begin) and [`end()`](/docs/api/yii/base/widget/index.md#method-end) calls, called the *body content* of the widget.
By default, any link click or form submission (for those forms with `data-pjax` attribute) within the body content
will trigger an AJAX request. In responding to the AJAX request, Pjax will send the updated body content (based
on the AJAX request) to the client which will replace the old content with the new one. The browser's URL will then
be updated using pushState. The whole process requires no reloading of the layout or resources (js, css).

You may configure [`$linkSelector`](/docs/api/yii/widgets/pjax/index.md#property-linkSelector) to specify which links should trigger pjax, and configure [`$formSelector`](/docs/api/yii/widgets/pjax/index.md#property-formSelector)
to specify which form submission may trigger pjax.

You may disable pjax for a specific link inside the container by adding `data-pjax="0"` attribute to this link.

The following example shows how to use Pjax with the [`GridView`](/docs/api/yii/grid/gridview/index.md) widget so that the grid pagination,
sorting and filtering can be done via pjax:

```
use yii\widgets\Pjax;

Pjax::begin();
echo GridView::widget([...]);
Pjax::end();
```

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-autoIdPrefix"></span>`$autoIdPrefix` static | `string` | Default: `'p'`. | `Pjax` |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$clientOptions`](#property-clientOptions) | `array` | Additional options to be passed to the pjax JS plugin. Default: `null`. | `Pjax` |
| <span id="property-enablePushState"></span>`$enablePushState` | `bool` | Whether to enable push state. Default: `true`. | `Pjax` |
| <span id="property-enableReplaceState"></span>`$enableReplaceState` | `bool` | Whether to enable replace state. Default: `false`. | `Pjax` |
| [`$formSelector`](#property-formSelector) | `string\|null\|false` | The jQuery selector of the forms whose submissions should trigger pjax requests. Default: `null`. | `Pjax` |
| <span id="property-id"></span>[`$id`](/docs/api/yii/base/widget/index.md#property-id) | `string` | Id of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`$linkSelector`](#property-linkSelector) | `string\|null\|false` | The jQuery selector of the links that should trigger pjax requests. Default: `null`. | `Pjax` |
| [`$options`](#property-options) | `array` | The HTML attributes for the widget container tag. Default: `[]`. | `Pjax` |
| [`$scrollTo`](#property-scrollTo) | `bool\|int` | How to scroll the page when pjax response is received. Default: `false`. | `Pjax` |
| [`$submitEvent`](#property-submitEvent) | `string` | The jQuery event that will trigger form handler. Default: `'submit'`. Available since 2.0.9. | `Pjax` |
| [`$timeout`](#property-timeout) | `int` | Pjax timeout setting (in milliseconds). Default: `1000`. | `Pjax` |
| <span id="property-view"></span>[`$view`](/docs/api/yii/base/widget/index.md#property-view) | [`yii\web\View`](/docs/api/yii/web/view/index.md) | The view object that can be used to render views or view files. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-viewPath"></span>[`$viewPath`](/docs/api/yii/base/widget/index.md#property-viewPath) read-only | `string` | The directory containing the view files for this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-afterRun"></span>[`afterRun()`](/docs/api/yii/base/widget/index.md#method-afterRun) | This method is invoked right after a widget is executed. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-beforeRun"></span>[`beforeRun()`](/docs/api/yii/base/widget/index.md#method-beforeRun) | This method is invoked right before the widget is executed. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-begin"></span>[`begin()`](/docs/api/yii/base/widget/index.md#method-begin) static | Begins a widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-end"></span>[`end()`](/docs/api/yii/base/widget/index.md#method-end) static | Ends a widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getId"></span>[`getId()`](/docs/api/yii/base/widget/index.md#method-getId) | Returns the ID of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-getView"></span>[`getView()`](/docs/api/yii/base/widget/index.md#method-getView) | Returns the view object that can be used to render views or view files. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-getViewPath"></span>[`getViewPath()`](/docs/api/yii/base/widget/index.md#method-getViewPath) | Returns the directory containing the view files for this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the object. | `Pjax` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`registerClientScript()`](#method-registerClientScript) | Registers the needed JavaScript. | `Pjax` |
| <span id="method-render"></span>[`render()`](/docs/api/yii/base/widget/index.md#method-render) | Renders a view. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-renderFile"></span>[`renderFile()`](/docs/api/yii/base/widget/index.md#method-renderFile) | Renders a view file. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`run()`](#method-run) | Executes the widget. | `Pjax` |
| <span id="method-setId"></span>[`setId()`](/docs/api/yii/base/widget/index.md#method-setId) | Sets the ID of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-setView"></span>[`setView()`](/docs/api/yii/base/widget/index.md#method-setView) | Sets the view object to be used by this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-widget"></span>[`widget()`](/docs/api/yii/base/widget/index.md#method-widget) static | Creates a widget instance and runs it. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`requiresPjax()`](#method-requiresPjax) protected |  | `Pjax` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_INIT"></span>[`EVENT_INIT`](/docs/api/yii/base/widget/index.md#event-EVENT_INIT) | [`yii\base\Event`](/docs/api/yii/base/event/index.md) | An event that is triggered when the widget is initialized via [`init()`](/docs/api/yii/base/widget/index.md#method-init). Event name: `'init'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="event-EVENT_BEFORE_RUN"></span>[`EVENT_BEFORE_RUN`](/docs/api/yii/base/widget/index.md#event-EVENT_BEFORE_RUN) | [`yii\base\WidgetEvent`](/docs/api/yii/base/widgetevent/index.md) | An event raised right before executing a widget. Event name: `'beforeRun'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="event-EVENT_AFTER_RUN"></span>[`EVENT_AFTER_RUN`](/docs/api/yii/base/widget/index.md#event-EVENT_AFTER_RUN) | [`yii\base\WidgetEvent`](/docs/api/yii/base/widgetevent/index.md) | An event raised right after executing a widget. Event name: `'afterRun'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |

## Property details

### $clientOptions <span id="property-clientOptions"></span>

```php
public array $clientOptions = null
```

Additional options to be passed to the pjax JS plugin.

Please refer to the
[pjax project page](https://github.com/yiisoft/jquery-pjax) for available options.

### $formSelector <span id="property-formSelector"></span>

```php
public string|null|false $formSelector = null
```

The jQuery selector of the forms whose submissions should trigger pjax requests.

If not set, all forms with `data-pjax` attribute within the enclosed content of Pjax will trigger pjax requests.
If set to false, no code will be registered to handle forms.
Note that if the response to the pjax request is a full page, a normal request will be sent again.

### $linkSelector <span id="property-linkSelector"></span>

```php
public string|null|false $linkSelector = null
```

The jQuery selector of the links that should trigger pjax requests.

If not set, all links within the enclosed content of Pjax will trigger pjax requests.
If set to false, no code will be registered to handle links.
Note that if the response to the pjax request is a full page, a normal request will be sent again.

### $options <span id="property-options"></span>

```php
public array $options = []
```

The HTML attributes for the widget container tag.

The following special options are recognized:

- `tag`: string, the tag name for the container. Defaults to `div`
  This option is available since version 2.0.7.
  See also [`Html::tag()`](/docs/api/yii/helpers/basehtml/index.md#method-tag).

### $scrollTo <span id="property-scrollTo"></span>

```php
public bool|int $scrollTo = false
```

How to scroll the page when pjax response is received.

If false, no page scroll will be made.
Use a number if you want to scroll to a particular place.

### $submitEvent <span id="property-submitEvent"></span>

```php
public string $submitEvent = 'submit'
```

The jQuery event that will trigger form handler.

Defaults to "submit".

**Since**

2.0.9

### $timeout <span id="property-timeout"></span>

```php
public int $timeout = 1000
```

Pjax timeout setting (in milliseconds).

This timeout is used when making AJAX requests.
Use a bigger number if your server is slow. If the server does not respond within the timeout,
a full page load will be triggered.

## Method details

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the object.

This method is called at the end of the constructor.
The default implementation will trigger an [`EVENT_INIT`](/docs/api/yii/base/widget/index.md#event-EVENT_INIT) event.

**Returns**

`mixed`

### registerClientScript() <span id="method-registerClientScript"></span>

```php
public function registerClientScript(): mixed
```

Registers the needed JavaScript.

**Returns**

`mixed`

### run() <span id="method-run"></span>

```php
public function run(): string|void
```

Executes the widget.

**Returns**

`string|void`: The rendering result may be directly "echoed" or returned as a string

### requiresPjax() <span id="method-requiresPjax"></span>

```php
protected function requiresPjax(): bool
```

**Returns**

`bool`: Whether the current request requires pjax response from this widget