1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- use manager\models\Comments;
- use yii\helpers\Html;
- use yii\helpers\Url;
- use yii\grid\ActionColumn;
- use yii\grid\GridView;
- use yii\data\ActiveDataProvider;
- use yii\web\View;
- $query = $model->hasMany(Comments::class,['news_id'=>"id"])->orderBy(['created_at'=>SORT_ASC]);
- $dataProvider = new ActiveDataProvider([
- 'query' => $query,
- 'pagination' => [
- 'pageSize' => 0
- ],
- ]);
- echo "comments";
- ?>
- <div class="comments-index">
- <?= GridView::widget([
- 'dataProvider' => $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"]);
- }],
- ],
- ]); ?>
- </div>
- <?
- $manager = \Yii::$app->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);
|