sysAd.js 6.6 KB

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