notification.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. app.controller('notificationCtrl', ['$scope', '$http', '$timeout', 'uiGridConstants', 'i18nService', 'toaster', function ($scope, $http, $timeout, uiGridConstants, i18nService, toaster) {
  2. i18nService.setCurrentLang("zh-cn");
  3. $scope.gridOptions = {
  4. data: 'myData',
  5. showGridFooter: true, //是否显示grid footer
  6. //-------- 分页属性 ----------------
  7. paginationPageSizes: [10, 20, 50, 100], //每页显示个数可选项
  8. paginationCurrentPage: 1, //当前页码
  9. paginationPageSize: 10, //每页显示个数
  10. totalItems: 0,// 总数量
  11. useExternalPagination: true,//是否使用分页按钮
  12. //过滤
  13. enableFiltering: true,
  14. columnDefs: [],
  15. //---------------api---------------------
  16. onRegisterApi: function (gridApi) {
  17. $scope.gridApi = gridApi;
  18. gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
  19. if ($scope.setPagingData) {
  20. $scope.getPagedDataAsync(newPage, pageSize);
  21. }
  22. });
  23. }
  24. };
  25. //查询条件
  26. var condition = $scope.condition = {
  27. searchKey: ""
  28. };
  29. function setColumnDefs() {
  30. $scope.gridOptions.columnDefs = [
  31. {field: 'id', displayName: '编号'},
  32. {field: 'text', displayName: '内容'},
  33. {field: 'startTime', displayName: '公告开始时间'},
  34. {field: 'endTime', displayName: '公告结束时间'},
  35. {
  36. field: 'status', displayName: '状态',
  37. cellTemplate: '<div class="temp-row" ng-class="{' +
  38. '\'text-dark\':row.entity.status==0,' +
  39. '\'text-success\':row.entity.status==1' +
  40. '}" >{{row.entity.status==0?"未运行":"正在运行"}}</div>'
  41. },
  42. {
  43. field: 'oper', displayName: '操作',
  44. enableFiltering: false,
  45. enableSorting: false,
  46. enableHiding: false,//禁止在列选择器中隐藏
  47. enableColumnMenu: false,// 是否显示列头部菜单按钮
  48. width: 200,
  49. cellTemplate: '<div class="grid-button">' +
  50. '<button class="btn btn-sm btn-rounded btn-success" ng-if="row.entity.status==0" ng-click="grid.appScope.openOrClose(row.entity,1)"><i class="fa fa-play"></i> 打开</button>' +
  51. '<button class="btn btn-sm btn-rounded btn-default" ng-if="row.entity.status==1" ng-click="grid.appScope.openOrClose(row.entity,0)"><i class="fa fa-close"></i> 关闭</button>' +
  52. '</div>'
  53. }
  54. ];
  55. var fields = $scope.gridOptions.columnDefs;
  56. for (var index in fields) {
  57. var item = fields[index];
  58. if (item && item['minWidth'] == null) {
  59. item['minWidth'] = 100;
  60. }
  61. }
  62. }
  63. function initDataGrid() {
  64. //首次加载表格
  65. $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
  66. }
  67. $scope.setPagingData = function (data, curPage, pageSize) {
  68. var pagedData = data.data.dataList;
  69. $scope.myData = pagedData;
  70. $scope.gridOptions.totalItems = data.data.total;
  71. };
  72. $scope.getPagedDataAsync = function (curPage, pageSize) {
  73. var params = {
  74. pageSize: pageSize,
  75. pageIndex: curPage
  76. };
  77. if (condition.searchKey != "") {
  78. params.searchKey = condition.searchKey
  79. }
  80. $http.get('/manager/getNotificationList', {
  81. params: params
  82. }).then(function (data) {
  83. $scope.setPagingData(data.data, curPage, pageSize);
  84. }).catch(function (data) {
  85. toaster.pop("error", "提示", "获取数据失败");
  86. });
  87. };
  88. setColumnDefs();
  89. initDataGrid();
  90. //事件
  91. $scope.event = {
  92. search: function () {
  93. $scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize);
  94. }
  95. };
  96. //编辑弹窗需要的数据
  97. $scope.dialogName = "编辑公告";
  98. $scope.dialogData = {};
  99. //添加
  100. $scope.add = function () {
  101. //重置表单状态
  102. $scope.noticeForm.$setPristine();
  103. $scope.noticeForm.$setUntouched();
  104. $scope.dialogName = "添加公告";
  105. $scope.dialogData = {status: true};
  106. $("#noticePanel").modal();
  107. };
  108. //编辑
  109. $scope.edit = function () {
  110. //重置表单状态
  111. $scope.noticeForm.$setPristine();
  112. $scope.noticeForm.$setUntouched();
  113. $scope.dialogName = "编辑公告";
  114. var rows = $scope.gridApi.selection.getSelectedRows();
  115. if (rows.length == 0) {
  116. toaster.pop("info", "提示", "请选择数据!");
  117. return;
  118. }
  119. if (rows.length > 1) {
  120. toaster.pop("info", "提示", "只能选中编辑一条数据");
  121. return;
  122. }
  123. var item = rows[0];
  124. $scope.dialogData = $.extend(true, {}, item);
  125. $scope.dialogData.status = $scope.dialogData.status == 1 ? true : false;
  126. $("#noticePanel").modal();
  127. };
  128. //打开或关闭
  129. $scope.openOrClose = function (entity, status) {
  130. $http({
  131. method: 'POST',
  132. url: '/manager/toggleNotice',
  133. data: {id: entity.id, status: status}
  134. }).then(function (response) {
  135. initDataGrid();
  136. }, function (response) {
  137. toaster.pop("error", "提示", "操作失败!");
  138. });
  139. };
  140. //删除
  141. $scope.deleteData = function () {
  142. var rows = $scope.gridApi.selection.getSelectedRows();
  143. if (rows.length == 0) {
  144. toaster.pop("info", "提示", "请选择数据!");
  145. return;
  146. }
  147. var ids = [];
  148. for (var i = 0; i < rows.length; i++) {
  149. ids.push(rows[i].id);
  150. }
  151. $.confirm({
  152. content: '确定删除?',
  153. buttons: {
  154. ok: {
  155. btnClass: 'btn-red',
  156. action: function () {
  157. $http({
  158. method: 'POST',
  159. url: '/manager/deleteNotice',
  160. data: {ids: ids}
  161. }).then(function (response) {
  162. initDataGrid();
  163. }, function (response) {
  164. toaster.pop("error", "提示", "删除失败!");
  165. });
  166. }
  167. },
  168. }
  169. });
  170. };
  171. //提交表单保存
  172. $scope.saveData = function () {
  173. //表单未校验通过不能提交
  174. if ($scope.noticeForm.$invalid) {
  175. return;
  176. }
  177. var url = "";
  178. if ($scope.dialogData.id == null) {
  179. url = "/manager/addNotice";
  180. } else {
  181. url = "/manager/editNotice";
  182. }
  183. //深度克隆 避免操作原对象
  184. var data = $.extend(true, {}, $scope.dialogData);
  185. data.status = $scope.dialogData.status ? 1 : 0;
  186. $http({
  187. method: 'POST',
  188. url: url,
  189. data: data
  190. }).then(function (response) {
  191. $('#noticePanel').modal('hide');//弹窗消失
  192. $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
  193. }, function (response) {
  194. toaster.pop("error", "提示", "保存失败!");
  195. });
  196. };
  197. $scope.startTimeOpen = false;
  198. $scope.endTimeOpen = false;
  199. $scope.timeChange = function (newDate, oldDate) {
  200. $scope.startTimeOpen = false;
  201. $scope.endTimeOpen = false;
  202. };
  203. }]);