filtering datatable with checkboxes, column value 'S' appear value 'N' dont

filtering datatable with checkboxes, column value 'S' appear value 'N' dont

andrebertonhaandrebertonha Posts: 11Questions: 1Answers: 0

I´m trying this question that i founded here, but with no success

$(document).ready( function () {
  var table = $('#dataTableClientes').DataTable();
  
 $('input:checkbox').on('change', function () {
   //build a regex filter string with an or(|) condition
        var origens = $('input:checkbox[name="orig"]:checked').map(function() {
          return '^' + this.value + '$';
        }).get().join('|');   

        //filter in column 5, with an regex, no smart filtering, not case sensitive
        table.column(5).search(origens, true, false, false).draw(false);
        table.column(6).search(origens, true, false, false).draw(false);
        table.column(7).search(origens, true, false, false).draw(false);
        table.column(8).search(origens, true, false, false).draw(false);   

  });

} );

Answers

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    You posted a similar question in this thread:
    https://datatables.net/forums/discussion/53671/how-can-i-use-multiple-checkbox-filter#latest

    Its hard to say what the problem is without seeing the full picture of what you have. Please post a link to your page or a test case replicating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    The example I posted in the other thread does work. I'm not sure why the same solution doesn't work for you without seeing it.

    Kevin

  • andrebertonhaandrebertonha Posts: 11Questions: 1Answers: 0

    It´s a combination of checkboxes and one select
    When click just refresh datatable result with the selected value
    I will post some picture, thanks

  • andrebertonhaandrebertonha Posts: 11Questions: 1Answers: 0

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Its difficult to help with your code without actually seeing it in action. Can you post a link to your page or a test case replicating the issue?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • andrebertonhaandrebertonha Posts: 11Questions: 1Answers: 0

    https://jsfiddle.net/w8oyfdub/1/
    is not working, here i could filter by first checkbox
    so the logic to filter the 4 checkboxes, i dont now if this is correct way yo do
    thanks for helping

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Thanks for the test case. That will help us to get you going. You may need to use a Search Plugin for this depending on how you want it to behave.

    I understand you have four columns which can have either S or N values. If you check Site for example then you want to search the S column for S or if unchecked for N. You have the same for the other 3 columns.

    Do you want this to be an and or an or search?

    How do you want to reset the search to show the whole table? Meaning how do you want to show both N and S in the Site column?

    Maybe you need two checkboxes for each column; one for S and one for N.

    Let us know what you want to do then we can help you adjust your code. If you want two checkboxes for each column please adjust your test case and post the new link.

    Kevin

  • andrebertonhaandrebertonha Posts: 11Questions: 1Answers: 0

    I want to be an and search. If the 4 checkboxes are checked then will show only registers who have this match
    When the user uncheck some of check boxes so I have to show the match result of others with S value
    When uncheck all checkboxes the entire datatable must to be shown

    André

  • andrebertonhaandrebertonha Posts: 11Questions: 1Answers: 0
  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Not sure if this is exactly what you want but it should get you started:
    https://jsfiddle.net/tnvf2aLq/2/

    It uses the search plugin. Inside the function its just a matter of Javascript code to determine if the row should be displayed (return true) or not displayed (return false). Hopefully you can workout any JS changes needed to reach your solution.

    Kevin

  • andrebertonhaandrebertonha Posts: 11Questions: 1Answers: 0

    Almost there, I´m trying to use .map function so when hidden 'S' value by other checkbox can be shown in users click

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Not sure I understand what you want to do with the .map function but you will want it in the click event not the search plugin function since the plugin is called for each row.

    Kevin

  • andrebertonhaandrebertonha Posts: 11Questions: 1Answers: 0

    Thanks for helping Kevin so i reach the result that I want

    $(function() {
      otable = $('#dataTableClientes').dataTable();
    });
    
    function filterme() {
      
      var types = $('input:checkbox[name="site"]:checked').map(function() {
        return this.value === 'on' ? 'S' : 'N';
      }).get().join('|');  
      otable.fnFilter(types, [5], true, false, false, false);
      
      var valueBoca = $('input:checkbox[name="boca"]:checked').map(function() {
        return this.value === 'on' ? 'S' : 'N';
      }).get().join('|');  
      otable.fnFilter(valueBoca, [6], true, false, false, false);  
      
      var valueFace = $('input:checkbox[name="face"]:checked').map(function() {
        return this.value === 'on' ? 'S' : 'N';
      }).get().join('|');  
      otable.fnFilter(valueFace, [7], true, false, false, false);  
      
      var valueIndica = $('input:checkbox[name="indica"]:checked').map(function() {
        return this.value === 'on' ? 'S' : 'N';
      }).get().join('|');  
      otable.fnFilter(valueIndica, [8], true, false, false, false);
      
    }   
    

    not sure if ih the best solution but it´s working fine
    so as soon as possible i will update one fiddle showing how it´s working
    and get some feedback
    Thanks for all

  • andrebertonhaandrebertonha Posts: 11Questions: 1Answers: 0
This discussion has been closed.