comments!="Y") return ""; /** @var yii\web\View $this */ /** @var yii\data\ActiveDataProvider $dataProvider */ $query = $model->hasMany(Comments::class,['news_id'=>"id"])->orderBy(['created_at'=>SORT_ASC]); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 0 ], ]); echo "comments"; ?>
$dataProvider, 'layout' => "{items}\n", 'columns' => [ 'created_at', 'author', 'message', ['label'=>"Модерация","format"=>"raw","value"=>function($model){ $moderate_button_class = $model->visible=="Y"?"btn-danger":"btn-success"; return Html::button($model->visible=="Y"?"Скрыть":"Пропустить",['class'=>"btn mtoggle {$moderate_button_class}", 'id'=>$model->id, "data-value"=>$model->visible=="Y"?"N":"Y"]); }], ], ]); ?>
getUrlManager()->createUrl('/manager/comments/ajaxtoggle'); $script = <<< JS $('.mtoggle').on('click', function(e) { console.log($(this).attr("id")); console.log($(this).attr("data-value")); $.ajax({ url: "$manager", method: "POST", data: {id: $(this).attr("id"), value: $(this).attr("data-value")}, success: function(data) { let obj = JSON.parse(data); if( obj['stat'] == 'ok' ){ let id = obj['id']; let val = obj['value']; if( val=="Y" ){ $('#'+id).toggleClass("btn-success"); $('#'+id).addClass('btn-danger'); $('#'+id).text("Скрыть"); $('#'+id).attr("data-value", 'N'); }else{ $('#'+id).toggleClass('btn-danger'); $('#'+id).addClass("btn-success"); $('#'+id).text("Пропустить"); $('#'+id).attr("data-value", 'Y'); } } } }); }); JS; $this->registerJs($script, View::POS_READY);