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".
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.
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:
PHP code
And then your PHP call will change to use the new function above:
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:
Here's a link to the Yii2 Api documentation