客戶提出對報表填報值進行過的修改信息希望能保存到數據庫中,形成記錄以備查詢操作記錄。
原理分析:
填報值 進行 修改時 只需通過異步方式觸發 將相關數據保存入庫即可。
這裏我們可以分別用_bindingEditor(),_cellValueChanged,以及ajax 實現上面的需求。
操作步驟:
第一、 利用光標定位事件 獲取 單元格 舊值 賦值給公共變量最終傳遞給值修改事件(在_cellValueChanged中單元格的值已經發生了改變 無法獲取到舊值)。
var tempValue="";
function _bindingEditor( cell ){
var table = _lookupTable( cell );
if( ! _submitEditor( table ) ) return; //
var editor = _lookupEditor( table, cell );
table.currEditor = editor;
_setEditorStyle(editor,cell );
tempValue+=cell.value+'&'; //舊值通過 公共變量傳遞到 觸發事件中
};
第二、 在 值修改事件中獲取舊值 最終異步保存數據
function _cellValueChanged(cell){
var strs= new Array();
strs=tempValue.split("&");
var oldValue=strs[strs.length-2];
tempValue=strs[strs.length-1]+'&';
var newValue=cell.value;
doSave(oldValue,newValue);
}
第三 簡單ajax 保存數據
function doSave(oldValue,newValue){
$.ajax({
type: "POST",
url: "save.jsp",
data: "oldValue="+oldValue+"&newValue="+newValue,
success: function(){
alert("數據已保存!");
}
});
}
後臺業務邏輯由 save.jsp完成。
……
Statement stmt =conn.createStatement();
//要執行的sql語句
String sql = "INSERT INTO updatelog (id,username, oldvalue,newvalue,updatetime) VALUES ('"+id+"','"+name+"','"+oldValue+"','"+newValue+"','"+date1+"') ";
System.out.println("sql="+sql);
int t = stmt.executeUpdate(sql);
……