Answer the question
In order to leave comments, you need to log in
How to change the icon depending on the server response using ajax?
There is such a js code for processing checkboxes
$("#change").on("click", function(e){
e.preventDefault()
var keys = $("#w0").yiiGridView("getSelectedRows");
$.ajax({
url: "'. \yii\helpers\Url::toRoute(['/product/exist']) .'",
type: "POST",
data: {id: keys},
success: function(){
$("input").parent().parent().removeClass("danger")
$("input:checked").prop("checked", false)
}
})
});
<span style="color:#090" title="Товар в наличии" class="glyphicon glyphicon-ok"></span>
<span style="color:#c11" title="Товар отсутсвует" class="glyphicon glyphicon-remove"></span>
Answer the question
In order to leave comments, you need to log in
Good evening.
Here is a simplified way for you using the status change as an example. In the table column, the same span.
// GirdView column
[
'attribute' => 'status',
'contentOptions' => function($model, $key, $index, $column){
return [
'class' => 'status-column',
'style' => 'cursor:pointer',
'id' => $key,
'onclick' => '
$.ajax({
url: "' . Url::toRoute('/ajax/update-status-mark') . '",
method: "POST",
data: {id: ' . $key . ', status: ' . $model->status . '},
success: function(data){
if(data == 0){
$("td#' . $key . ' span").removeClass("label-success").addClass("label label-danger").text("Blocked")
$("tr[data-key=' . $key . ']").removeAttr("style")
}
else{
$("td#' . $key . ' span").removeClass("label-danger").addClass("label label-success").text("Active")
$("tr[data-key=' . $key . ']").css("backgroundColor","rgb(226, 243, 227)")
}
}
})
'
];
},
]
// Controller action
public function actionUpdateStatusMark($id)
{
$model = $this->findModel($id);
$model->status = $model->status == 0 ? 1 : 0;
$model->save(false,['status']);
return $model->status;
}
success: function(data){}
In data, the server's response. Analyze what in the response is responsible for your conditions for displaying the icon and change the icon with a class or completely replacing the tag with the icon (well, or if you always have 2 icons, then through .toggle or .css display: none)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question