如何通过ajax将变量发送到服务器(Java),然后加载带有响应的DataTable

How to send a variable to the server (Java) via ajax, then load a DataTable with the response

本文关键字:加载 然后 DataTable 响应 Java ajax 何通过 变量 服务器      更新时间:2023-09-26

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
                } ); },