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

4.3k Views Asked by At

I have a GridView and in my ActionColumn, there is a "View Payslip" button on each row. "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". This button is disabled by default and will only be enabled if all buttons indicate "View Payslip". enter image description here

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
I tried this but it doesn't work since it passes an ID and I have multiple values to pass:

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. I'm just a newbie. Hope someone could be of help.

2

There are 2 best solutions below

0
On

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.

Assumption

I'm assuming you used the [yii\grid\CheckboxColumn] (api link) to generate the CheckBox Column.

JavaScript

Add the following function to approve multiple ids in one go:

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

PHP code

And then your PHP call will change to use the new function above:

<?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:

$dataProvider->getModels()

Here's a link to the Yii2 Api documentation

0
On

Try to check this post. checkboxColumn but this can only return 1 value. 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.

I hope this help.