app.controller('sysAdCtrl', ['$scope', '$http', '$timeout', 'uiGridConstants', 'i18nService', 'toaster', function ($scope, $http, $timeout, uiGridConstants, i18nService, toaster) { i18nService.setCurrentLang("zh-cn"); moment.locale('zh-cn'); $scope.startTimeOpen = false; $scope.endTimeOpen = false; $scope.timeChange = function (newDate, oldDate) { $scope.startTimeOpen = false; $scope.endTimeOpen = false; }; $scope.gridOptions = { data: 'myData', showGridFooter: true, //是否显示grid footer //-------- 分页属性 ---------------- paginationPageSizes: [10, 20, 50, 100], //每页显示个数可选项 paginationCurrentPage: 1, //当前页码 paginationPageSize: 10, //每页显示个数 totalItems: 0,// 总数量 useExternalPagination: true,//是否使用分页按钮 //过滤 enableFiltering: false, 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 = { startTime: moment().format("YYYY-MM-DD"), endTime: moment().format("YYYY-MM-DD"), searchKey: "" }; function setColumnDefs() { $scope.gridOptions.columnDefs = [ {field: 'createdTime', displayName: '时间', width: 150,}, {field: 'title', displayName: '标题', width: 240,}, {field: 'content', displayName: '内容'}, ]; 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 } params.startTime = condition.startTime params.endTime = condition.endTime $http.get('/ad/adwords', { params: params }).then(function (data) { data = data.data $scope.setPagingData(data, curPage, pageSize); }).catch(function (data) { toaster.pop("error", "提示", "获取数据失败"); }); }; setColumnDefs(); initDataGrid(); //事件 $scope.event = { quickTime: function (event, passDay, passDay2) { if (passDay) { $scope.condition.startTime = moment().add(-(passDay - 1), "day").format("YYYY-MM-DD"); } passDay2 = passDay2 || 1 $scope.condition.endTime = moment().add(-(passDay2 - 1), "day").format("YYYY-MM-DD"); }, search: function () { $scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize); } }; //编辑弹窗需要的数据 $scope.dialogName = "编辑"; $scope.dialogData = {}; function getOneRow() { var rows = $scope.gridApi.selection.getSelectedRows(); if (rows.length === 0) { toaster.pop("info", "提示", "请选择数据!"); return false; } if (rows.length > 1) { toaster.pop("info", "提示", "只能选中编辑一条数据"); return false; } return rows[0] } //添加 $scope.add = function () { //重置表单状态 $scope.weblogPanel.$setPristine(); $scope.weblogPanel.$setUntouched(); $scope.dialogName = "添加"; $scope.dialogData = { sex: -1 }; $("#weblogPanel").modal(); }; //编辑 $scope.edit = function () { //重置表单状态 $scope.weblogPanel.$setPristine(); $scope.weblogPanel.$setUntouched(); $scope.dialogName = "编辑"; var entity = getOneRow(); if (!entity) { return } $scope.dialogData = $.extend(true, {}, entity); $("#weblogPanel").modal(); }; //删除特性 $scope.delete = 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: '/ad/adword/delete', data: {ids: ids} }).then(function (response) { initDataGrid(); }, function (response) { toaster.pop("error", "提示", "删除失败!"); }); } }, } }); }; //提交表单保存 $scope.saveData = function () { //表单未校验通过不能提交 if ($scope.weblogPanel.$invalid) { return; } var url = ""; if ($scope.dialogData.id == null) { url = "/ad/adword/new"; } else { url = "/ad/adword/edit"; } //深度克隆 避免操作原对象 var data = $.extend(true, {}, $scope.dialogData); $http({ method: 'POST', url: url, data: data }).then(function (response) { $('#weblogPanel').modal('hide');//弹窗消失 $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize); }, function (response) { toaster.pop("error", "提示", "保存失败!"); }); }; }]);