【问题标题】:Yii 2: Add prompt text field on clicking on GridViewYii 2:在点击 GridView 时添加提示文本字段
【发布时间】:2017-01-09 06:53:09
【问题描述】:

我在 GridView 中创建了 onClick 操作,它会将我重定向到控制器中的操作。

Index.php 中的代码:

},
  'rowOptions' => function ($model, $key, $index, $grid) {
      return ['id' => $model['id'], 'onclick' => 'var myid=this.id; var enteredValue = prompt("Please enter a number");  
        window.location.href = "index.php?r=patientservices%2Fpassing" + "&id=myid, " + enteredValue; '];
},

在我的控制器代码中:

public function actionPassing($id,$price)
{

    $ser= $this->findModel($id); 
    $model = new Receipts();
    $countclinic=Receipts::find()
                ->where(['patient_services_id'=>$id])
                ->count(); 

        if ($countclinic== 0) { 

         $model->patient_id=$ser->patient_id;
         $model->price=$price;  // the price that i need to pass
         $model->doctor_id=$ser->doctor_id;
         $model->patient_services_id=$ser->id;

         $model->reg_date=DATE('y-m-d h:m:s');
         $model->description='دفعة تحت الحساب';
         $model->userin =  \Yii::$app->user->identity->id ;
         $model->save();

            $models = $this->findModel($id);
            $models->state=2;
            $models->save();

JS 代码:

$this->registerJs("
$('.custom_button').on('click', function() {

    alert(id);
    var enteredValue = prompt('Please enter a number');
    if (enteredValue != null) {
        window.location.href = 'index.php?r=patientservices%2Fpassing' + '&id=, ' + enteredValue;
    }
});
");

我需要的是:
当用户单击 GridView 中的行时,会为用户显示提示文本,并且插入在提示文本中的数字与 ID 一起传递给控制器​​以在那里使用它,但 js 代码将 id 作为字符而不是数字传递

$model->price=$ser->price;

【问题讨论】:

    标签: javascript gridview yii2 yii2-advanced-app


    【解决方案1】:

    你可以这样做:

    1. 不要使用链接,而是使用# 作为链接,因为您还不想重定向用户。

    2. 用户点击按钮后,您想“召唤”一个提示。这只能通过 JavaScript/jQuery 来实现。

    3. 您也可以在 JavaScript/jQuery 代码中插入 PHP 代码,并形成一个 URL。

    例如(在 GridView 中):

    'buttons' => [
        'passing' => function () {
            return Html::a('Content', '#', [
                'class' => 'btn btn-primary custom_button',
                'title' => 'تسجيل القيمة كـ إيصال',
            ]);
        },
    ],
    

    然后在文件末尾:

    $this->registerJs("
        $('.custom_button').on('click', function() {
            var enteredValue = prompt('Please enter a number');
            if (enteredValue != null) {
                window.location.href = '".$url."' + '&id= ' + enteredValue;
            }
        });
    ");
    

    值得注意的是,我已经包含了一个类 custom_button,因为我们想知道用户单击的按钮是否正确,而不是任何。

    【讨论】:

    • 有一个小问题 .. 控制器中应包含的函数 ($id,$price) js 代码没有获取我的行的 id,我不知道如何获取它或将其添加到我的 custom_button 然后在 js 代码中的传递 id 中使用它
    • @NaeemAli 你可以通过传递$model 作为参数来获得$id$price。例如,GridView 中的数组中的'passing' => function ($model) {'data-price' => $model->price。然后在 JS 中:window.location.href = '".$url."' + '&id= ' + enteredValue + '&price=' + $(this).data('price');
    • 亲爱的@Edvin 出了点问题我使用了这个代码:'passing' => function ($model) { return Html::a('Content', '#', [ 'class' => 'btn btn-primary custom_button', 'data-price' => $model->price, 'title' => 'تسجيل القيمة كـ إيصال', 然后当我尝试它时它给了我Trying to get property of non-object
    • 不一定是$model,应该和你在GridView中使用searchModel一样。
    • 对不起Undefined variable: searchModel
    猜你喜欢
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多