index.blade.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. @extends('layouts.app')
  2. @section('title')单位@endsection
  3. @section('content')
  4. <span id="nav2">
  5. @component('maintenance.menu')@endcomponent
  6. @component('maintenance.unit.menu')@endcomponent
  7. </span>
  8. <div class="container-fluid" id="list" @drop="drop($event)" @dragover="dragover($event, false)">
  9. <div class="modal fade" tabindex="-1" role="dialog" id="modal">
  10. <div class="modal-dialog modal-lg modal-dialog-centered">
  11. <div class="modal-content">
  12. <div class="modal-header">
  13. <button type="button" class="close" data-dismiss="modal">&times;</button>
  14. </div>
  15. <div class="modal-body">
  16. <div class="row">
  17. <label class="col-2 offset-1" for="name">名称</label>
  18. <input class="col-7 form-control form-control-sm" :class="errors.name ? 'is-invalid' : ''" id="name" type="text" v-model="unit.name">
  19. <span class="invalid-feedback mt-0 offset-3" role="alert" v-if="errors.name">
  20. <strong>@{{ errors.name[0] }}</strong>
  21. </span>
  22. </div>
  23. </div>
  24. <div class="modal-footer">
  25. <button type="button" class="btn btn-success" @click="submitUnit()">提交</button>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. <div class="card">
  31. <div class="card-body">
  32. @can("计量单位-录入")<button class="btn btn-outline-info mb-1 mr-3" @click="openModal()"><span class="fa fa-plus"></span>&nbsp;新&nbsp;&nbsp;增</button>@endcan
  33. @can("计量单位-录入")<button class="btn border border-2 mb-1 mr-3" :class="isSaveSort ? 'btn-success' : 'text-secondary'" :disabled="!isSaveSort" @click="sort()"> 保 存 </button>@endcan
  34. <table class="table table-striped table-sm">
  35. <tr>
  36. <th>ID</th>
  37. <th>计量单位名称</th>
  38. <th>录入时间</th>
  39. <th>操作</th>
  40. </tr>
  41. <tbody id="parent">
  42. <tr v-for="(u,i) in units" draggable="true" :id="'unit-'+u.id" :data-order="u.sequence" :data-id="u.id"
  43. @click="selectTr===i+1?selectTr=0:selectTr=i+1" :class="selectTr===i+1?'focusing' : ''"
  44. @drop="drop($event)" @dragover="dragover($event)" @dragstart="dragstart(u.id)" style="cursor: move">
  45. <td class="text-muted">@{{u.id}}</td>
  46. <td>@{{u.name}}</td>
  47. <td class="text-muted">@{{u.created_at}}</td>
  48. <td>
  49. @can('计量单位-编辑')
  50. <button class="btn btn-sm btn-outline-primary" @click="openModal(u)">改</button> @endcan
  51. @can('计量单位-删除')
  52. <button class="btn btn-sm btn-outline-dark" @click="destroy(u)">删</button> @endcan
  53. </td>
  54. </tr>
  55. <tr id="dragover-container" class="border-dashed-red" v-if="isDragover" @drop="drop($event)" @dragover="dragover($event,false)">
  56. <td colspan='4' class='text-secondary'><div class='w-100 text-center'>拖拽至此</div></td>
  57. </tr>
  58. </tbody>
  59. </table>
  60. {{$units->links()}}
  61. </div>
  62. </div>
  63. </div>
  64. @endsection
  65. @section('lastScript')
  66. <script>
  67. new Vue({
  68. el:"#list",
  69. data:{
  70. units:[
  71. @foreach( $units as $unit)
  72. {id:'{{$unit->id}}',name:'{{$unit->name}}',created_at:'{{$unit->created_at}}',sequence:'{{$unit->sequence}}'},
  73. @endforeach
  74. ],
  75. selectTr:0,
  76. unit:{},
  77. errors:{},
  78. dragoverId:"",
  79. isDragover : false,
  80. isSaveSort : false,
  81. },
  82. methods:{
  83. edit:function(id){
  84. location.href = "{{url('maintenance/unit')}}/"+id+"/edit";
  85. },
  86. destroy:function(unit){
  87. if(!confirm('确定要删除单位“' + unit.name + '”吗?'))return;
  88. let data=this;
  89. let url = "{{url('maintenance/unit')}}/"+unit.id;
  90. window.axios.delete(url,{id:unit.id})
  91. .then(function (response) {
  92. if(response.data.success){
  93. for (let i = 0; i < data.units.length; i++) {
  94. if (data.units[i].id===unit.id){
  95. data.units.splice(i,1);
  96. break;
  97. }
  98. }
  99. tempTip.setDuration(1000);
  100. tempTip.showSuccess('删除单位"'+unit.name+'"成功!')
  101. }else{
  102. tempTip.setDuration(1000);
  103. tempTip.show('删除单位"'+unit.name+'"失败!')
  104. }
  105. }).catch(function (err) {
  106. tempTip.setDuration(3000);
  107. tempTip.show('删除单位失败!'+'网络错误:' + err);
  108. });
  109. },
  110. openModal(unit = null){
  111. if (unit) this.unit={id:unit.id,name:unit.name};
  112. else this.tag={id:"",name:""};
  113. $("#modal").modal("show");
  114. },
  115. submitUnit(){
  116. let url="{{url('maintenance/unit/save')}}";
  117. let msg=this.unit.id ? "成功修改单位“"+this.unit.name+"”" : "成功新增单位“"+this.unit.name+"”";
  118. window.tempTip.postBasicRequest(url,this.unit,(res)=>{
  119. if(res && res.errors){
  120. this.errors = res.errors;
  121. return '';
  122. }
  123. if (this.unit.id){
  124. this.units.some((unit)=> {
  125. if (unit.id === this.unit.id){
  126. unit.name = this.unit.name;
  127. return true;
  128. }
  129. });
  130. }else this.units.unshift({
  131. id:res.id,
  132. name:res.name,
  133. created_at:res.created_at,
  134. });
  135. $("#modal").modal("hide");
  136. return msg;
  137. },true);
  138. },
  139. dragover(e, isAllow = true){
  140. e.preventDefault();
  141. e.stopPropagation();
  142. if (isAllow) e.currentTarget.after(document.getElementById("dragover-container"))
  143. },
  144. dragstart(id){
  145. $("#unit-"+id).after($("#dragover-container"));
  146. this.dragoverId = id;
  147. this.isDragover = true;
  148. },
  149. drop(e){
  150. e.preventDefault();
  151. e.stopPropagation();
  152. $("#dragover-container").after($("#unit-"+this.dragoverId));
  153. this.dragoverId = "";
  154. this.isDragover = false;
  155. this.isSaveSort = true;
  156. },
  157. sort(){
  158. let parent = document.getElementById("parent").children;
  159. let update = [["id","sequence"]];
  160. for (let i=0;i<parent.length;i++){
  161. let order = parent[i].getAttribute("data-order");
  162. let id = parent[i].getAttribute("data-id");
  163. if (id && order !== i+1)update.push({id:id,sequence:i+1});
  164. }
  165. if (update.length > 1){
  166. let url = "{{url('maintenance/unit/sort')}}";
  167. window.tempTip.postBasicRequest(url,{"update":update},res=>{
  168. this.isSaveSort = false;
  169. return "";
  170. });
  171. }else this.isSaveSort = false;
  172. },
  173. }
  174. });
  175. </script>
  176. @endsection