range_filtering. How are you supplying 'data' to search function in the example given below

range_filtering. How are you supplying 'data' to search function in the example given below

ihemantihemant Posts: 7Questions: 1Answers: 0
edited August 2014 in Free community support

I am asking question in reference to "http://www.datatables.net/examples/plug-ins/range_filtering.html"

range_filtering. How are you supplying 'data' to search function in the example given below

I am using

$.fn.dataTable.ext.search.push(
            function( settings, data, dataIndex ) {
                var min = parseInt( $('#min').val(), 10 );
                var max = parseInt( $('#max').val(), 10 );
                var age = parseFloat( data[6] ) || 0; // use data for the age column
        console.log(JSON.stringify(data));
                if ( ( isNaN( min ) && isNaN( max ) ) ||
                     ( isNaN( min ) && age <= max ) ||
                     ( min <= age   && isNaN( max ) ) ||
                     ( min <= age   && age <= max ) )
                {
                    return true;
                }
                return false;
            }
        );

$(document).ready(function() {
            var table = $(".userInfo").DataTable({
            "bFilter":true,
            "bRetrieve":false,
            "bDestroy":false,
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "<?php echo $this->Html->Url(array('controller' => 'Admins', 'action' => 'ajaxData')); ?>",
        });
             
            // Event listener to the two range filtering inputs to redraw on input
            $('#min, #max').keyup( function() {
                table.draw();
            } );
        });

This code, its picking data from php json and showing data on the page (id, name, email , region_id, member_since and age) where age is an alias I am calculating age from data of birth of person.
Now the example you given is not working for me (i guess coz its dynamic data)

How the $.fn.dataTable.ext.search.push() & function( settings, data, dataIndex ) will receive data value??

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,640Questions: 1Answers: 10,092 Site admin
    Answer ✓

    data is the original data object. In my example that is an array. But in your case it sounds like it is an object, so you would use, for example, data.member_since etc to access the data.

    Allan

  • ihemantihemant Posts: 7Questions: 1Answers: 0

    Hi allan, thanks for taking your time for replying my question so soon. I found for custom filtering code today, and since your code was working I tried it on my code. but was unable to figure how to get the parameters for $.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {...});

    so as you are saying If i use "function (settings,data.member_since, dataIndex){...} will it work?

    Coz I have to use filters with age as well as member since dates.

  • ihemantihemant Posts: 7Questions: 1Answers: 0

    I hope that it will work on dynamic data, as I have database with more than 70,000 records.

  • allanallan Posts: 61,640Questions: 1Answers: 10,092 Site admin
    Answer ✓

    No - the function prototype you suggest will not work. It will cause a Javascript error.

    I've suggesting that wherever you want to access the data you use data.{property_name}. If you add console.log( data ) to the top of the function, you will be able to look at the console in your browser and see what the data structure is.

    I have database with more than 70,000 records.

    I would suggest using server-side processing in that case. In which case the filtering would be done at the server, and the Javascript filtering discussed above would not be used at all.

    Allan

  • ihemantihemant Posts: 7Questions: 1Answers: 0

    sorry but i am not able to figure out how to user serverside scripting for filtering data, also how to trigger it with datatable api.

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67

    @ihemant , If you are using datatables 1.10 you can try my yadcf for datatables , see this server side column filtering showcase page http://yadcf-showcase.appspot.com/server_side_source.html

  • ihemantihemant Posts: 7Questions: 1Answers: 0
    edited August 2014

    Thanks daniel. I have alreay implemented custom filters with server side scripting.
    I just added "jquery.dataTables.columnFilter.js" from one of the custom filter examples and

    $(selector).dataTable()..columnFilter({ sPlaceHolder: "head:after",
    aoColumns: [
    null,{ type: "text" },{ type: "text" },null,null,
    { type: "number-range" }
    ]
    });

    And it worked along with the custom filter code I made. Just having designing issues. And I wonder if I can change pagination at the bottom (its just showing "Previous Next" only). Also I wonder if i can custom add fields for filter. As here I am unable to fix the design.

    The next trouble I am with is the charts that are going to show the data from clubbing 2-3 tables and display the result stats on chart also require server side filter. :( ( I am using cakephp 2.5.3) not to mention the records i have, tables contain over 70,000 records and with live users count will keep increasing. I am unable to find Chart API for it.

This discussion has been closed.