morne
morne

Reputation: 4169

Searching from DataTable

https://datatables.net/reference/api/search()#Example

Im using the link above as an example for my searching, but my setup is quite different.

<td>
    <div class="plCell_desktop">
        <input type="radio" class="" data-lnk_id="414107671" data-group="RUTH">
        <label for="414107671">RUTH</label>
    </div>
</td>

here is a extract from my table.

The only visable bit of data is "Ruth".
but when I search for say '76' it will still bring "Ruth" back as a result.
The reason is most likely the fact that I have a lot more info in the table cell than "Ruth".

Okay, so my question is. Can you force DataTables to search from the beginning of a word. e.g. ( enter "uth" will not bring back "Ruth", but "Ru" will, hope it makes sense ).

Can you do a kind of "innerHTML.val()" search with DataTables?

Upvotes: 1

Views: 2282

Answers (3)

Arnauld
Arnauld

Reputation: 6110

If you need to access the matching labels many times in your code, you may want to avoid computing the regular expression each time. One solution would be to run a one-time pre-processing that would add a custom attribute. Let's call it isOk.

You then can select the labels with a standard jQuery selector:

$('label[isOk=Y]')

Below is some demo code.

var regex = /^RU/;

$('label').each(function() {
  $(this).attr('isOk', $(this).html().match(regex) !== null ? 'Y' : 'N');
});

var res = $('label[isOk=Y]');

// check whether we've selected the correct labels
res.each(function() { console.log($(this).attr('for')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td>
    <div class="plCell_desktop">
        <input type="radio" class="" data-lnk_id="414107671" data-group="RUTH">
        <label for="414107671">RUTH</label>
        <input type="radio" class="" data-lnk_id="414107672" data-group="RUTH">
        <label for="414107672">RUTH TOO</label>
        <input type="radio" class="" data-lnk_id="414107673" data-group="RUTH">
        <label for="414107673">NOT REALLY RUTH</label>
    </div>
</td>

Upvotes: 1

davidkonrad
davidkonrad

Reputation: 85518

Okay, so my question is. Can you force DataTables to search from the beginning of a word. e.g. ( enter "uth" will not bring back "Ruth", but "Ru" will, hope it makes sense ).

Yes. Create a custom filter upfront that perform filtering like this. The default "smart search" will be overruled by the custom filter and any future filtering will go through that :

$.fn.dataTable.ext.search.push(function( settings, data, dataIndex ) {
   var term = $('.dataTables_filter input').val().toLowerCase()
   for (var i=0, l=data.length; i<l; i++) {
     if (data[i].toLowerCase().indexOf(term) == 0 ) return true
   }
   return false
})

demo -> http://jsfiddle.net/qxcjzuxa/

As you may notice it would be very easy to make the dataTables filtering overall case sensitive (just an example). The filter array is a LIFO-structure where you can have multiple filters on top of each other. You remove a filter simply by $.fn.dataTable.ext.search.pop() if you for any reason will disable "beginning of word" filtering dynamically.

Upvotes: 1

Nick Bull
Nick Bull

Reputation: 9856

Could you just use jQuery?

$(".plCell_desktop label").each(function() {
  // Using RexExp matching
  RexExp regex = new RegExp(); // Your regex obj
  if ($(this).val().match(regex)) {
    // Do stuff if it matches
  }

  // Or if you just want to do something if it has a value:
  if ($(this).val()) {
    // ...
  }
});

Upvotes: 1

Related Questions