我個人被玩弄獲取數據表刪除功能,爲我的一個項目工作。
我發現的是我需要在文檔的頭部包含jquery-ui.js
腳本。
我還發現你需要像這樣格式化你的表格行。 <tr id="1">
,這樣datatables可以自動從選定的行中獲取正確的id#並將其傳遞到您的php頁面以供進一步處理。
數據表可以通過使用DT_RowId
特殊屬性自動爲每個錶行添加一個id。
如果您已經設置了數據表以使用服務器端實現,那麼您可以在從服務器返回的JSON數據中包含DT_RowId
以響應ajax請求。
例如JSON格式:
{
"sEcho": 1,
"iTotalRecords": "1",
"iTotalDisplayRecords": "1",
"aaData": [
{
"DT_RowId": "1",
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"version": "4",
"grade": "X"
}
]
}
這裏是我的示例代碼實體模型。服務器端實現不包含在這個例子中我只是使用示例中帶有數據表的示例源objects.txt。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="datatables/media/js/jquery.dataTables.js"></script>
<script src="examples/examples_support/jquery.jeditable.js"></script>
<script src="http://jquery-datatables-editable.googlecode.com/svn/trunk/media/js/jquery-ui.js"></script>
<script src="datatables/media/js/jquery.datatables.editable.js"></script>
<style type="text/css">
@import "datatables/media/css/demo_table.css";
@import "datatables/media/css/demo_table_jui.css";
@import "examples/examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>
<script>
$(document).ready(function() {
var anOpen = [];
var sImageUrl = "/datatables/examples/examples_support/";
var oTable = $('#example').dataTable({
"sPaginationType": "full_numbers",
"bJQueryUI": true,
"bProcessing": true,
"sAjaxSource": "/datatables/examples/ajax/sources/objects.txt",
"aoColumns":
[
{
"mDataProp": null,
"sClass": "control center",
"sDefaultContent": '<img src="'+sImageUrl+'details_open.png'+'">'
},
{ "mDataProp": "engine" },
{ "mDataProp": "browser" },
{ "mDataProp": "grade" }
]
}).makeEditable({
sDeleteURL: "DeleteData.php",
//Converts to JQuery UI button (full list of options on http://jqueryui.com/demos/button)
oDeleteRowButtonOptions:
{
label: "Remove",
icons: {primary:'ui-icon-trash'}
},
//Additional Paramaters can be sent for processing...
oDeleteParameters:
{
action:"delete_dt_projects_record",
dtSecurity:"9999999999"
}
});
//For Expandable Collapsible Rows
$('#example td.control').live('click', function() {
var nTr = this.parentNode;
//var projID = $(this).closest("td").next().text();
//console.log(projID);
//console.log(anOpen);
//console.log(nTr);
var i = $.inArray(nTr, anOpen);
//console.log(i);
if (i === -1)
{
$('img', this).attr('src', sImageUrl+"details_close.png");
var nDetailsRow = oTable.fnOpen(nTr, fnFormatDetails(oTable, nTr), 'details');
$('div.innerDetails', nDetailsRow).slideDown();
anOpen.push(nTr);
}
else
{
$('img', this).attr('src', sImageUrl+"details_open.png");
$('div.innerDetails', $(nTr).next()[0]).slideUp(function()
{
oTable.fnClose(nTr);
anOpen.splice(i, 1);
});
}
});
function fnFormatDetails(oTable, nTr)
{
var oData = oTable.fnGetData(nTr);
var sOut =
'<div class="innerDetails">'+
'<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
'<tr><td>Rendering engine:</td><td>'+oData.engine+'</td></tr>'+
'<tr><td>Browser:</td><td>'+oData.browser+'</td></tr>'+
'<tr><td>Platform:</td><td>'+oData.platform+'</td></tr>'+
'<tr><td>Version:</td><td>'+oData.version+'</td></tr>'+
'<tr><td>Grade:</td><td>'+oData.grade+'</td></tr>'+
'</table>'+
'</div>';
return sOut;
}
});
</script>
</head>
<body>
<div style="width:900px">
<div class="add_delete_toolbar">
<button id="btnDeleteRow">Delete</button>
</div>
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<thead>
<tr>
<th></th>
<th>Rendering engine</th>
<th>Browser</th>
<th>CSS grade</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</body>
</html>
注意您可能必須將腳本和樣式URL路徑更改爲您所在的位置才能看到工作示例。
更新:您還需要在HTML源代碼中的任意位置手動包含此項。
<button id="btnDeleteRow">Delete</button>