Hi members,
I try to do gridview filtering inside activeForm to create data master and details, but gridview filtering doesn’t work and did unexpected behaviour, like this post https://github.com/yiisoft/yii2/issues/10456
What i want here, do filtering and check the selected row with CheckboxColumn
and then posting to database.
please advice. thanks before
here is my code.
controller
public function actionCreate()
{
$searchModel = new PoAgenSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere(['tax_used' => 0]);
$model = new PphHeader();
$model->trx_id = 'PPH-' . date('Ymd') . '-' . '001';
$model->trx_date = date('Y-m-d');
$model->no_urut = 'MEI25';
$model->total = 0;
if ($this->request->isPost) {
if ($model->load($this->request->post())) {
$model->created_by = Yii::$app->user->identity->id;
$model->created_time = date('Y-m-d H:i:s');
$model->print_count = 0;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
$model->loadDefaultValues();
}
return $this->render('create', [
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]);
}
view files create.php
<?= $this->render('_form', [
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]) ?>
_form.php file
<?php
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\date\DatePicker;
use yii\grid\GridView;
/** @var yii\web\View $this */
/** @var app\models\PphHeader $model */
/** @var yii\widgets\ActiveForm $form */
$gridColumns = [
[
'class' => 'yii\grid\CheckboxColumn',
'checkboxOptions' =>
function($model) {
return ['value' => $model->id, 'class' => 'checkbox-row', 'id' => 'checkbox'];
}
],
'id',
['attribute' => 'buy_date', 'label' => 'Tgl. Penerimaan'],
'vehicle_id',
[
'attribute' => 'agen_id',
'label' => 'Agen',
'value' => function($model) {
return $model->agen_id . ' - ' . $model->agen->name;
}
],
['attribute' => 'agen.address_1', 'label' => 'Lokasi'],
'pks.name',
'agen.sub_agen',
[
'attribute' => 'r_bersih',
'format'=>['decimal', 0],
],
[
'attribute' => 'total_rupiah',
'format'=>['decimal', 0],
],
[
'attribute' => 'pph.value',
'label' => 'Pot. Svc.T (%)',
'format'=>['decimal', 2],
],
[
'attribute' => 'pot_pph',
'format'=>['decimal', 0],
]
];
?>
<div class="pph-header-form">
<?php $form = ActiveForm::begin(['id' => 'form-pph-22' ]); ?>
<div class="row">
<div class="col-6">
<?= $form->field($model, 'trx_id')->textInput(['maxlength' => true, 'readonly' => true]) ?>
</div>
<div class="col-6">
<?= $form->field($model, 'trx_date')->widget(DatePicker::class,
[
'type' => DatePicker::TYPE_COMPONENT_APPEND,
'pluginOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]) ?>
</div>
<div class="col-6">
<?= $form->field($model, 'no_urut')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-6">
<?= $form->field($model, 'company_id')->dropDownList(
ArrayHelper::map(app\models\CompanyProfile::find()->all(), 'id', 'name'),
['prompt' => 'Select Company Profile']
) ?>
</div>
<div class="col-12">
<?= $form->field($model, 'agen_id')->widget(kartik\select2\Select2::class, [
'data' => ArrayHelper::map(\app\models\Agen::find()->where(['listed' => true, 'role' => 'AGEN'])->all(), 'id', 'name'),
'options' => ['placeholder' => 'Select agen'],
'pluginOptions' => [
'allowClear' => true,
],
]
) ?>
<?= $form->field($model, 'total')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-12">
<?= GridView::widget([
'id' => 'kvgrid-poagen-pph22',
'dataProvider'=>$dataProvider,
'filterModel'=> null,
'columns'=>$gridColumns,
]);
?>
</div>
</div> <!-- /.row -->
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>