简体   繁体   English

Yii2单击按钮时获取每​​个GridView行的值(单击按钮时获取多个值)

[英]Yii2 Get Value of Each GridView Row on Button Click (Get Multiple Values on One Button Click)

I have a GridView and in my ActionColumn , there is a "View Payslip" button on each row. 我有一个GridView ,在我的ActionColumn ,每行上都有一个“查看工资单”按钮。 "Create Payslip" will be seen instead of "View Payslip" if the user has not created any payslip yet. 如果用户尚未创建任何工资单,则将看到“创建工资单”而不是“查看工资单”。 I also have a button outside this GridView named "Approve Payslips". 在此GridView之外,还有一个名为“批准薪资单”的按钮。 This button is disabled by default and will only be enabled if all buttons indicate "View Payslip". 默认情况下,此按钮是禁用的,并且仅在所有按钮均指示“查看工资单”时才启用。 在此处输入图片说明

PHP 的PHP
This is code for the "Approve Payslips" button: 这是“批准工资单”按钮的代码:

<?php echo Html::button('Approve Payslips', ['value' => 'approve-w', 'class' => 'btn btn-warning btn-responsive approve-w', 'style' => 'display:none', 'onclick'=>'approveWeekly(value)']); ?>

JavaScript 的JavaScript
I tried this but it doesn't work since it passes an ID and I have multiple values to pass: 我尝试了此操作,但由于它传递了ID并且有多个值要传递,因此它不起作用:

function approveWeekly(id){
    $.ajax({
        url: 'index.php?r=periods/approveweekly',
        dataType: 'json',
        method: 'GET',
        data: {id : id},
        success: function (data, textStatus, jqXHR) {
           $.pjax.reload({container:'#w_gridview'});
        },
        error: function (jqXHR, textStatus, errorThrown) { 
            alert("error");
        }
    });
}

Also tried this one but it's not working again because I just got this code from my "Email Selected Payslips" button (check the photo above): 也尝试过此操作,但由于我刚刚从“通过电子邮件发送所选工资单”按钮(请查看上面的照片)获得了此代码,因此无法再次使用:

function approveBiMonthly(){       
    var keylist = $('#b_gridview').yiiGridView('getSelectedRows');
    //alert(keylist);
    keylist = '\''+keylist+'\'';
    $.ajax({
        url: 'index.php?r=periods/approvebimonthly', // your controller action
        dataType: 'json',
        method: 'GET',
        data: {keylist: keylist},
        success: function (data, textStatus, jqXHR) {
        //alert(keylist);
        },
        error: function (jqXHR, textStatus, errorThrown) { 
            alert('error');
        }
    });
}

Anyone has any idea or knows how to implement this kind of feature? 任何人都有任何想法或知道如何实现这种功能? I don't know what to put in my jQuery code anymore. 我不知道要在我的jQuery代码中放什么了。 I'm just a newbie. 我只是一个新手。 Hope someone could be of help. 希望有人能帮上忙。

I think you are mixing the row data actions ("View Payslips" and "Create Payslips"), with the multi-row action ('Approve Payslips'). 我认为您正在将行数据操作(“查看工资单”和“创建工资单”)与多行操作(“批准工资单”)混合在一起。 Since you have 'Approve Payslips' outside the Grid, I'm assuming you want to Approve the all rows selected in the Grid, right? 由于您在网格外部有“批准工资单”,因此我假设您要批准网格中选择的所有行,对吗?

Here's one possible solution and uses Yii2 provided jQuery. 这是一个可能的解决方案,并使用Yii2提供的jQuery。

Assumption 假设条件

I'm assuming you used the [yii\\grid\\CheckboxColumn] ( api link ) to generate the CheckBox Column. 我假设您使用[yii\\grid\\CheckboxColumn]api链接 )来生成CheckBox Column。

JavaScript 的JavaScript

Add the following function to approve multiple ids in one go: 添加以下功能以一次性批准多个ID:

function approveSelected(selectedRows){
    for (var i = 0; i < selectedRows.length; i++) {
        approveWeekly(selectedRows[i]);
    }
}

PHP code PHP代码

And then your PHP call will change to use the new function above: 然后您的PHP调用将更改为使用上面的新函数:

<?php echo Html::button('Approve Payslips', ['value' => 'approve-w', 'class' => 'btn btn-warning btn-responsive approve-w', 'style' => 'display:none', 'onclick'=>'approveSelected($("#grid").yiiGridView("getSelectedRows"))']); ?>

Sidenote 边注

If you are interested in getting all the GridView models shown on current page, you can access them using the following code PHP code: 如果您有兴趣获取当前页面上显示的所有 GridView模型,则可以使用以下代码PHP代码访问它们:

$dataProvider->getModels()

Here's a link to the Yii2 Api documentation 这是指向Yii2 Api文档的链接

Try to check this post. 尝试检查此帖子。 checkboxColumn but this can only return 1 value . checkboxColumn,但这只能返回1值 I don't know on how to get all the values... 我不知道如何获得所有价值...

That is my biggest problem as of now. 到目前为止,这是我最大的问题 I'm still going to figure out on how to get all the values.... 我仍然要弄清楚如何获取所有值。

Except this one solution: You can get all the values using that code and return the needed ID and then you can query to the model and return all the connected data.... Which is not applicable on my system/code . 除了这一解决方案之外:您可以使用该代码获取所有值并返回所需的ID,然后可以查询模型并返回所有连接的数据。...不适用于我的系统/代码

I hope this help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM