yii\widgets\Pjax::registerClientScript PHP Method

registerClientScript() public method

Registers the needed JavaScript.
    public function registerClientScript()
    {
        $id = $this->options['id'];
        $this->clientOptions['push'] = $this->enablePushState;
        $this->clientOptions['replace'] = $this->enableReplaceState;
        $this->clientOptions['timeout'] = $this->timeout;
        $this->clientOptions['scrollTo'] = $this->scrollTo;
        if (!isset($this->clientOptions['container'])) {
            $this->clientOptions['container'] = "#{$id}";
        }
        $options = Json::htmlEncode($this->clientOptions);
        $js = '';
        if ($this->linkSelector !== false) {
            $linkSelector = Json::htmlEncode($this->linkSelector !== null ? $this->linkSelector : '#' . $id . ' a');
            $js .= "jQuery(document).pjax({$linkSelector}, {$options});";
        }
        if ($this->formSelector !== false) {
            $formSelector = Json::htmlEncode($this->formSelector !== null ? $this->formSelector : '#' . $id . ' form[data-pjax]');
            $submitEvent = Json::htmlEncode($this->submitEvent);
            $js .= "\njQuery(document).on({$submitEvent}, {$formSelector}, function (event) {jQuery.pjax.submit(event, {$options});});";
        }
        $view = $this->getView();
        PjaxAsset::register($view);
        if ($js !== '') {
            $view->registerJs($js);
        }
    }

Usage Example

 public function init()
 {
     if (Yii::$app->request->isPjax) {
         $id = Yii::$app->request->headers->get('X-PJAX-Container');
         if ($id) {
             $this->id = substr($id, 1);
             $this->_pjax = Pjax::begin(ArrayHelper::merge(['id' => $this->getId(), 'linkSelector' => false, 'formSelector' => false, 'enablePushState' => false, 'enableReplaceState' => false], $this->pjaxOptions));
             $this->_pjax->registerClientScript();
         }
     }
     parent::init();
 }
All Usage Examples Of yii\widgets\Pjax::registerClientScript