newsindex.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. use manager\models\Comments;
  3. use yii\helpers\Html;
  4. use yii\helpers\Url;
  5. use yii\grid\ActionColumn;
  6. use yii\grid\GridView;
  7. use yii\data\ActiveDataProvider;
  8. use yii\web\View;
  9. //if($model->comments!="Y") return "";
  10. /** @var yii\web\View $this */
  11. /** @var yii\data\ActiveDataProvider $dataProvider */
  12. $query = $model->hasMany(Comments::class,['news_id'=>"id"])->orderBy(['created_at'=>SORT_ASC]);
  13. $dataProvider = new ActiveDataProvider([
  14. 'query' => $query,
  15. 'pagination' => [
  16. 'pageSize' => 0
  17. ],
  18. ]);
  19. echo "comments";
  20. ?>
  21. <div class="comments-index">
  22. <?= GridView::widget([
  23. 'dataProvider' => $dataProvider,
  24. 'layout' => "{items}\n",
  25. 'columns' => [
  26. 'created_at',
  27. 'author',
  28. 'message',
  29. ['label'=>"Модерация","format"=>"raw","value"=>function($model){
  30. $moderate_button_class = $model->visible=="Y"?"btn-danger":"btn-success";
  31. return Html::button($model->visible=="Y"?"Скрыть":"Пропустить",['class'=>"btn mtoggle {$moderate_button_class}", 'id'=>$model->id, "data-value"=>$model->visible=="Y"?"N":"Y"]);
  32. }],
  33. ],
  34. ]); ?>
  35. </div>
  36. <?
  37. $manager = \Yii::$app->getUrlManager()->createUrl('/manager/comments/ajaxtoggle');
  38. $script = <<< JS
  39. $('.mtoggle').on('click', function(e) {
  40. console.log($(this).attr("id"));
  41. console.log($(this).attr("data-value"));
  42. $.ajax({
  43. url: "$manager",
  44. method: "POST",
  45. data: {id: $(this).attr("id"), value: $(this).attr("data-value")},
  46. success: function(data) {
  47. let obj = JSON.parse(data);
  48. if( obj['stat'] == 'ok' ){
  49. let id = obj['id'];
  50. let val = obj['value'];
  51. if( val=="Y" ){
  52. $('#'+id).toggleClass("btn-success");
  53. $('#'+id).addClass('btn-danger');
  54. $('#'+id).text("Скрыть");
  55. $('#'+id).attr("data-value", 'N');
  56. }else{
  57. $('#'+id).toggleClass('btn-danger');
  58. $('#'+id).addClass("btn-success");
  59. $('#'+id).text("Пропустить");
  60. $('#'+id).attr("data-value", 'Y');
  61. }
  62. }
  63. }
  64. });
  65. });
  66. JS;
  67. $this->registerJs($script, View::POS_READY);