Data Tables and custom Search

Data Tables and custom Search

ShaneBrennanShaneBrennan Posts: 49Questions: 18Answers: 1

Hi Everyone

Sorry I don;t know a lot of the terminology, even though I've been using Javascript, AJAX, etc for a while - but getting there.

Basically I want to take modify the following:

'''
$('#lstProjects').DataTable( {
"lengthMenu": [[ 25, 50, -1], [ 25, 50, "All"]],
"pageLength": 25,
"iDisplayLength": 25,
"bProcessing": true,
"bServerSide": true,
"stateSave": true,
"sAjaxSource": "/server_side/scripts/DT_listAllProjects.php?ProjectManager=" + projectManagerID + "&ClientID=" + ClientID + "&DateRange=" + dateRangeID,
"order": [[0, 'asc']],
"columns" : [
null,
null,
null,
'''

I would like to package the parameters into a data object like a standard AJAX $.POST:

'''
function saveTheData() {
theData = new Object();
theData.projectManagerID = projectManagerID ;
theData.ClientID = ClientID;
theData.dateRangeID= dateRangeID;
$.post("SavesAndEdits/updateClientOrderDetails.php",theData,
function(data,status){
if (data != <?=$reqId?> ) {
'''

So I end up with a function that would build the data object then instruct the table to redraw.... Is this possible?

Thank you in advance for your help.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin

    I'm afraid I don't understand what you are looking to do. You want to POST your DataTables configuration object to the server?

    Allan

  • ShaneBrennanShaneBrennan Posts: 49Questions: 18Answers: 1

    Sorry Allan - I'll try to explain better.

    I have my own routine PHP for selecting and sorting records for AJAX calls - which is called using the . "sAjaxSource" line of the .datatable thingie (sorry don't know the terminology. because it's my own routine - using things the .search, like "table.search(this.value).draw();", will only work on the 10, 25 or 100 records my routine returns.

    At the moment If I use the following sAjaxSource line to select all orders for a particular client I get the correct results :
    '''
    "sAjaxSource": "/server_side/scripts/DT_BuildingsInOrder.php?OrderID=<?=$reqId?>"
    '''

    because in my code I use a $_GET to get the value sent over and can create my "subSetOf" string to ensure I only get the buildings assigned for the required OrderID:

    '''
    if (isset($_GET['OrderID'])) {
    // $subSetOf = " orderID='" . $_GET['OrderID'] . "'";
    $subSetOf = " orderID=" . $_GET['OrderID'] ;
    }
    '''

    Basically, I'm just trying to tidy things up really, I want to not include this OrderID into the POST/GET itself without including it in the URL.

    The code will work fine without it... but putting the client/order/quote/project manager codes etc. would give the user the opportunity to have too much info about the system and maybe access client/order details they have no right to access.

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin

    Slightly confused by this statement I'm afraid:

    I want to not include this OrderID into the POST/GET itself without including it in the URL.

    Doe you mean you want to include the OrderID in the submitted data, but not in the query string?

    You could include it in the POST body parameters, but that isn't really any more secure than including it in the query string, at least to anyone who knows what they are doing. You would really need some validation at the server-side to ensure that the user has access to the OrderID that they have requested.

    Allan

  • ShaneBrennanShaneBrennan Posts: 49Questions: 18Answers: 1

    Sorry for that sentence... my head was obviously not in sync with my keyboard.

    Should be "I want to include the OrderID into the POST/GET (hidden parameters) and not in the URL itself"

    That's correct.. yes move it from the URL into the POST parameters. I know it's not totally secure as it's still possible to read the headers, but the application will be using HTTPS so better than putting the info into the URL.

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin
    Answer ✓

    Got it!

    In that case use:

    ajax {
      url: "/server_side/scripts/DT_listAllProjects.php?ProjectManager=" + projectManagerID + "&DateRange=" + dateRangeID,
      type: 'post',
      data: function (d) {
        d.ClientID = ClientID;
      }
    }
    

    You'll need to make sure you read it as POST on the server-side. You could change the other parameters to POST body parameters if you want as well.

    I've used the ajax option as that is the current recommend way of setting Ajax configuration - you'll need to also add:

    $.fn.dataTable.ext.legacy.ajax = true;
    

    before you initialise the table, since your server-side script appears to be expecting the legacy parameters. More details here.

    Note that the legacy parameters will be removed by default from the next major version - although I will have a backwards compatibility file that could re-enable them.

    Allan

  • ShaneBrennanShaneBrennan Posts: 49Questions: 18Answers: 1

    Fantastic!!

    Thanks Allan, excellent answer as always - this looks exactly what I need... I'll give it a go in the morning.

  • ShaneBrennanShaneBrennan Posts: 49Questions: 18Answers: 1

    Thanks Allan

    That worked a treat - for 1 parameter working only 5 more to go lol

    Thank you for your help

This discussion has been closed.