[英]How to Update just one field in Yii2 with custom button, and not all records?
I'm really new to yii2 and looking forward to post as few questions as I can. 我真的是yii2的新手,并希望发布尽可能少的问题。 I would like to update a data field with curdate() when a button is pressed.. but currently, pressing the button updates ALL the records: 我想在按下按钮时使用curdate()更新数据字段。但是当前,按下按钮将更新所有记录:
[
'class' => 'kartik\grid\ActionColumn',
'header' => 'Aggiorna',
'template' => '{my_button}',
'buttons' => [
'my_button' => function ($url, $model, $key) {
return Html::button('<span class="glyphicon glyphicon-link">
</span>',
['value'=>Yii::$app->db
->createCommand("UPDATE
qual_lab_sistema_qualita_attivita SET
data_ultimo_invio=CURDATE()
WHERE id= '$model->id' ;")
->execute(),
'class' => 'btn btn-default btn-xs',
'id'=>'modalButtonView',
]);;
}
],
'options' => [
'width' => '20',
],
],
Can u help me pleasE? 你能帮我取悦吗? Thanks a lot 非常感谢
This doesn't work this way. 这样行不通。 You should send HTTP request to your controller's action for this. 为此,您应该将HTTP请求发送到控制器的操作。 For example with AJAX. 例如,使用AJAX。
[
'class' => 'yii\grid\ActionColumn',
'buttons' => [
'my_button' => function ($url, $model, $key) {
return Html::button('<span class="glyphicon glyphicon-link"></span>', [
'onclick' => "
$.ajax({
type: 'DELETE',
url: '" . Url::to(['update-item', 'id' => $model->primaryKey]) . "',
dataType: 'json'
}).done(function(){alert('Updated');});
",
]);
}
],
],
And create action 并创造行动
public function actionUpdateItem($id)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$updatedRows = \Yii::$app->db
->createCommand()
->update('{{qual_lab_sistema_qualita_attivita }}', [
'data_ultimo_invio' => new \yii\db\Expression("NOW()")
], ['id' => $id])
->execute();
return [];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.