123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- app.controller('devCommandCtrl', ['$scope', '$http', '$timeout', 'toaster', function ($scope, $http, $timeout, toaster) {
- $scope.gridOptions = {
- data: 'myData',
- showGridFooter: true, //是否显示grid footer
- //-------- 分页属性 ----------------
- paginationPageSizes: [10, 20, 50, 100], //每页显示个数可选项
- paginationCurrentPage: 1, //当前页码
- paginationPageSize: 10, //每页显示个数
- totalItems: 0,// 总数量
- columnDefs: [],
- //---------------api---------------------
- onRegisterApi: function (gridApi) {
- $scope.gridApi = gridApi;
- gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
- if ($scope.setPagingData) {
- $scope.getPagedDataAsync(newPage, pageSize);
- }
- });
- }
- };
- //编辑弹窗需要的数据
- $scope.dialogName = "编辑";
- $scope.dialogData = {};
- //查询条件
- var condition = $scope.condition = {
- searchKey: "",
- searchType: {value: "*", text: "命令信息"},
- searchTypeList: [
- {value: "*", text: "命令信息"},
- {value: "devTypeCode", text: "驱动编码"},
- ],
- };
- //事件
- $scope.event = {
- search: function () {
- $scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize);
- }
- };
- $scope.config = {
- driverCodeList: []
- };
- function setColumnDefs() {
- $scope.gridOptions.columnDefs = [
- {field: 'devTypeCode', displayName: '驱动编码', minWidth: 120},
- {field: 'cmd', displayName: '功能码', minWidth: 120},
- {field: 'description', displayName: '描述', minWidth: 120},
- {field: 'common', displayName: '是否通用', minWidth: 100},
- {field: 'active', displayName: '是否启用', minWidth: 100},
- {
- field: 'params', displayName: '参数', minWidth: 380,
- enableFiltering: false,
- enableSorting: false,
- enableHiding: false,//禁止在列选择器中隐藏
- enableColumnMenu: false,// 是否显示列头部菜单按钮
- cellTemplate: '<span class="grid-button" ng-repeat=" item in row.entity.params" ><button class="btn btn-rounded btn-sm btn-info ">{{item.description}}</button></span>'
- }
- ];
- var fields = $scope.gridOptions.columnDefs;
- for (var index in fields) {
- var item = fields[index];
- if (item && item['minWidth'] == null) {
- item['minWidth'] = 80;
- }
- }
- }
- $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
- }
- // 查询类型
- if (condition.searchType.value != "") {
- params.field = condition.searchType.value;
- }
- $http.get('/superadmin/getDeviceCommands', {
- params: params
- }).then(function (data) {
- data = data.data
- $scope.setPagingData(data, curPage, pageSize);
- }).catch(function (data) {
- toaster.pop("error", "提示", "获取列表失败");
- });
- };
- function initDataGrid() {
- //首次加载表格
- $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
- }
- setColumnDefs();
- initDataGrid();
- //添加
- $scope.addDevCommand = function () {
- //重置表单状态
- $scope.devCommandForm.$setPristine();
- $scope.devCommandForm.$setUntouched();
- $scope.dialogName = "添加";
- $scope.dialogData = {
- common:true,
- active:true,
- "topic_pre":"smartBox",
- "role":"supermanager",
- };
- $("#devCommandForm").modal();
- };
- //编辑
- $scope.editDevCommand = function () {
- //重置表单状态
- $scope.devCommandForm.$setPristine();
- $scope.devCommandForm.$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);//需要深度克隆
- //设备驱动编码转换
- var driverCodeSelect = {};
- for (var index2 in $scope.config.driverCodeList) {
- var item2 = $scope.config.driverCodeList[index2];
- var value2 = item2.code;
- if ($scope.dialogData.devTypeCode == value2) {
- driverCodeSelect = item2;
- break;
- }
- }
- $scope.dialogData.driverCodeSelect = driverCodeSelect;
- $("#devCommandForm").modal();
- };
- //删除编码
- $scope.deleteDevCommand = 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: '/superadmin/deleteDeviceCommand',
- data: {ids: ids}
- }).then(function (response) {
- initDataGrid();
- }, function (response) {
- toaster.pop("error", "提示", "删除失败!");
- });
- }
- },
- }
- });
- };
- //提交表单保存
- $scope.saveDevTypeData = function () {
- if ($scope.devCommandForm.$invalid) {
- return
- }
- var saveData = $.extend(true,{},$scope.dialogData)
- var url = "";
- if (saveData.id == null) {
- url = "/superadmin/addDeviceCommand";
- } else {
- url = "/superadmin/editDeviceCommand";
- }
- if (!saveData.common) {
- if (!saveData.driverCodeSelect || !saveData.driverCodeSelect.code) {
- toaster.pop("info", "提示", "请填写驱动编码");
- return
- }
- // 转换驱动编码格式
- saveData.devTypeCode = saveData.driverCodeSelect.code;
- delete saveData.driverCodeSelect;
- }
- $http({
- method: 'POST',
- url: url,
- data: saveData
- }).then(function (response) {
- //保存成功 弹窗消失
- $('#devCommandForm').modal('hide');
- $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
- }, function (response) {
- toaster.pop("error", "提示", "保存失败!");
- });
- };
- //获取驱动编码列表
- $http.get('/device/driver/code/list', {
- params: {
- pageSize: 1000,//暂时可以获取1000个设备类型
- pageIndex: 1
- }
- }).then(function (data) {
- data = data.data
- var list = data.payload.dataList;
- // 精简数据结构
- for (var index in list) {
- var item = list[index];
- $scope.config.driverCodeList.push(item);
- }
- }).catch(function (data) {
- toaster.pop("error", "提示", "获取列表失败");
- });
- $scope.deleteParam = function (index) {
- var params = $scope.dialogData.params;
- if (params[index]) {
- params.splice(index, 1);
- }
- };
- $scope.addParam = function () {
- var params = $scope.dialogData.params;
- if (!$.isArray(params)) {
- params = $scope.dialogData.params = [];
- }
- var length = params.length;
- var packageItem = {
- "description": "",
- "key": "",
- "allow_change": false,
- "default": ""
- };
- params.push(packageItem);
- };
- }]);
|