omitting rather than include the matching results from that search

omitting rather than include the matching results from that search

NehaCNehaC Posts: 20Questions: 5Answers: 0
edited February 2021 in Free community support

For example -
if you type “hello” into the name search field, only results that DO NOT INCLUDE “hello” in the name will appear in the results.

This question has accepted answers - jump to:

Answers

  • NehaCNehaC Posts: 20Questions: 5Answers: 0
    edited February 2021

    Some buddy please help, me

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773

    You will need to a Search Plugin for the comparison and your own search input to trigger the plugin. See this Range Search example for a running example of a search plugin.

    Kevin

  • NehaCNehaC Posts: 20Questions: 5Answers: 0

    Hello Kelvin,

    But this solution is not working for me.

    Please tell me another solution.

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773

    But this solution is not working for me.

    What did you try? Can you build a simple test case showing what you are trying?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • NehaCNehaC Posts: 20Questions: 5Answers: 0

    How I can try this solution to name field ?

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773

    Here is an example:
    http://live.datatables.net/kisamiju/1/edit

    It removes the matching items in the Office column. Type London into the Remove search input.

    Kevin

  • NehaCNehaC Posts: 20Questions: 5Answers: 0
    edited February 2021

    Need a 2 check box
    1) To enable/disable Regular expression
    2) To omitting rather than include the matching results

    below need code in if statement

    var table = $('#example').Datatable()

    $('#example thead tr:eq(1) th').each( function (i) {

    $( 'input', this ).on( 'keyup change', function () {
    if ( table.column(i).search() !== this.value ) {

                // for regular exppression
                if ($('#chk_'+i).prop('checked') == true) { 
                   reg_exp = true
                   smart_search = false
                 }
                 else {
                  smart_search = true
                  reg_exp = false
                 }
    
                 // for 
                 if ($('#not_equal_'+i).prop('checked') == true) {
    
                      table.draw();
                 }
                 else {
    
                  table
                  .column(i)
                  .search( $('#text_box'+i).val(),reg_exp,smart_search )
                  .draw()
                 }
                 }
                 });
    

    }
    Hope so, You understood my problem.

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    This example is working looking at, since it's doing mostly what you're looking for.

    Colin

  • NehaCNehaC Posts: 20Questions: 5Answers: 0
    edited February 2021

    Hello Colin,

    I saw this example, I have done To enable/disable Regular expression.

    I want a solution for this To omitting rather than include the matching results

    For example -
    if you type “hello” into the name search field, only results that DO NOT INCLUDE “hello” in the name will appear in the results in the data table

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773

    if you type “hello” into the name search field, only results that DO NOT INCLUDE “hello” in the name will appear in the results in the data table

    The example I provided does this. If you want to enable or disable this using a checkbox then within the search plugin example I provide you can get the state of the checkbox and if unchecked, for example, return true, like this snippet of code:

          // Show all rows since input is blank
          if (removeMe === '') {
            return true;
          }
    

    Kevin

  • NehaCNehaC Posts: 20Questions: 5Answers: 0
    edited February 2021

    Hello Kelvin,

    I understood this example but I didn't understand how to add in if a condition so please give me solution.

    can you please check this example -
    1) the first checkbox is disabled/endable regular expression
    2) To omitting rather than include the matching results from that search

    Here is my code

    I need code in if the condition of not_equal checkbox i.e if the second check box is checked true then

    http://live.datatables.net/qunodalu/1/edit

    so give me solutions

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773
    Answer ✓

    Maybe this example will give you a start.
    http://live.datatables.net/pezecuyu/1/edit

    I moved the header clone above the Datatables init so the sorting events aren't cloned. I added orderCellsTop to move the sorting events to the top row.

    I changed this code:

                     if ($('#not_equal_'+i).prop('checked') == true) {
                          console.log("in ifff")
                          
                      // Clear column search if checked
                      table
                      .column(i)
                      .search( '' )
                      .draw()
                     }
                     else {
                      console.log( "in else value = "+$('#text_box'+i).val())
                      table
                      .column(i)
                      .search( $('#text_box'+i).val(),reg_exp,smart_search )
                      .draw();
                     }
    

    If the not_equal checkbox is checked then the column search is cleared. Otherwise you will end up with conflict column search and not_equal search removing all rows from the table.

    I added the search plugin to loop through all cells in the row getting the not_equal checkbox state and the text input for comparison.

    Likely this needs more work. Hope it gets you started.

    Kevin

  • NehaCNehaC Posts: 20Questions: 5Answers: 0

    I saw your example and its working as per my requirements but the below function not called and I don't know when this function is called

    so can you tell me how can I call this function explicitly?

    $.fn.dataTable.ext.search.push(
    function( settings, searchData, index, rowData, counter ) {
    // Loop through all columns and get the checkbox state and input value
    for (i=0; i<searchData.length; i++) {
    var checked = $('#not_equal_' + i).prop('checked');
    console.log( "&&&&& ")
    // hide row if checked and input matches cell
    if (checked && $('#text_box'+i).val() === searchData[i]) {
    return false;
    }
    }

    return true;
    }

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773

    The search plugin is called anytime the Datatable is drawn, for example searching, sorting and paging will draw the Datatable. The draw() in line 8 in my last code snippet is used to call the search plugin. Also the draw() in line 15 calls the plugin.

    Kevin

  • NehaCNehaC Posts: 20Questions: 5Answers: 0

    Okay. I have written a console Statement but it was not printing that statement also.

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773

    Did you update the test case to show the problem?

    Kevin

  • NehaCNehaC Posts: 20Questions: 5Answers: 0
    edited February 2021

    Here the test case -

    http://live.datatables.net/qunodalu/1/edit

    Please check once.

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773
    Answer ✓

    Why did you remove this code (line 5 above) from my example?

     // Clear column search if checked
     table
     .column(i)
     .search( '' )
     .draw()
    

    As I explained this is needed to clear any searches in that column otherwise all the rows will be filtered. I added back to your example:
    http://live.datatables.net/qunodalu/2/edit

    Kevin

  • NehaCNehaC Posts: 20Questions: 5Answers: 0

    Hello Kevin,

    In the example, it's working fine but I don't know when I use the same code in my current project it's not working.

    http://live.datatables.net/qunodalu/1/edit

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773

    Its hard to say without seeing the problem. Can you post a link to your page or update the test case to show the issue?

    Look for errors in the browser's console.

    Use the browser's debugger to trace your code to see where its not working.

    Kevin

This discussion has been closed.