Within my yii2 application, I am looking to group machines into specific machine groups. Due to the large number of machines, users must select them via a checkbox column in a kartik-gridview, which offers sorting and filtering capabilities that a standard multiselect form field lacks.
The challenge arises when attempting to process the selected rows of the grid. The only solution I have found involves using Javascript, as outlined in this forum post: http://www.yiiframework.com/forum/index.php/topic/53777-gridview-get-selected-colum/
To achieve this, I've incorporated the following code snippet within a button:
<?= Html::button(yii::t('app', 'Save'), ['class' => 'btn btn-primary',
'onclick' => "var keys = $('#w0.grid-view').yiiGridView('getSelectedRows');"
. "$.ajax({"
. "type: 'POST',"
. "url: 'http://localhost:8080/eddb1/frontend/web/index.php?r=machine-group/test',"
. "dataType: 'json',"
. "data: {keylist: keys}});"]) ?>
As highlighted, I had to resort to using $.ajax
rather than $.post
, as the latter resulted in a 404 not found error due to issues with the URL.
However, I'm encountering an issue where the ajax POST request is consistently empty. Here's the controller code snippet designed to echo the post-request data:
public function actionTest()
{
echo var_dump(Yii::$app->request->post());
die();
}
I even attempted using:
echo var_dump($_POST);
with identical results. What could be causing this problem? Is there a way to process the selected rows without relying on Javascript? I experimented with embedding the gridview within ActiveForm tags and using a standard submitButton, but no post-request was triggered.
If anyone can provide assistance, I've been grappling with this for 2 days now...thank you in advance!
PS.: below is the gridview code snippet:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel'=>$searchModel,
'columns' => [
['class' => 'kartik\grid\CheckboxColumn'],
'company_name',
'identifier',
'licenceplate',
'serial_number',
'type_name',
'class_name',
'manufacturer',
'model_name',
'variant_name',
],
'persistResize' => true,
'showPageSummary' => false,
'pjax' => true,
'panel' => [
'before' => ''
],
'toolbar' => [
'{export}',
'{toggleData}'
]
]);
?>