Adjust the datatables filters correctly with diferent words [duplicate]












0















This question already has an answer here:




  • Match whole string

    3 answers




I have a datatable with several columns.
One of them is id=event



To filter by "Event" I release a modal with a multiple select2 (id=event_filter) and by clicking on the Send button (id=filter-button) I launch the following code by javascript.



$(document).ready(function (e) {
let table = $('#datatable-table').DataTable();
$('#filter-button').click(function (e) {
e.preventDefault();
let str = $('#event_filter').find(':selected').map(function() {
return $( this ).text();
})
.get()
.join( "|" );
if (str.length > 0){
console.log(str);
table
.columns($('th#event'))
.search(jQuery.fn.DataTable.ext.type.search.string(str),true,false )
.draw();
} else {
table
.columns($('th#event'))
.search("")
.draw();
}
})
})


Among the select options are:



-Natal



-Pre Natal



-Post Natal



The problem is: When I select ONLY Natal, the console.log (str) returns Natal, but the table filters Natal, Pre Natal and Post Natal.



The behavior you must have is:



I select Natal => Only the rows with Natal appear



Select Natal and Pre Natal => appear only rows with Pre Natal and Natal



What am I doing wrong?










share|improve this question















marked as duplicate by Wiktor Stribiżew regex
Users with the  regex badge can single-handedly close regex questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 15:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0















    This question already has an answer here:




    • Match whole string

      3 answers




    I have a datatable with several columns.
    One of them is id=event



    To filter by "Event" I release a modal with a multiple select2 (id=event_filter) and by clicking on the Send button (id=filter-button) I launch the following code by javascript.



    $(document).ready(function (e) {
    let table = $('#datatable-table').DataTable();
    $('#filter-button').click(function (e) {
    e.preventDefault();
    let str = $('#event_filter').find(':selected').map(function() {
    return $( this ).text();
    })
    .get()
    .join( "|" );
    if (str.length > 0){
    console.log(str);
    table
    .columns($('th#event'))
    .search(jQuery.fn.DataTable.ext.type.search.string(str),true,false )
    .draw();
    } else {
    table
    .columns($('th#event'))
    .search("")
    .draw();
    }
    })
    })


    Among the select options are:



    -Natal



    -Pre Natal



    -Post Natal



    The problem is: When I select ONLY Natal, the console.log (str) returns Natal, but the table filters Natal, Pre Natal and Post Natal.



    The behavior you must have is:



    I select Natal => Only the rows with Natal appear



    Select Natal and Pre Natal => appear only rows with Pre Natal and Natal



    What am I doing wrong?










    share|improve this question















    marked as duplicate by Wiktor Stribiżew regex
    Users with the  regex badge can single-handedly close regex questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 11 at 15:51


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0








      This question already has an answer here:




      • Match whole string

        3 answers




      I have a datatable with several columns.
      One of them is id=event



      To filter by "Event" I release a modal with a multiple select2 (id=event_filter) and by clicking on the Send button (id=filter-button) I launch the following code by javascript.



      $(document).ready(function (e) {
      let table = $('#datatable-table').DataTable();
      $('#filter-button').click(function (e) {
      e.preventDefault();
      let str = $('#event_filter').find(':selected').map(function() {
      return $( this ).text();
      })
      .get()
      .join( "|" );
      if (str.length > 0){
      console.log(str);
      table
      .columns($('th#event'))
      .search(jQuery.fn.DataTable.ext.type.search.string(str),true,false )
      .draw();
      } else {
      table
      .columns($('th#event'))
      .search("")
      .draw();
      }
      })
      })


      Among the select options are:



      -Natal



      -Pre Natal



      -Post Natal



      The problem is: When I select ONLY Natal, the console.log (str) returns Natal, but the table filters Natal, Pre Natal and Post Natal.



      The behavior you must have is:



      I select Natal => Only the rows with Natal appear



      Select Natal and Pre Natal => appear only rows with Pre Natal and Natal



      What am I doing wrong?










      share|improve this question
















      This question already has an answer here:




      • Match whole string

        3 answers




      I have a datatable with several columns.
      One of them is id=event



      To filter by "Event" I release a modal with a multiple select2 (id=event_filter) and by clicking on the Send button (id=filter-button) I launch the following code by javascript.



      $(document).ready(function (e) {
      let table = $('#datatable-table').DataTable();
      $('#filter-button').click(function (e) {
      e.preventDefault();
      let str = $('#event_filter').find(':selected').map(function() {
      return $( this ).text();
      })
      .get()
      .join( "|" );
      if (str.length > 0){
      console.log(str);
      table
      .columns($('th#event'))
      .search(jQuery.fn.DataTable.ext.type.search.string(str),true,false )
      .draw();
      } else {
      table
      .columns($('th#event'))
      .search("")
      .draw();
      }
      })
      })


      Among the select options are:



      -Natal



      -Pre Natal



      -Post Natal



      The problem is: When I select ONLY Natal, the console.log (str) returns Natal, but the table filters Natal, Pre Natal and Post Natal.



      The behavior you must have is:



      I select Natal => Only the rows with Natal appear



      Select Natal and Pre Natal => appear only rows with Pre Natal and Natal



      What am I doing wrong?





      This question already has an answer here:




      • Match whole string

        3 answers








      jquery regex datatables






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 13:29

























      asked Nov 11 at 13:19









      Jose

      409




      409




      marked as duplicate by Wiktor Stribiżew regex
      Users with the  regex badge can single-handedly close regex questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 11 at 15:51


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Wiktor Stribiżew regex
      Users with the  regex badge can single-handedly close regex questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 11 at 15:51


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          Finally I found the solution:



          return '^'+$(this).text()+'$';





          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Finally I found the solution:



            return '^'+$(this).text()+'$';





            share|improve this answer


























              0














              Finally I found the solution:



              return '^'+$(this).text()+'$';





              share|improve this answer
























                0












                0








                0






                Finally I found the solution:



                return '^'+$(this).text()+'$';





                share|improve this answer












                Finally I found the solution:



                return '^'+$(this).text()+'$';






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 14:33









                Jose

                409




                409















                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings