如何在yii2中调用控制器的按钮onclick功能?

问题描述 投票:0回答:1

在尝试了一些可能的解决方案后,我在其他问题中发现,我无法找到我的代码的问题......我想从Yii2控制器中调用一个函数,在视图中点击一个按钮。

在视图中就是这个按钮。

<div class="form-group-result"> <?php echo Html::submitButton('Delete', [ 'onclick' => ' $.ajax({ type: "POST", url: "/search/clearJson" ']);?> </div> </div>

在我的SearchController中,我有这个函数。

public function clearJson(){........}

这个函数将删除一个json文件......

对不起,我的英语不好。

php ajax yii2
1个回答
0
投票

我解决了,这就是我解决的方法。

<?php echo Html::submitButton('Delete', [ $this->context->clearJson()]);?>

0
投票

你首先使用javascript的目的是什么?你的解决方案绕过了javascript,因为你只是简单地运行一个等价的命令,如下面的命令,直接访问控制器,而不是通过javascript。

<?php echo Html::submitButton('Delete', ['clearJson']); ?>

下面是一个使用Kartiks Gridview的例子,它的tick选择在一个单独的位置访问Javascript。

从长远来看,如果你打算使用Javascript,你最好做以下工作。

按钮 -> JavascriptAppAsset文件夹 -> 主计长

在index.php中的按钮

<li class = "btn btn-danger btn-lg" onclick="js:getCopyitbybimonthly()" data-toggle="tooltip" title="<?php echo Yii::t('app','If you tick one of the previous cleans, the detail will be copied to a date roughly two months ahead of its date. Adjust the date once copied to get a more realistic date.')?>"><?php echo Yii::t('app',' + 2 month') ?></li>

scripts2.js中的Javascript

function getCopyitbybimonthly (){
var keys = $('#w0').yiiGridView('getSelectedRows'); 
$.post({ type: "GET",
         url: '/salesorderheader/copyticked/4',
    dataType: "json",
    data: {keylist: keys,          
    },
    success: $.pjax.reload({container:'#kv-unique-id-0'}) 
});

}

在SalesorderheaderController.php中的actionCopyticked($id)

public function actionCopyticked($id)
{
  if (!\Yii::$app->user->can('Update Daily Clean')) {
        throw new \yii\web\ForbiddenHttpException(Yii::t('app','You do not have permission to copy the ticked step.'));
  }
  $keylist = Yii::$app->request->get('keylist');
  foreach ($keylist as $key => $value)
  {
   //find the record of the $value checked item
   $model2 = Salesorderheader::findOne($value);
   $salesorderdetails = [];
   $salesorderdetails = $model2->salesorderdetails;
   $model = new Salesorderheader();
   $model->status = $model2->status;
   $model->statusfile = $model2->statusfile;
   $model->employee_id = $model2->employee_id;
    if ($id == 1) { $model->clean_date = date('Y-m-d');}
    if ($id == 2)
    { 
        $date = $model2->clean_date;
        $addeddate = date('Y-m-d', strtotime($date. ' + 31 days'));
        $model->clean_date = $addeddate;
    }
    if ($id == 3)
    { 
        $date = $model2->clean_date;
        $addeddate = date('Y-m-d', strtotime($date. ' + 14 days'));
        $model->clean_date = $addeddate;
    }
    if ($id == 4)
    { 
        $date = $model2->clean_date;
        $addeddate = date('Y-m-d', strtotime($date. ' + 60 days'));
        $model->clean_date = $addeddate;
    }
    if ($id == 5)
    { 
        $date = $model2->clean_date;
        $addeddate = date('Y-m-d', strtotime($date. ' + 7 days'));
        $model->clean_date = $addeddate;
    }
   $model->sub_total = 0;
   $model->tax_amt=0;
   $model->total_due=0;
   $model->save();
   Yii::$app->session['sod'] = $salesorderdetails;
   foreach ($salesorderdetails as $key => $value)
   {
       $model3= new Salesorderdetail();
       $model3->sales_order_id = $model->sales_order_id;
       $model3->cleaned = "Not Cleaned";
       $product_id = $salesorderdetails[$key]['product_id'];
       $found = Product::find()->where(['id'=>$product_id])->one();
       if ($found->frequency == "Weekly")
        {
                $date = strtotime("+7 day");
                $addeddate = date("Y-m-d" , $date);
                $model3->nextclean_date = $addeddate;
        };
        if ($found->frequency == "Monthly")
            {
               $date = strtotime("+30 day");
               $addeddate = date("Y-m-d" , $date);
               $model3->nextclean_date = $addeddate;
            };
        if ($found->frequency == "Fortnightly")
            {
               $date = strtotime("+15 day");
               $addeddate = date("Y-m-d" , $date);
               $model3->nextclean_date = $addeddate;
            };
        if ($found->frequency == "Every two months")
            {
               $date = strtotime("+60 day");
               $addeddate = date("Y-m-d" , $date);
               $model3->nextclean_date = $addeddate;
            }; 
       if ($found->frequency == "Not applicable")
            {
               $model3->nextclean_date = date("Y-m-d"); 
            };  
       $model3->productcategory_id = $salesorderdetails[$key]['productcategory_id'];
       $model3->productsubcategory_id =$salesorderdetails[$key]['productsubcategory_id'];
       $model3->product_id = $salesorderdetails[$key]['product_id'];
       $model3->unit_price = Yii::$app->formatter->asDecimal($salesorderdetails[$key]['unit_price'],2);
       $model3->paid = 0;
       $model3->save();
   } 

 };
 //return;

}

© www.soinside.com 2019 - 2024. All rights reserved.