jQuery增删改查

var curEditRow = -1, curNewRow = -1, curSelRow = -1; 
var rowData = {}; 

$(function () { 
/********************************* 初始化日期 ***********************************/ 
$("input[name='datepicker']").datepicker({ 
changeMonth: true, 
changeYear: true, 
dateFormat: 'yy-mm-dd', 
regional: ['zh-CN'] 
}); 
initDatePicker("startTime", -30); 
initDatePicker("endTime", 0); 


$('#dialog').dialog({ 
title: "查看数据库日志信息", 
autoOpen: false, 
width: 300, 
height: 350, 
top: 100, 
autoOpen: false, 
modal: true, 
buttons: { 
"关闭": function () { 
$(this).dialog("close"); 


}); 

listDBLog_Init(); 
}); 

function getParams() { 
var params = {startTime: '', endTime: '' }; 
params.startTime = $("#startTime").val(); 
params.endTime = $("#endTime").val(); 
return params; 
}

//查询按钮响应事件 
function tableQueryClick() { 
CompareDate("#startTime", "#endTime", true); 
var tableName = "#list"; 
if (!$(tableName).getGridParam("url")) { 
listDBLog_Init(); 
return; 


var params = getParams(); 
$(tableName).appendPostData(params); 
$(tableName).trigger("reloadGrid"); 



function listDBLog_Init() { 
$("#list").jqGrid({ 
url: 'ListDBLog', 
datatype: 'json', 
mtype: 'GET', 
colNames: ['Id', '用户', '错误类型', '控制器名称', '方法名称', '结果', '时间'], 
colModel: [{ name: 'Id', index: 'Id', sortable: false, editable: false, width: 50, hidden: false }, 
{ name: 'UserId', index: 'UserId', sortable: false, editable: true, search: false, width: 80 }, 
{ name: 'Type', index: 'Type', sortable: false, editable: true, search: false, width: 80 }, 
{ name: 'ControllerName', index: 'ControllerName', sortable: false, editable: true, search: false, width: 80 }, 
{ name: 'ActionName', index: 'ActionName', sortable: false, editable: true, search: false, width: 170 }, 
{ name: 'Result', index: 'Result', sortable: false, editable: false, search: false, width: 80,hidden:false }, 
{ name: 'Date', index: 'Date', sortable: false, editable: false, search: false, width: 100 }

], 
pager: $("#pager"), 
autowidth: true, 
shrinkToFit: true, 
forceFit: true, 
height: '100%', 
rowNum: 30, 
rowList: [30, 50, 80], 
altRows: true, 
formEdit: true, 
modal: true, 
drag: true, 
sortname: '', 
sortorder: 'desc', 
viewrecords: true, 
multiselect: false, 
imgpath: GRID_IMG_PATH, 
caption: '数据库日志报表', 
postData: getParams(), 
ondblClickRow: function (id) { 
var gr = $(this).getRowData(id); 
$("#UserId").html(gr.UserId); 
$("#Type").html(gr.Type); 
$("#ControllerName").html(gr.ControllerName); 
$("#ActionName").html(gr.ActionName); 
$("#Result").html(gr.Result); 
$("#Date").html(gr.Date); 
$('#dialog').dialog("open"); 
}, 
loadComplete: function () { 
curEditRow = -1; 
curSelRow = -1; 
curNewRow = -1; 
var $table = $("#list"); 
var ids = $table.getDataIDs(); 
$table.jqGrid('setSelection', ids[0]); 
}, 
onSelectRow: function (id) { 
if (id == curNewRow) { 
curSelRow = id; 
return; //新增加行的选中事件 

var $table = $("#list");

if (curNewRow != -1) { 
if (!confirm("当前存在新增而未提交的行?确认放弃提交吗?")) { 
return; 
} else { 
cancelNewRow(); 


if (curEditRow != -1) { 
if (!confirm("存在编辑未提交的行?确认放弃提交吗?")) { 
return; 


curEditRow = -1; 
curNewRow = -1; 
curSelRow = id; 
disableRowEdit(); //禁止提交


}).navGrid("#pager", { 
edit: false, add: false, del: false, search: false 
}) ; 



//"新增"事件 
function insertNewRow() { 

if (curNewRow != -1) { 
//$.weeboxs.open('当前存在新增而未提交的行,请选提交.', { title: '提示<span style="font-weight:lighter">(3秒后自动关闭)<span>', type: 'alert', timeout: 3 }); 
alert("当前存在新增而未提交的行,请选提交"); 
return; 

if (curEditRow != -1) { 
if (!confirm("存在编辑未提交的行?确认放弃提交吗?")) { 
return; 

curEditRow = -1; 


curNewRow = "new_10000"; 
var tableName = "#list"; 
var $table = $(tableName); 
initRowData(); //初始化行数据值 
$table.addRowData(curNewRow, rowData); 

$table.jqGrid('editRow', curNewRow); 
$(tableName).jqGrid('setSelection', curNewRow); 
enableRowEdit(); 
}

//"修改"事件 
function editSelRow() { 
var tableName = "#list"; 

if (curEditRow != -1) { 
cancelEditRow(tableName, curEditRow); 

if (curSelRow == -1) { 
//$.weeboxs.open('您未选择编辑行.', { title: '提示<span style="font-weight:lighter">(3秒后自动关闭)<span>', type: 'alert', timeout: 3 }); 
alert("您没有选择编辑行"); 
return; 

curEditRow = curSelRow; 
enableRowEdit(); 
$(tableName).editRow(curEditRow); 
//$(tableName).setCell(curEditRow, 'editHtml', editHtml); 
initRowData(); //初始化行数据值 

//"删除"事件 
function delSelRow() { 
if (curSelRow == -1) { 
alert("未选择删除行!"); 
//$.weeboxs.open('您尚未选择删除行.', { title: '提示<span style="font-weight:lighter">(3秒后自动关闭)<span>', type: 'alert', timeout: 3 }); 
return; 

if (curEditRow != -1 || curNewRow != -1) { 
if (!confirm("当前存在编辑未提交的行?确认放弃编辑吗?")) { 
return; 

}

if (confirm("确定删除被选择的项吗?")) { 
$.ajax({ 
url: 'DeleteDblogItem', 
data: "Id=" + $("#list").getCell(curSelRow, "Id") + "&", 
type: "POST", 
dataType: "json", 
success: function (jdata, stat) { 
//$.weeboxs.open(jdata.info, { title: '提示<span style="font-weight:lighter">(3秒后自动关闭)<span>', type: 'alert', timeout: 3 }); 
alert(jdata.info); 
tableQueryClick(); //重新查询 
return; 

}); 




function cancelNewRow() { 
$('#list').jqGrid('delRowData', curNewRow); 
// $("#" + curNewRow + "_UserID").unbind("blur"); //取消绑定 
curNewRow = -1; 
curSelRow = -1; 

function cancelEditRow() { 
$('#list').jqGrid('restoreRow', curEditRow); 
curEditRow = -1; 

function initRowData() { 
rowData = null; 
rowData = { Id: '', UserId: '', Type: '', ControllerName: '',ActionName:'',Result:'',Date:''}; 

function enableRowEdit() { 
$('#btSaveAll').attr('disabled', false); 
$('#btCancelAll').attr('disabled', false); 

function disableRowEdit() { 
$('#btSaveAll').attr('disabled', true); 
$('#btCancelAll').attr('disabled', true); 
}

//保存按钮事件 
function SaveAll() { 
if (curNewRow != -1) { 
saveNewRowChecked(); 
} else if (curEditRow != -1) { 
saveEditRowChecked(); 


function CancelAll() { 
if (curNewRow != -1) { 
cancelNewRow(); 
} else if (curEditRow != -1) { 
cancelEditRow(); 



function saveNewRowChecked() { 
var tableName = "#list"; 
var str; 
//取编辑数据 
for (var index in rowData) { 
if (index.toString() == 'undefined' ) { 
continue; 

str = $(tableName).find("#" + curNewRow + "_" + index).val(); 
if (str != undefined) { 
rowData[index] = Trim(str); 


}

str = jQuery.param(rowData); 
$.ajax({ 
url: 'AddDblog', 
data: str, 
type: "POST", 
dataType: "json", 
success: function (jdata, stat) { 
//$.weeboxs.open(jdata.info, { title: '提示<span style="font-weight:lighter">(3秒后自动关闭)<span>', type: 'alert', timeout: 3 }); 
alert(jdata.info); 
if (jdata.result == "success") { 
//$("#" + curEditRow + "_UserId").unbind("blur"); //取消绑定 
tableQueryClick(); //重新查询 
} else { 
enableRowEdit(); 

return; 
}, 
error: function (XMLHttpRequest, textStatus, errorThrown) { 
enableRowEdit(); 

}); 


function saveEditRowChecked() { 
var tableName = "#list"; 
var str; 
//取编辑数据 
for (var index in rowData) { 
if (index.toString() == 'undefined') { 
continue; 

str = $(tableName).find("#" + curEditRow + "_" + index).val(); 
if (str != undefined) { 
rowData[index] = Trim(str); 




str = jQuery.param(rowData); 
$.ajax({ 
url: 'UpdateDblog', 
data: "Id=" + $("#list").getCell(curSelRow, "Id") + "&" + str, 
type: "POST", 

dataType: "json", 
success: function (jdata, stat) { 
//$.weeboxs.open(jdata.info, { title: '提示<span style="font-weight:lighter">(3秒后自动关闭)<span>', type: 'alert', timeout: 3 }); 
alert(jdata.info); 
if (jdata.result == "success") { 
//$("#" + curEditRow + "_UserID").unbind("blur"); //取消绑定 
tableQueryClick(); //重新查询 

curEditRow = -1; 
disableRowEdit(); 
return; 
}, 
error: function (XMLHttpRequest, textStatus, errorThrown) { 
enableRowEdit(); 

}); 
}

你可能感兴趣的:(jquery)