Occurence of Word Stats from Filtered

Occurence of Word Stats from Filtered

hackerseraphhackerseraph Posts: 5Questions: 0Answers: 0
edited January 2014 in General
Team,

Is there a way to pull stats for items that are filtered. Take for instance Filter for word giraffe. In my stats div I can have Legs Occurs: 5 Times because there may be 5 different cells that have the word "legs" showing.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    So you want to get the data from the filtered table? You can use the `_` method in 1.9 to do that: http://datatables.net/docs/DataTables/1.9.4/#_ - specifically the `filter` option setting it to `apply` .

    Allan
  • hackerseraphhackerseraph Posts: 5Questions: 0Answers: 0
    edited January 2014
    I've tried using

    [code]
    $(document).ready(function() {
    var oTable = $('#tablesorter').dataTable();


    // Get the data from the filtered table rows
    var ptwo = oTable._('tr', {"filter": "applied"});


    var ptwofind = ptwo.match(/findthisword/g);
    var ptwocount = ptwofind.length;

    alert( "Total is"+ptwocount);

    } );
    [/code]

    on a table initialised with

    [code]
    $(document).ready(function() {
    $('#tablesorter').dataTable(
    {
    "iDisplayLength": 150,
    "aaSorting": [[2,'asc'], [0,'asc']],
    "aLengthMenu": [[150, 25, 50, -1], [150, 25, 50, "All"]],
    "sDom": 'T<"top"if>rtl<"bottom"p><"clear">',
    "oLanguage": {
    "oPaginate": {
    "sNext": "Next page",
    "sPrevious": "Previous page | "
    }},
    "bJQueryUI": false,
    "aoColumnDefs": [{ "bVisible": false, "aTargets": [19, 18, 17, 16, 15, 14, 13, 12,] }],
    });
    });
    [/code]

    to find a specific word, the following console errors
    [code]
    Uncaught TypeError: Object [object Array] has no method 'match'
    [/code]
    Am I taking the long way around just to have it output how many times a word shows up in filtered results?
  • hackerseraphhackerseraph Posts: 5Questions: 0Answers: 0
    edited January 2014
    Found my error, needed to convert the array to a string first before matching

    [code]
    $(document).ready(function() {
    var oTable = $('#tablesorter').dataTable();


    // Get the data from the filtered table rows
    var ptwo = oTable._('tr', {"filter": "applied"});


    var ptwofind = ptwo.toString().match(/P2/gi);
    var ptwocount = ptwofind.length;

    alert( "Total is"+ptwocount);

    } );
    [/code]

    The above can be modified, to have javascript output the amount to a div on the fly :) enjoy
  • hackerseraphhackerseraph Posts: 5Questions: 0Answers: 0
    Below is the code to have it output to a div, can anyone help me with a method that updates when you change the filter without refreshing the page?

    [code]
    $(document).ready(function() {
    var oTable = $('#tablesorter').dataTable();


    // Get the data from the filtered table rows
    var ptwo = oTable._('tr', {"filter": "applied"});


    var ptwofind = ptwo.toString().match(/P2/gi);
    var ptwocount = ptwofind.length;

    document.getElementById('stats').innerHTML="P2 Total is:"+ptwocount;

    } );
    [/code]
  • hackerseraphhackerseraph Posts: 5Questions: 0Answers: 0
    Below is revised for error checking if .match() cant find anything, returning undefined -> .length will throw an error against undefined. Think of it like dividing by zero !!!

    [code]
    // Get the XXXXX from filtered results
    var oTable = $('#yourTable').dataTable();
    var xxxfind = tableFilter.toString().match(/xxx/gi);
    if ( xxxfind != undefined ){
    var xxxcount = xxxfind.length;
    document.getElementById('statsDiv').innerHTML+="xxx total is: "+xxxcount+"
    ";
    }
    else {
    document.getElementById('statsDiv').innerHTML+="xxx total is: 0
    ";
    }
    });
    });
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > can anyone help me with a method that updates when you change the filter without refreshing the page?

    Use the `filter` event - http://datatables.net/docs/DataTables/1.9.4/#filter . (note in DataTables 1.10 this is called `search` and must be used with a `.dt` namespace when attaching a jQuery event listener!).

    Allan
This discussion has been closed.