我目前正在尝试使用 Jquery 的 Datatable 插件创建一个完全可修改的数据表。完全可修改的表是指用户将能够在表上编辑、删除、更新、添加列。根据示例,目前我正在尝试以下 javascript 代码:
function var_dump(obj) {
if(typeof obj == "object") {
return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
} else {
return "Type: "+typeof(obj)+"\nValue: "+obj;
}
}//end function var_dump
var oTable;
var giRedraw = false;
var giCount=11;
$(document).ready(function() {
/* Add a click handler to the rows - this could be used as a callback */
$("#example tbody").click(function(event) {
$(oTable.fnSettings().aoData).each(function (){
$(this.nTr).removeClass('row_selected');
});
$(event.target.parentNode).addClass('row_selected');
});
/* Add a click handler for the delete row */
$('#delete').click( function() {
var anSelected = fnGetSelected( oTable );
var iRow = oTable.fnGetPosition( anSelected[0] );
oTable.fnDeleteRow( iRow );
} );
oTable = $('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "lib/server_processing.php",
"fnDrawCallback": function () {
$('#example tbody td').editable( 'lib/editable_ajax.php', {
"callback": function( sValue, y ) {
alert(sValue);
/* Redraw the table from the new data on the server */
oTable.fnDraw();
},
"height": "14px"
} );
}
} );
} );
function fnClickAddRow() {
oTable.fnAddData( [
giCount+".1",
giCount+".2",
giCount+".3",
giCount+".4",
giCount+".5", ]
);
giCount++;
}
/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
var aReturn = new Array();
var aTrs = oTableLocal.fnGetNodes();
for ( var i=0 ; i<aTrs.length ; i++ )
{
if ( $(aTrs[i]).hasClass('row_selected') )
{
aReturn.push( aTrs[i] );
}
}
return aReturn;
}
这在用户界面中工作正常,但我不知道如何将列信息发送到服务器端。好的,我可以将列的新值发送到 server_ajax 脚本,但是如何发送有关该列的其他相关信息,例如数据库中已编辑列的唯一 ID。