将每个pjax链接显示到每个特殊div中

时间:2015-12-22 09:58:53

标签: yii2 pjax

我使用pjax创建Ajax链接。如何将每个回调链接发送到每个div?

<?php \yii\widgets\Pjax::begin(['id'=>'pop']) ?>
<?php
  echo \yii\bootstrap\Html::a('link1',['site/index1']);
  echo \yii\bootstrap\Html::a('link2',['site/index2']);
  echo \yii\bootstrap\Html::a('link3',['site/index3']);
  echo \yii\bootstrap\Html::a('link4',['site/index4']);
  echo \yii\bootstrap\Html::a('link5',['site/index5']);
\yii\widgets\Pjax::end();
    ?>
<div id="pop1"></div>
<div id="pop2"></div>
<div id="pop3"></div>
<div id="pop4"></div>
<div id="pop5"></div>

1 个答案:

答案 0 :(得分:0)

我创建了使用ajax(链接和表单)的新扩展

<?php

namespace app\component;
use smallbearsoft\ajax\AjaxAsset;
use Yii;

 use yii\widgets\Pjax;

class myPjax  extends Pjax{

   public $ajaxLink=false;
   public $ajaxForm=false;
   public $multiAjaxLink=false;
    public $params=[];

    function init()
    {
        if($this->ajaxLink==true)
        {
            $this->Link($this->params);

        }
        if($this->ajaxForm==true)
        {
            $this->ajaxForm($this->params);

        }
        if($this->multiAjaxLink==true)
        {
            $this->multiLink($this->params);

        }
        parent::init();
    }

       public    function Link()
    {
        $id=$this->params['id'];
        $url=$this->params['url'];
        $dviId=$this->params['divId'];
 $url=Yii::$app->urlManager->createAbsoluteUrl($url);
           $js2="$('#".$id."').on('click', function() { $.ajax({url: '".$url."',type: 'POST',success : function(res){ $('#".$dviId."').html(res);}});});";


        $view = $this->getView();
        AjaxAsset::register($view);

        if ($js2 !== '') {
            $view->registerJs($js2);
        }
        return ;
    }
    public function multiLink()
    {
        foreach($this->params as $value=>$key):
        $id=$key['id'];
        $url=$key['url'];
        $dviId=$key['divId'];
 $url=Yii::$app->urlManager->createAbsoluteUrl($url);
           $js2="$('#".$id."').on('click', function() { $.ajax({url: '".$url."',type: 'POST',success : function(res){ $('#".$dviId."').html(res);}});});";


        $view = $this->getView();
        AjaxAsset::register($view);


            $view->registerJs($js2);
        endforeach;

        return ;
    }
    public  function ajaxForm()
    {

        $form_id=$this->params['id'];

        $divId=$this->params['divId'];
        $url=Yii::$app->urlManager->createAbsoluteUrl($this->params['url']);


       $js2=" $('#$form_id').submit(function(e){ $.ajax({url : '$url',type: 'POST',data : $(this).serializeArray(),success:function(res){ $('#$divId').html(res);}});e.preventDefault();e.unbind();});";


        $view = $this->getView();
        AjaxAsset::register($view);
        $view->registerJs($js2);

        return ;

    }
    static function ajaxForm2($url,$idform=null,$dviId='show',$type='POST')
    {
        $pop="$.ajax({type:'".$type."',url :' ".$url." ',data: $('#".$idform."').serialize(),success:function(res){ $('#".$dviId."').html(res);}})";
        echo $pop;

    }

    public function registerClientScript()
    {

    }

} 

和使用:

<?php myPjax::begin(['multiAjaxLink'=>true,
    'params'=>[
        ['url'=>['controller/action'],'id'=>'id-link','divId'=>'where-show'],
        ['url'=>['controller/action'],'id'=>'id-link','divId'=>'where-show'],
        ['url'=>['controller/action'],'id'=>'id-link','divId'=>'where-show'],

    ]
]) ?>