hot_search.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/3/17 0017
  6. * Time: 上午 10:52
  7. */
  8. use yii\helpers\Url;
  9. use yii\widgets\LinkPager;
  10. ?>
  11. <style>
  12. .color{
  13. color: red;
  14. }
  15. </style>
  16. <div class="row">
  17. <div class="col-sm-12">
  18. <div class="ibox float-e-margins">
  19. <div class="ibox-content">
  20. <div class="row">
  21. <div class="col-sm-5 m-b-xs">
  22. <a class="btn btn-sm btn-primary addhotsearch" href="#"> 新建热门搜索</a>
  23. </div>
  24. </div>
  25. <div class="table-responsive">
  26. <table class="table table-striped">
  27. <thead>
  28. <tr>
  29. <th>ID</th>
  30. <th>热门搜索词</th>
  31. <th>操作</th>
  32. </tr>
  33. </thead>
  34. <tbody>
  35. <form id="checkForm" method="post">
  36. <?php $i=0; foreach($datas as $data):?>
  37. <tr>
  38. <td><?php $i++;echo $i;?></td>
  39. <td class="keyword"><?= $data->keyword?></td>
  40. <td>
  41. <a href="#" data="true" hotsearchid="<?= $data->id?>" class="btn btn-sm btn-primary edit_search">修改</a>
  42. <a class="btn btn-sm btn-danger del_search"hotsearchid="<?= $data->id?>">删除</a>
  43. </td>
  44. </tr>
  45. <?php endforeach;?>
  46. </form>
  47. </tbody>
  48. </table>
  49. </div>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. <script>
  55. var lock = true;
  56. $('.addhotsearch').click(function(){
  57. if(lock){
  58. var i = $('tr').length;
  59. var html = '<tr><td>'+parseInt(i-1)+'</td><td class="keyword"><input width="20px"></td><td>';
  60. html+='<a href="#" data="false" class="btn btn-sm btn-primary edit_search">添加</a>';
  61. html+='<a class="btn btn-sm btn-danger del_search">删除</a></td></tr>';
  62. $('tbody').append(html);
  63. }
  64. lock = false;
  65. });
  66. $('.table-responsive').on('click','.edit_search',function(){
  67. var tr = $(this).parents('tr');
  68. if($(this).attr('data')=='true'){
  69. var keyword = $(this).parents('tr').find('.keyword').text();
  70. var html = '<input value="'+keyword+'">';
  71. $(this).parents('tr').find('.keyword').html(html);
  72. $(this).attr('data','false');
  73. $(this).text('确认');
  74. }else{
  75. var type = $(this).text();
  76. var tr = $(this).parents('tr');
  77. var keyword = $(this).parents('tr').find('input').val();
  78. $(this).attr('data','true');
  79. $(this).text('修改');
  80. if(type=='添加'){
  81. $.ajax({
  82. url:'<?=Url::toRoute(['webconfig/addhotsearch']);?>',
  83. type:'POST',
  84. dataType:'json',
  85. data:{keyword:keyword,'_csrf-backend':'<?=Yii::$app->request->csrfToken;?>'},
  86. success:function(data){
  87. if(data.sign==1){
  88. swal("", data.msg, "success");
  89. tr.find('.keyword').html(keyword);
  90. tr.find('.edit_search').attr('hotsearchid',data.id);
  91. tr.find('.del_search').attr('hotsearchid',data.id);
  92. lock = true;
  93. }else{
  94. swal("", data.msg, "error");
  95. tr.remove();
  96. lock = true;
  97. }
  98. }
  99. })
  100. }else if(type=='确认'){
  101. var id = $(this).attr('hotsearchid');
  102. $.ajax({
  103. url:'<?=Url::toRoute(['webconfig/edithotsearch']);?>',
  104. type:'POST',
  105. dataType:'json',
  106. data:{id:id,keyword:keyword,'_csrf-backend':'<?=Yii::$app->request->csrfToken;?>'},
  107. success:function(data){
  108. if(data.sign==1){
  109. swal("", data.msg, "success");
  110. tr.find('.keyword').html(keyword);
  111. // $(this).attr('hotsearchid',data.id);
  112. // tr.find('.del_search').attr('hotsearchid',data.id);
  113. // lock = true;
  114. }else{
  115. swal("", data.msg, "error");
  116. // tr.remove();
  117. }
  118. }
  119. })
  120. }
  121. }
  122. });
  123. $('.table-responsive').on('click','.del_search',function(){
  124. var id = $(this).attr('hotsearchid');
  125. var tr = $(this).parents('tr');
  126. swal({
  127. title: "确定删除吗?",
  128. type: "warning",
  129. showCancelButton: true,
  130. confirmButtonColor: "#DD6B55",
  131. confirmButtonText: "确定",
  132. cancelButtonText: "取消",
  133. closeOnConfirm: true,
  134. closeOnCancel: true },
  135. function(isConfirm){
  136. if (isConfirm) {
  137. $.ajax({
  138. url:'<?=Url::toRoute(['webconfig/delhotsearch']);?>',
  139. type:'POST',
  140. dataType:'json',
  141. data:{id:id,'_csrf-backend':'<?=Yii::$app->request->csrfToken;?>'},
  142. success:function(data){
  143. tr.remove();
  144. if(data.sign==1){
  145. tr.remove();
  146. }else{
  147. swal("", data.msg, "error");
  148. }
  149. }
  150. })
  151. }
  152. });
  153. })
  154. </script>