2015-09-23 120 views
3

我正在学习Yii2,试图使用它来制作一个项目。添加新的列表选项,动态刷新更新列表

我收到了一些表单。我想实现在旅途中添加新列表选项的功能。

当前点击“添加新选项”按钮调用弹出窗体。提交后,Yii带我到这个新选项的页面。原因如下:

public function actionCreate() 
{ 
    $model = new Request(); 

    if ($model->load(Yii::$app->request->post()) && $model->save()) { 
     return $this->redirect(['view', 'id' => $model->id]); 
    } else { 
     return $this->render('create', [ 
      'model' => $model, 
      'manager' => Stuff::find()->all(), 
      'movercompany' => MoverCompany::find()->all(), 
      'worktype' => Worktype::find()->all(), 
      'customer' => Customer::find()->all(), 
     ]); 
    } 
} 

我在submittimg后想要的只是刷新更新列表并选择新选项。

我该怎么做?

_form.php这个:

<?php 

use yii\helpers\Html; 
use yii\helpers\ArrayHelper; 
use yii\helpers\Url; 
use yii\widgets\ActiveForm; 
use kartik\select2\Select2; 
use yii\bootstrap\Modal; 

/* @var $this yii\web\View */ 
/* @var $model app\models\Request */ 
/* @var $form yii\widgets\ActiveForm */ 
?> 

<div class="request-form"> 

    <?php 
     $form = ActiveForm::begin(); 
     date_default_timezone_set("Europe/Moscow"); 
     $model->request_date = date('Y-m-d'); 
     $model->request_time = date('H:i'); 
    ?> 

    <?= $form->field($model, 'request_date')->widget(\yii\jui\DatePicker::classname(), [ 
     'language' => 'ru', 
     'dateFormat' => 'yyyy-MM-dd', 
    ]) ?> 

    <?= $form->field($model, 'request_time')->textInput(['style' => 'width: 70px;']) ?> 

    <?= $form->field($model, 'customer_id')->widget(Select2::classname(), [ 
     'data' => ArrayHelper::map($customer, 'id', 'fullInfo'), 
     'language' => 'ru', 
     'options' => ['placeholder' => 'Выбрать клиента...'], 
     'pluginOptions' => [ 
      'allowClear' => true 
     ], 
    ]);?> 

    <?= $form->field($model, 'KP_id')->textInput() ?> 


    <?= $form->field($model, 'quantity')->input('number', ['style' => 'width: 75px;']) ?> 
    <div> 

     <?= $form->field($model, 'request_type')->dropDownList(
      ArrayHelper::map($worktype, 'id', 'title'), 
      array('prompt' => 'Выберите вид работ:', 'style' => 'width: 200px;') 
     ) ?> 

     <?= Html::button('+', ['value' => Url::to('index.php?r=worktype/create'), 'class' => 'btn btn-success', 'id' => 'modalButton']) ?> 

    </div> 
    <?php 
     Modal::begin([ 
       'header' => '<h4>Виды работ</h4>', 
       'id' => 'modal', 
       'size' => 'modal-lg', 
      ]); 
     echo "<div id='modalContent'></div>"; 
     Modal::end(); 
    ?> 

    <?= $form->field($model, 'payment_type')->dropDownList(
     [ 
      '0' => 'Нал', 
      '1' => 'Безнал' 
     ], 
     ['style' => 'width: 80px;']) 
    ?> 

    <?= $form->field($model, 'address')->textarea(['rows' => 2]) ?> 

    <?= $form->field($model, 'minimal_payment')->input('number', ['style' => 'width: 100px;']) ?> 

    <?= $form->field($model, 'mover_company_id')->dropDownList(
     ArrayHelper::map($movercompany, 'id', 'name'), 
     array('prompt' => 'Выберите компанию:', 'style' => 'width: 200px;') 
    ) ?> 

    <?= $form->field($model, 'manager_id')->dropDownList(
     ArrayHelper::map($manager, 'id', 'name'), 
     array('prompt' => 'Выберите менеджера:', 'style' => 'width: 200px;') 
    ) ?> 

    <?= $form->field($model, 'workers_number')->input('number', ['style' => 'width: 100px;']) ?> 

    <?= $form->field($model, 'workhours')->input('number', ['style' => 'width: 100px;']) ?> 

    <?= $form->field($model, 'payment_additional')->input('number', ['style' => 'width: 100px;']) ?> 

    <?= $form->field($model, 'payment_car')->input('number', ['style' => 'width: 100px;']) ?> 

    <?= $form->field($model, 'payment_sum')->input('number', ['style' => 'width: 100px;']) ?> 

    <?= $form->field($model, 'status')->dropDownList(
     [ 
      '0' => 'Открыт', 
      '1' => 'Закрыт' 
     ], 
     ['style' => 'width: 200px;'] 
    ) ?> 

    <?= $form->field($model, 'comment')->textarea(['rows' => 2]) ?> 

    <div class="form-group"> 
     <?= Html::submitButton($model->isNewRecord ? 'Создать' : 'Обновить', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
    </div> 

    <?php ActiveForm::end(); ?> 

</div> 
+0

用表格显示整个代码。 –

+0

@InsaneSkulll添加了表单代码。 – Marat

回答

1

我用未来技术来实现这一点。

我需要在请求表单页面上动态添加一个选项到公司列表。

  1. 所以首先我们在公司控制器中创建新的动作。它不同于renderAjax而不是render,返回1,而不是带你到新公司页面:

    public function actionAdd() {model = new Company();

    if ($model->load(Yii::$app->request->post()) && $model->save()) { 
        echo 1; 
    } else { 
        return $this->renderAjax('create', [ 
         'model' => $model, 
        ]); 
    }; 
    

    }

  2. 添加按钮,在视图_form.php这个包含公司名单:

    地址::对( '?的index.php R =公司/加'),' class'=>'btn btn-success','id'=>'modalButton'])?>
  3. 添加弹出窗口,其中包含用于创建新公司的表单。

3.1。模态窗口_form.php这个:

use yii\bootstrap\Modal; 
use yii\widgets\Pjax; 

<?php 
    Modal::begin([ 
     'header' => '<h4>Company<h4>', 
     'id' => 'modal', 
     'size' => 'modal-lg', 
    ]); 

    echo "<div id='modalContent'></div>"; 

    Modal::end(); 
?> 

3.2。将pjax应用于公司名单:

<?php Pjax::begin(['id' => 'companyList']); ?> 
... 
<?php Pjax::end(); ?> 

3.3。编辑资产/ AppAsset.php:

public $js = [ 
    'js/main.js' 
]; 

3.4。创建文件夹和js文件web/js/main.js:

$(function(){ 
    $('#modalButton').click(function(){ 
     $('#modal').modal('show') 
      .find('#modalContent') 
      .load($(this).attr('value')); 
    }); 
}); 

3.5。添加脚本到公司_form.php这个:

<?php 

$script = <<< JS 

$('form#{$model->formName()}').on('beforeSubmit', function(e) 
{ 
    var \$form = $(this); 
    $.post(
     \$form.attr("action"), // serialize yii2 form 
     \$form.serialize() 
    ) 
     .done(function(result) { 
     if(result == 1) 
     { 
      $(document).find('#modal').modal('hide'); 
      $.pjax.reload({container:'#companyList'}); 
      $(document).on('pjax:complete', function() { 
       $('#customer-company_id option:last-child').attr('selected', true); 
      }) 
     }else 
     { 
      $(\$form).trigger("reset"); 
      $("#message").html(result.message); 
     } 
     }).fail(function() 
     { 
      console.log("server error"); 
     }); 
    return false; 
}); 

JS; 

$this->registerJs($script); 

?> 

下面是我们所拥有的:按Add按钮弹出,形式与新的选项属性。提交此表单将关闭弹出窗口,刷新公司列表并选择最后一个选项。很粗鲁,但它的作品。