123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- app.controller('notificationCtrl', ['$scope', '$http', '$timeout', 'uiGridConstants', 'i18nService', 'toaster', function ($scope, $http, $timeout, uiGridConstants, i18nService, toaster) {
- i18nService.setCurrentLang("zh-cn");
- $scope.gridOptions = {
- data: 'myData',
- showGridFooter: true, //是否显示grid footer
- //-------- 分页属性 ----------------
- paginationPageSizes: [10, 20, 50, 100], //每页显示个数可选项
- paginationCurrentPage: 1, //当前页码
- paginationPageSize: 10, //每页显示个数
- totalItems: 0,// 总数量
- useExternalPagination: true,//是否使用分页按钮
- //过滤
- enableFiltering: true,
- columnDefs: [],
- //---------------api---------------------
- onRegisterApi: function (gridApi) {
- $scope.gridApi = gridApi;
- gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
- if ($scope.setPagingData) {
- $scope.getPagedDataAsync(newPage, pageSize);
- }
- });
- }
- };
- //查询条件
- var condition = $scope.condition = {
- searchKey: ""
- };
- function setColumnDefs() {
- $scope.gridOptions.columnDefs = [
- {field: 'id', displayName: '编号'},
- {field: 'text', displayName: '内容'},
- {field: 'startTime', displayName: '公告开始时间'},
- {field: 'endTime', displayName: '公告结束时间'},
- {
- field: 'status', displayName: '状态',
- cellTemplate: '<div class="temp-row" ng-class="{' +
- '\'text-dark\':row.entity.status==0,' +
- '\'text-success\':row.entity.status==1' +
- '}" >{{row.entity.status==0?"未运行":"正在运行"}}</div>'
- },
- {
- field: 'oper', displayName: '操作',
- enableFiltering: false,
- enableSorting: false,
- enableHiding: false,//禁止在列选择器中隐藏
- enableColumnMenu: false,// 是否显示列头部菜单按钮
- width: 200,
- cellTemplate: '<div class="grid-button">' +
- '<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>' +
- '<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>' +
- '</div>'
- }
- ];
- var fields = $scope.gridOptions.columnDefs;
- for (var index in fields) {
- var item = fields[index];
- if (item && item['minWidth'] == null) {
- item['minWidth'] = 100;
- }
- }
- }
- function initDataGrid() {
- //首次加载表格
- $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
- }
- $scope.setPagingData = function (data, curPage, pageSize) {
- var pagedData = data.data.dataList;
- $scope.myData = pagedData;
- $scope.gridOptions.totalItems = data.data.total;
- };
- $scope.getPagedDataAsync = function (curPage, pageSize) {
- var params = {
- pageSize: pageSize,
- pageIndex: curPage
- };
- if (condition.searchKey != "") {
- params.searchKey = condition.searchKey
- }
- $http.get('/manager/getNotificationList', {
- params: params
- }).then(function (data) {
- $scope.setPagingData(data.data, curPage, pageSize);
- }).catch(function (data) {
- toaster.pop("error", "提示", "获取数据失败");
- });
- };
- setColumnDefs();
- initDataGrid();
- //事件
- $scope.event = {
- search: function () {
- $scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize);
- }
- };
- //编辑弹窗需要的数据
- $scope.dialogName = "编辑公告";
- $scope.dialogData = {};
- //添加
- $scope.add = function () {
- //重置表单状态
- $scope.noticeForm.$setPristine();
- $scope.noticeForm.$setUntouched();
- $scope.dialogName = "添加公告";
- $scope.dialogData = {status: true};
- $("#noticePanel").modal();
- };
- //编辑
- $scope.edit = function () {
- //重置表单状态
- $scope.noticeForm.$setPristine();
- $scope.noticeForm.$setUntouched();
- $scope.dialogName = "编辑公告";
- var rows = $scope.gridApi.selection.getSelectedRows();
- if (rows.length == 0) {
- toaster.pop("info", "提示", "请选择数据!");
- return;
- }
- if (rows.length > 1) {
- toaster.pop("info", "提示", "只能选中编辑一条数据");
- return;
- }
- var item = rows[0];
- $scope.dialogData = $.extend(true, {}, item);
- $scope.dialogData.status = $scope.dialogData.status == 1 ? true : false;
- $("#noticePanel").modal();
- };
- //打开或关闭
- $scope.openOrClose = function (entity, status) {
- $http({
- method: 'POST',
- url: '/manager/toggleNotice',
- data: {id: entity.id, status: status}
- }).then(function (response) {
- initDataGrid();
- }, function (response) {
- toaster.pop("error", "提示", "操作失败!");
- });
- };
- //删除
- $scope.deleteData = function () {
- var rows = $scope.gridApi.selection.getSelectedRows();
- if (rows.length == 0) {
- toaster.pop("info", "提示", "请选择数据!");
- return;
- }
- var ids = [];
- for (var i = 0; i < rows.length; i++) {
- ids.push(rows[i].id);
- }
- $.confirm({
- content: '确定删除?',
- buttons: {
- ok: {
- btnClass: 'btn-red',
- action: function () {
- $http({
- method: 'POST',
- url: '/manager/deleteNotice',
- data: {ids: ids}
- }).then(function (response) {
- initDataGrid();
- }, function (response) {
- toaster.pop("error", "提示", "删除失败!");
- });
- }
- },
- }
- });
- };
- //提交表单保存
- $scope.saveData = function () {
- //表单未校验通过不能提交
- if ($scope.noticeForm.$invalid) {
- return;
- }
- var url = "";
- if ($scope.dialogData.id == null) {
- url = "/manager/addNotice";
- } else {
- url = "/manager/editNotice";
- }
- //深度克隆 避免操作原对象
- var data = $.extend(true, {}, $scope.dialogData);
- data.status = $scope.dialogData.status ? 1 : 0;
- $http({
- method: 'POST',
- url: url,
- data: data
- }).then(function (response) {
- $('#noticePanel').modal('hide');//弹窗消失
- $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
- }, function (response) {
- toaster.pop("error", "提示", "保存失败!");
- });
- };
- $scope.startTimeOpen = false;
- $scope.endTimeOpen = false;
- $scope.timeChange = function (newDate, oldDate) {
- $scope.startTimeOpen = false;
- $scope.endTimeOpen = false;
- };
- }]);
|