alarmgroup.js 7.31 KB
//页面初始化
$(function() {
	refreshAlarmGroupTable();
});

// 刷新表格数据
function refreshAlarmGroupTable() {
	$("#alarmGroupTable").table(
			{
				columnAutoWidth : false,
				url : contextPath + "/alarmGroup/getAlarmGroup?groupName=" + $("#alarmGroup_search").val().trim() + "&usersName=" + $("#userName_search").val().trim(),
				striped : true,
				pagination : true,
				pageSize : 10,
				loadFilter : function(data) {
					return defaultLoadFilter(data);
				},
				columns : [
						{
							title : "索引",
							field : "id",
							width : "60px"
						},
						{
							title : "报警组名称",
							field : "groupName",
							width : "15%"
						},
						{
							title : "登陆用户名",
							field : "usersName",
							width : "65%"
						},
						{
							title : "说明",
							field : "remark",
							width : "20%"
						},
						{
							title : "创建时间",
							field : "createTime",
							width : "160px",
							formatter : function(value, rowData, rowIndex) {
								return rowData.createTime.substring(0,rowData.createTime.length - 2);
							}
						},
						{
							title : "最后一次更新时间",
							field : "updateTime",
							width : "160px",
							formatter : function(value, rowData, rowIndex) {
								return rowData.updateTime.substring(0,rowData.updateTime.length - 2);
							}
						},
						{
							title : "操作",
							width : "100px",
							formatter : function(value, rowData, rowIndex) {
								var div = $("<div>");
								$(
										"<button onclick=\"editAlarmGroup(\'"
												+ rowData.id + "\',\'"
												+ rowData.groupName + "\',\'"
												+ rowData.usersName + "\',\'"
												+ rowData.remark + "\')\">").addClass(
										"btn btn-xs btn-success").html("修改")
										.appendTo(div);
								div.append("&nbsp;");
								$(
										"<button onclick=\"deleteAlarmGroup(\'"
												+ rowData.id + "\')\">")
										.addClass("btn btn-xs btn-danger")
										.html("删除").appendTo(div);
								return div;
							}
						} ]
			});
}

//报警组名称、说明、用户名 只对或可写
function canWrite(isRead){
	if(isRead){
		$("input[name='alarmType']").removeAttr("readonly");
	}
	else{
		$("input[name='alarmType']").attr("readonly","readonly");
	}
}

//新增按钮,如果处于修改,则屏蔽改按钮
function isEdit(isEdit){
	if(isEdit){
		$("#btnNew").attr("class","btn btn-warning");
		$("#btnNew").attr("disabled","disabled");
		
		$("button[name='btnIsEditStatus']").attr("class","btn btn-primary");
		$("button[name='btnIsEditStatus']").removeAttr("disabled");
	}else{
		$("#btnNew").attr("class","btn btn-primary");
		$("#btnNew").removeAttr("disabled");
		
		$("button[name='btnIsEditStatus']").attr("class","btn btn-warning");
		$("button[name='btnIsEditStatus']").attr("disabled","disabled");
	}
}

//0:新增数据 大于0:修改
var isHaveId = 0;

//搜索
function btnSearch(){
	refreshAlarmGroupTable();
}

//编辑取消
function btnCancel(){
	isHaveId = 0;
	$("#alarmGroup").val("");
	$("#usersName").val("");
	$("#remark").val("");
	canWrite(false);
	isEdit(false);
}

//新增
function btnNew(){
	isHaveId = 0;
	canWrite(true);
	isEdit(true);
}

//保存
function btnSave(){
	if($("#alarmGroup").attr("readonly")=="readonly"){
		return;
	}
	var alarmGroup = $("#alarmGroup").val();
	if($.trim(alarmGroup) == ""){
		alert("报警组名称必填!");
		document.getElementById("alarmGroup").focus();
		return;
	}
	var usersName = $("#usersName").val();
	if($.trim(usersName) == ""){
		alert("用户必选!");
		document.getElementById("usersName").focus();
		return;
	}
	var remark = $("#remark").val();
	var params = {};
	params.groupName = alarmGroup;
	params.usersName = usersName;
	params.remark = remark;
	params.id = isHaveId;
	$.ajax({
        url: '/alarmGroup/addOrUpdateAlarmGroup',
        type: 'POST',
        data: params,
        dataType: 'json',
        success: function (data) {
            if (!data || data.code != 200) {
            	alert('系统异常:'+data.message);
            }else{
            	alert('保存成功');
            	refreshAlarmGroupTable();
            	
            	btnCancel();
            }
        },
        error: function (data) {
        	alert('系统异常');
        }
    });
}

//修改
//id:索引
//groupName:报警组名称
//usersName:用户
//remark:说明
function editAlarmGroup(id, groupName, usersName, remark){
	isHaveId = id;
	$("#alarmGroup").val(groupName);
	$("#usersName").val(usersName);
	$("#remark").val(remark);
	canWrite(true);
	isEdit(true);
}

//删除数据
function deleteAlarmGroup(id){
	if(confirm("请谨慎删除当前报警组!")){
		if(confirm("是否确定要删除当前报警组!")){
			$.ajax({
		        url: '/alarmGroup/deleteAlarmGroupById?id='+id,
		        type: 'POST',
		        dataType: 'json',
		        success: function (data) {
		            if (!data || data.code != 200) {
		            	alert('删除失败:'+data.message);
		            }else{
		            	alert('删除成功!');
	            		refreshAlarmGroupTable();
		            }
		        },
		        error: function (data) {
		        	alert('系统异常');
		        }
		    });
		}
	}
}

//搜索用户
function btnQueryUser(){
	onLoadUsers($("#usersName").val());
	$("#myModalLabel").text("请选择用户");
	$("#hostGroupForm #messageAlert").hide();
    if (isHaveId == 0) {//新增页面
    }else {
    }
    $("#myModal").modal('show');
}

//加载用户数据
function onLoadUsers(users){
	$("#usersTable").table({
        columnAutoWidth: false,
        url: contextPath + "/manage/getAllUser",
        striped: true,
        loadFilter: function (data) {
            return defaultLoadFilter(data);
        },
        columns: [
        	{
                title: "",
                width:"30px",
                field: "name",
                formatter: function (value, rowData, rowIndex) {
                	var check;
                	if(users != null){
                		var userList = users.split(',');
                		for(var i=0;i<userList.length;i++){
                			 if(userList[i] == rowData.name){
                				 check = "checked";
                				 break;
                			 }
                		}
                	}
                	var div = $("<div>");
                    //复选框
                	if(check == "checked"){
                		$("<input name='userCheckType' type='checkbox' checked='"+check+"' value='"+value+"'>").addClass("ckbox-default").appendTo(div);
                	}else{
                		$("<input name='userCheckType' type='checkbox' value='"+value+"'>").addClass("ckbox-default").appendTo(div);
                	}
                    return div;
                }
            },
        	{
	            title: "登陆用户名",
	            field: "name",
	            width: "200px"
        	},
	        {
	            title: "中文名",
	            field: "cname",
	            width: "100px"
	        },
	        {
	            title: "手机号",
	            field: "mobile",
	            width: "100px"
	        }]
	    });
}

//批量选择用户
function chooseUsers(){
	var listUsers = "";
	$("#usersTable input[type='checkbox']:checked").each(function(){
		listUsers += $(this).val() + ",";
	});
	if(listUsers.length > 0){
		listUsers = listUsers.substring(0,listUsers.length - 1);
	}
	$("#usersName").val(listUsers);
	$("#myModal").modal('hide');
}