$.fn.datatable.ext.search.push not call

$.fn.datatable.ext.search.push not call

maboteomaboteo Posts: 2Questions: 1Answers: 0

i try... i try.. i try but i don't get the way..
I use server-side method to get the data, can is it the problem?
this is the initialization..

$(document).ready(function() {

var dt = $('#datatable').DataTable( {
        "applyFilter":true,
        "bJQueryUI": true,
        "bFilter": true,
        "bSort": true,
        "language": {
            "lengthMenu": "Visualizza _MENU_ records per pagina",
            "zeroRecords": "Nessun record trovato!",
            "info": "Pagina _PAGE_ di _PAGES_",
            "infoEmpty": "No records available",
            "infoFiltered": "(filtrati da _MAX_ records totali)"
        },
        "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
        "displayLength": 100,
        "processing": true,
        "serverSide": true,
        "stateSave": true,

        "ajax": "controller_spese.php",
        "columns": [
            { "data": "scDateScad" },
            { "data": "Fornitore" },
            { "data": "scName" },
            { "data": "Tipologia" },
            { "data": "scEuro" },
            { "data": "scState"}
          ],
        "scrollY":        "600px",
        "scrollCollapse": true,
        "paging":         false,
        "order": [[0, 'desc']]
    } );

//this is the event listener

$('#min, #max').change( function() {
  console.log("dentro");
    dt.draw();
} );

// and in the extern of "$(document).ready(function() {" i put the example..

$.fn.datatable.ext.search.push(

function( settings, data, dataIndex ) {
    var min = $('#min').val();
    var max = $('#max').val();
    var age = parseFloat( data[0] ) || 0; // use data for the age column
    console.log("dentro ext...");

    if ( ( isNaN( min ) && isNaN( max ) ) ||
         ( isNaN( min ) && age <= max ) ||
         ( min <= age   && isNaN( max ) ) ||
         ( min <= age   && age <= max ) )
    {
        return true;
    }
    return false;
}

);

where i take the cow by the balls???

Answers

  • allanallan Posts: 63,234Questions: 1Answers: 10,417 Site admin

    I use server-side method to get the data

    You are mixing a client-side filter, with server-side processing - with server-side processing all of the filtering is done at the server. So using the client-side filter will never work!

    If you want to perform filtering, it has to be done at the server and you'd need to adjust your server-side script accordingly if you want to continue using server-side processing.

    Allan

  • maboteomaboteo Posts: 2Questions: 1Answers: 0
    edited June 2015

    can you give me a link for an example of custom filter with server-side script.
    i think i need to use a "post" method into the ajax call.

    something who can be so:

    var dt = $('#datatable').DataTable( {

            "language": {
                "lengthMenu": "Visualizza _MENU_ records per pagina",
                "zeroRecords": "Nessun record trovato!",
                "info": "Pagina _PAGE_ di _PAGES_",
                "infoEmpty": "No records available",
                "infoFiltered": "(filtrati da _MAX_ records totali)"
            },
            "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
            "displayLength": 100,
            "processing": true,
            "serverSide": true,
            "stateSave": true,
            "responsive": true,
             "sDom": '<"top">rt<"bottom"lp>',
            "ajax": {
            "url": "controller_single_act.php",
            "type": "POST",
                        "data": {"rghId": post}
        },
            "columns": [
                { "data": "rghId" },
                { "data": "rghDateStep" },
                { "data": "Autista" },
                { "data": "rgbLocStart" },
                { "data": "rgbLocEnd" },
                { "data": "Targa" },
                { "data": "Cliente" },
                { "data": "state" },
                { "data": "rghCost" },  
            ],
            "scrollCollapse": false,
            "paging":         false,
            "order": [[0, 'desc']],
            "drawCallback": function( settings ) {
        var api = this.api();
        // Output the data for the visible rows to the browser's console
        console.log( api );
          api.rows.add(5);
    

    }
    } );

  • allanallan Posts: 63,234Questions: 1Answers: 10,417 Site admin

    I'm afraid I don't have one. You would need to modify whatever is in controller_single_act.php to perform the filter - presumably using an SQL WHERE condition, but it depends upon your script. The ajax.data parameter can be used to send the filtering information to the server - you should use it as a function so it is executed on each request.

    Allan

  • calacala Posts: 52Questions: 15Answers: 0

    Hi to everybody!

    @maboteo, did you resolve your problem? If yes, could you explain me your approach?

    I've created an ajax call that returns me an object with the same "shape" of the response of the server-side script that is in every server-side example, but I cannot understand how to re-draw the table with that infos.

    Someone has an idea?

    Thanks for the help!

  • allanallan Posts: 63,234Questions: 1Answers: 10,417 Site admin

    @cla - I think we would need more information that you have provided to be able to offer any help. A link to the page showing the issue for example.

    Allan

  • calacala Posts: 52Questions: 15Answers: 0

    Thanks Allan!

    I want to filter my table, but with the serverside on, I cannot use the $.fn.datatable.ext.search.push function.

    I though that, if I can obtain the same structure of the server response with filtered rows (with an ajax call that return me that filtered table object), it will be possible to call

    table.draw(filtered_rows)
    

    or something like this, in order to draw the "new" table.

  • allanallan Posts: 63,234Questions: 1Answers: 10,417 Site admin

    draw() accepts only one parameter and that tells DataTables how the draw should be performed. There is no option to draw only certain rows.

    Allan

  • calacala Posts: 52Questions: 15Answers: 0
    edited November 2015

    Maybe I've understood something...

    I pass with ajax.data an indicator of which table I want to recall (full_table, date_filtered_table, etc...).

    For example:

    ajax: {
        url: "../server_processing_editor_3tab_view_prova.php",
        type: "POST",
        data:{'which_table': structure}
    }
    

    structure is a variable initialized to 'full_table'.

    After that, in that php file with a case I can recall the different tables:

    switch ($_POST['which_table']) {
        case 'full_table':
            require('server_processing_editor.php');
            break;
        case 'date_filtered_table':
            require('filter_date.php');
            break;
    }
    

    The file selected with 'full_table' is the "default" server-side script, instead of the other one that give me the table filtered.

    now, if I set the variable structure with the appropriate value when I want add a filter to the table (or eliminate it), can I recall the different tables (maybe with a table.draw() )?

    yes you can see that I want to resolve all with draw...

  • allanallan Posts: 63,234Questions: 1Answers: 10,417 Site admin

    That looks fine. If you are using server-side processing then ajax.data will always be sent to the server.

    Allan

  • calacala Posts: 52Questions: 15Answers: 0

    Thank you!

    I've tried

    table.ajax.data(post_data).reload();
    

    with

    var post_data={
        'vista': 'filter_date',
        'pagination_value': pagination_value,
        'current_page': current_page,
        'min_date': min_date,
        'max_date': max_date
    };
    

    but I have an error:

    Uncaught TypeError: table.ajax.data is not a function

    Where is my fault?

  • amboseambose Posts: 1Questions: 0Answers: 0

    Hi,
    This is not linear method. but it works.

     //*****OPERAZIONI DIRETTAMENTE SULLA DATATABLE*****//
        $('#filter li').click(function() {
          stato = $(this).text();
          dt.draw();
        });
        // On each draw, loop over the `detailRows` array and show any child rows
        dt.on('draw', function() {
          for (var i = 0; i < dt.data().length; i++) {
            if (dt.data()[i].state != stato && stato != "Disattiva filtri" || dt.data()[i].state == "Terminata") {
              dt.row(i).node().remove();
            }
            if (dt.data()[i].state == "In Viaggio") $(dt.row(i).node()).css("background-color", "#CCFF99");
            if (dt.data()[i].state == "In Chiusura") $(dt.row(i).node()).css("background-color", "#FFFFCC");
            if (dt.data()[i].state == "Creata") $(dt.row(i).node()).css("background-color", "#FFFF66");
          }
        });
      });
    
  • allanallan Posts: 63,234Questions: 1Answers: 10,417 Site admin

    Uncaught TypeError: table.ajax.data is not a function

    Without a link to a test case I can't say for sure. My guess is that this relates to the top FAQ.

    Allan

  • calacala Posts: 52Questions: 15Answers: 0

    Grazie @ambose!

    I've also found this discussion:

    https://www.datatables.net/forums/discussion/30286/ajax-reload-is-not-sending-updated-params

    and this worked for me!
    When I'll have more time I'll test your solution!

    now I've another issue... if I limit the row returned by the server to the pagination value (for example 10 row), the 1st page view is correct, but obviously I've no other pages to show.
    If I don't limit it, all the rows are showed.

    Practical example: paginator set to 10; I need to display 17 rows filtered. In the first case, only the first 10 are showed but the paginator indicator is not shown, in the second case the paginator correctly have 2 pages but all the 17 rows are shown in the first and in the second page.

    Is it possible to correct this? Thank you!

This discussion has been closed.