Datatables 1.10 - Pass a DOM field value to server side processing page

Datatables 1.10 - Pass a DOM field value to server side processing page

nickatfltnickatflt Posts: 4Questions: 2Answers: 0
edited July 2015 in Free community support

I have a DataTable setup like this that's processed in a ColdFusion CFC:

var user_table = $(this).find("table.permissions_user_table").DataTable({
    "info":false,
    "lengthChange":false,
    "processing":false,
    "serverSide":true,
    "ajax":{
        "url": "/cfc/intranet.cfc",
        "data":{
            "method":"processDatatables",
            "element_type":"entity",
            "element_id":"14",
            "selected_users":getUserList()
        },
        "type":"post"
    },
    "columns": [
        { "data": "id" },
        { "data": "name" },
        { "data": "location" }
    ],
    "order": [[0, 'asc'], [1, 'asc']]
});

The "getUserList()" is simply this:

function getUserList(){
    alert("fire!");
    return $("#user_list_field").val();
}

On initialization, the getUserList function runs, "fire!" pops up, and the DataTable is correctly created. However, when the table is redrawn, even if the user_list_field has a different value, the DataTable is redrawn with the original value of "selected_users." It doesn't fire getUserList(), but it still goes out to the server side code and processes the search or reorder correctly.

Is there a special way to reload the ajax.data from the DOM before processing the redraw?

Answers

  • AshbjornAshbjorn Posts: 55Questions: 2Answers: 16

    Would it be possible to create and assign a function call to the data parameter instead?

            "data": function(d) {
                return $("yourFormWithValues").serialize();
            },
    

    This should in theory defer the execution of that function and provide the functionality you request, but limitations may apply as there is no way right now to validate your current form and its values. The assumption here is that $("yourFormWithValues") contains all the elements you wish to pass, so if you are just concerned with the selected_users then you can adjust the return statement accordingly.

    Hope this helps,

  • nickatfltnickatflt Posts: 4Questions: 2Answers: 0

    I ended up finding a solution on StackOverflow. I had to set the selected_users list to a global variable and use a function to refresh/pass it. So, I got rid of getUserList(), defined a global variable var global_user_list = ""; above my $(document).ready() function, and replaced line 12 above with this:

    "selected_users":function () { 
        global_user_list = $("#user_list_field").val();
        return global_user_list;
    }
    
This discussion has been closed.