You cannot get a checked column. I presume you mean you want to get SELECTED ROWS in a CheckboxColumn.
Something like this…
// javascript to call for your button on
// the view - which on click - will process the
// action for all selected rows
var keys = $('#grid').yiiGridView('getSelectedRows');
$.post({
url: vUrl, // your controller action
dataType: 'json',
data: {keylist: keys},
success: function(data) {
alert('I did it! Processed checked rows.')
},
});
// your controller action (php code)
public function actionProcessSelected() {
if (isset($_POST['keylist'])) {
$keys = \yii\helpers\Json::decode($_POST['keylist']);
// you will have the array of pk ids to process in $keys
// perform batch action on these keys and return status
// back to ajax call above
}
}
In your view button action, include a javascript similar to one below:
$('element').one('click',function() {
var keys = $('#grid').yiiGridView('getSelectedRows'); // returns an array of pkeys, and #grid is your grid element id
$.post({
url: '/site/calculate-total', // your controller action
dataType: 'json',
data: {keylist: keys},
success: function(data) {
if (data.status === 'success') {
alert('Total price is ' + data.total);
}
},
});
});
In your controller (php code for action similar to below):
public function actionCalculateTotal() {
if (isset($_POST['keylist'])) {
$keys = \yii\helpers\Json::decode($_POST['keylist']);
if (!is_array($keys)) {
echo Json::encode([
'status' => 'error',
'total' => 0
]);
return;
}
$total = 0;
// you could alternatively write a single query using
// SQL IN CONDITION, instead of loop below to fetch the total
// from DB in ONE SINGLE fetch from the DB.
foreach ($keys as $key) {
$model = YourSourceModel::findOne($key);
// get total price from your model's column e.g. price
$total += !empty($model->price) ? $model->price : 0;
}
echo Json::encode([
'status' => 'success',
'total' => $total
]);
}
echo Json::encode([
'status' => 'error',
'total' => 0
]);
}
You can also register the above asset as a dependency within an AssetBundle. In yii advanced app, this is included by default via the frontend\assets\AppAsset.php (check this line):
public $depends = [
'yii\web\YiiAsset',
//
]
The YiiAsset includes JQueryAsset as a dependency.
You may need to debug your code using Firebug or some tool to understand where the error is received. BTW… your JS alert will not return a string because, the [font="Courier New"]var keys[/font] is an array.
I am not sure you understood at all. The function $(’#w1’).yiiGridView(‘getSelectedRows’) returns an array of the primary keys (which are selected through checkbox in the grid). Please check the code I shared earlier in this page - the variable keys is an array. You need to read/loop through this array in your controller action (passed by an ajax call) to do whatever you want (read based on selected pkeys from db, and generate total or do any calculation).
I’ve tried with absolutly url, and relatives one, but not. How can it be??
This is my action in myController:
public function actionMyAction( ) {
if (isset($_POST['keylist'])) {
$keys = \yii\helpers\Json::decode($_POST['keylist']);
// you will have the array of pk ids to process in $keys
// perform batch action on these keys and return status
// back to ajax call above
}
}
$keys = \yii\helpers\Json::decode(\Yii::$app->request->post('keylist'));
// you will have the array of pk ids to process in $keys
// perform batch action on these keys and return status
// back to ajax call above
}
return TRUE;
}