如何通过ajax将变量发送到服务器(Java),然后加载带有响应的DataTable
How to send a variable to the server (Java) via ajax, then load a DataTable with the response
JavaScript
$(document).ready(function() {
console.log("get tasks");
$('#tblTask').dataTable({
<!-- Retrieve a static json file, you could also have a URL to a controller method. -->
"sAjaxSource" : "/getTasks",
"sAjaxDataProp": "",
<!-- Indicate to dataTable what the field names are we want, in the order we want them in the table. -->
"aoColumns": [
{"data": "taskID",
"visible": false},
{"data": "task_Runbook_ID",
"visible": false},
{"data": "taskNumber"},
{"data": "taskDependencies"},
{"data": "taskStatus"},
{"data": "taskDescription"},
{"data": "duration"},
{"data": "ownerName"},
{"data": "planStartTime"},
{"data": "planEndTime"},
{"data": "actualStartTime"},
{"data": "actualEndTime"},
{"data": "comments"}
]
});
});
表
<table class="table" id="tblTask">
<thead>
<tr>
<th style="display: none">ID</th>
<th style="display: none">Runbook ID</th>
<th width="50px">Task Number</th>
<th width="70px">Dependency</th>
<th width="50px">Status</th>
<th width="100px">Description</th>
<th width="30px">Duration</th>
<th width="70px">Owner</th>
<th width="50px">Planned Start</th>
<th width="50px">Planned End</th>
<th width="50px">Actual Start</th>
<th width="50px">Actual End</th>
<th width="50px">Comments</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Java
@RequestMapping(value = "/getTasks")
public @ResponseBody Iterable<Task> getTasks(Model model) {
// System.out.println("RBID!!!" + request.getParameter("rbID"));
List<Task> tasks = taskRepository.findByTaskRunbookID(3); //3 is here until we can get id from gui
return tasks;
}
因此,当加载页面时,我将加载一个DataTable,其中包含从服务器检索的一堆任务,服务器将从数据库中检索这些任务。然而,我想在发出请求时从客户端发回一个javascript变量,这样该方法就可以根据ID检索任务列表。
这段代码很有效,因为它只加载所有任务。现在,我想根据从客户端发送的变量加载特定的任务。
我想要的伪:
var rbID = something;
$('#tblTask').dataTable({
"sAjaxSource" : "/getTasks",
"data" : {"rbID" : rbID }, //this is sent to the server, where I query a list of tasks where taskID is equal to this rbID.
"sAjaxDataProp": "",
"aoColumns": [.....] // Then the response is a JSON object, which I display in the DataTable like so.
我想明白了。
"sAjaxSource" : "/getTasks",
"fnServerData": function ( sSource, aoData, fnCallback ) {
/* Add some data to send to the source */
aoData.push( { "name": "rbID", "value": rbID } );
$.ajax( {
"dataType": 'json',
"url": sSource,
"data": aoData,
"success": fnCallback
} ); },
相关文章:
- AngularJS加载JSON数据,然后从中解析/加载HTML
- 先在Angular中加载配置文件,然后再加载其他文件
- 加载文件,然后调用回调函数
- 如何更改javascript的一个变量,然后运行;“加载”;而无需重新加载页面
- 下拉重新加载页面,然后检索值
- 先加载特定的图像,然后再加载页面的其余部分
- 加载页面,然后在页面加载时执行 javascript
- Angular js,在选择元素上,我想 POST 数据以将其保存在数据库中,然后我想使用 PUT 更新它而无需重新加载
- 使用 jQuery 加载 JSON,然后将其显示给查看器
- 淡出加载然后淡入不起作用
- 可以进行现场重新加载,然后触发点击功能
- 在RadWindow中加载新页面,然后从加载的窗体中关闭窗口
- 加载在JSBN中创建的RSA公钥,然后加密消息
- 加载时可以单击按钮#1,然后需要单击按钮#2两次.按钮#1也需要点击两次
- 地图,rails 4.2,javascript,鼠标悬停,只工作一次(或两次).然后在重新加载之后
- 如何在jQuery中进行页面加载,然后点击
- removeChild,然后再次添加以前删除的Child以重新加载并清理它
- jQuery UI#Accordion加载然后fadeIN..怎样
- 中间的页脚加载然后粘在底部
- 加载然后换行,但是换行在jQuery中不起作用