我有一个带有复选框操作列的 checkin
模型的 GridView
在同一个 View 中,我有模型 message
的形式,带有 2 个字段 message
和 fileinput
但我想在提交按钮上再发送一个数据点击 message
模型表单,它是复选框的键。
我该怎么做?
它只能通过 javascript
完成,还是还有其他一些技术?
这是我的网格和 View 代码
<div class="row">
<p>
<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>
<div class="form-group col-xs-3 col-lg-3">
<?= $form->field($model, 'message')->textarea(['rows' => 6]) ?>
</div>
<div class="form-group col-xs-3 col-lg-3">
<?= $form->field($model, 'file')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton('Send', ['class' => 'btn btn-danger','id'=>'sendMessage']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</p>
</div>
<div class="checkin-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?php Pjax::begin(['id' => 'checkin-grid', 'timeout' => false]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'showOnEmpty'=>true,
'columns' => [
[
'class' => 'yii\grid\CheckboxColumn',
],
[
'attribute' => 'user_id',
'label' => 'Email',
'value' => 'users.email',
],
'user_type',
],
]);
?>
<?php Pjax::end(); ?>
这是我的 checkin/index
代码,我可以在其中访问 message
和 fileinput
但我想要 keys
还有...
所以用户在发送消息之前必须至少检查一行
public function actionIndex()
{
$model = new Message();
$searchModel = new CheckinSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
最佳答案
您可以扩展您的 Message 模型,添加您需要的字段
class Message extends \yii\db\ActiveRecord
{
public $yourField;
public static function tableName()
{
...
}
这样一来,在您的模型中,您还有一个未映射到数据库的字段,您可以根据需要使用....
当你提交你的值时,你应该像这样管理模型的提交相关操作
$models = $dataProvider->getModels();
if (Model::loadMultiple($models, Yii::$app->request->post()) && Model::validateMultiple($models)) {
...... your code for models managemnt
}
F
关于javascript - 向 yii2 中的表单提交操作发送额外参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32902045/