0

I use ASP.NET MVC5 & JQuery. Currently I have a table setup that is filled with values from a database. Two columns: Name & Explanation

I can filter through both columns by typing and all rows that have a value that contains the input will be shown. Because the explanations are quite lengthy I would like to highlight the exact matches in the values in the cells. Think of making them bold, curved or anything like that.

Is there any way I could do this using JQuery?


EDIT1:

Filtering itself works already. I would just like to see the following to happen:

input: Book in t

Cell value: Today I am reading a Book in the garden.

The purpose of this would be to clarify where the value matches with your input.

Let me know if you have any more questions!


Input on which I filter:

<tr class="tableRow">
   <td><input type="search" class="form-control tableFilter"  placeholder="Filter..." /></
</tr>

Current JQuery that handles filtering:

//Table Filter based on input
$(".tableFilter").keyup(function () {
    //split the current value of searchInput
    var data = this.value.split(" ");
    //create a jquery object of the rows
    var jo = $(".table").find("tbody tr");
    if (this.value == "") {
        jo.show();
        return;
    }
    //hide all the rows
    jo.hide();

    //Filter the jquery object to get results.
    jo.filter(function (i, v) {
        var $t = $(this);
        var matched = true;
        for (var d = 0; d < data.length; ++d) {
            if (data[d].match(/^\s*$/)) {
                continue;
            }
            var regex = new RegExp(data[d].toLowerCase());
            if ($t.text().toLowerCase().replace(/(manual|auto)/g, "").match(regex) === null) {
                matched = false;
            }
        }
        return matched;
    }).show();
})
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Can You provide any code? Will be easier to help. Maybe [this](https://stackoverflow.com/a/15364327/5962118) will be helpful. – Anwarus Nov 23 '17 at 12:56
  • Added code and some extra explanation! –  Nov 23 '17 at 13:14

1 Answers1

0

You mean something like this. Instead of using regex I used here indexOf function to get to know if provided string exist in cell.

//Table Filter based on input
$(".tableFilter").keyup(function() {

  var rows = $(".table").find("tbody tr");

  //Filter the jquery object to get results.
  if (this.value.length > 0) {
   //First hide all an remove class used to identify matched rows
    rows.removeClass("match").hide().filter(function() {
     var match = false;
     $(this).find("td.filter").each(function() {
        var indexOf = $(this).text().toLowerCase().indexOf($(".tableFilter").val().toLowerCase());
        //Check with indexOf if this row cell include search string
        if(indexOf !== -1) {
          match = true;
           return;
        } 
      });
     return match;
    }).addClass("match").show();
  } else {
   //If filter not provided show all 
    rows.removeClass("match").show().find("b").contents().unwrap();
  }

  highlight(this.value);
})

var highlight = function(string) {
  $(".table").find("tbody tr.match td.filter").each(function() {
  
   if($(this).text().indexOf(string) === -1)
     return;
  
    var matchStartIndex = $(this).text().toLowerCase().indexOf(string.toLowerCase());
    var matchEndIndex = matchStartIndex + string.length - 1;

    var beforeMatch = $(this).text().slice(0, matchStartIndex);
    var matchText = $(this).text().slice(matchStartIndex, matchEndIndex + 1);
    var afterMatch = $(this).text().slice(matchEndIndex + 1);

    //Here set selected text to e.g. bold style
    $(this).html(beforeMatch + "<b>" + matchText + "</b>" + afterMatch);
  });
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="search" class="form-control tableFilter" placeholder="Filter..." />
<table class="table" border="1">
  <thead>
    <tr class="tableRow">
      <th>Col1</th>
      <th>Col2</th>
      <th>Actions</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="filter">test of tests</td>
      <td class="filter">second of tests</td>
      <td>Edit/Delete</td>
    </tr>
    <tr>
      <td class="filter">book in the garden</td>
      <td class="filter">two books in the garden</td>
      <td>Edit/Delete</td>
    </tr>
    <tr>
      <td class="filter">chain hell</td>
      <td class="filter">chain hell continues</td>
      <td>Edit/Delete</td>
    </tr>
  </tbody>
</table>
Anwarus
  • 138
  • 2
  • 11
  • Yes! This is what I intended to do! You're a champ! One issue though is that the matching text it returns is not shown in table form anymore. Just a long string with highlighted bits, do you happen to know how I'd fix that? Edit: I just fiddled around with it, and when I backspace my search value untill it is empty, every value in the name Collumn is replaced with a string made of the entire length of the search result string. –  Nov 23 '17 at 14:10
  • Now should be better. I was replacing whole tr than only single cell (td). – Anwarus Nov 23 '17 at 14:20
  • In your example there is only 1 collumn. In my case there is 1 input field filtering two collumns. For some reason when a match happens in the 2nd collumn it gets automatically moved to the 1st. What makes that happen? –  Nov 23 '17 at 14:24
  • Yep because I wasn't considering more columns. Made some edits and now it will accept as many cols as You wish. – Anwarus Nov 23 '17 at 14:50
  • You're a champ! Fixed something in 45 minutes I've been struggling with all day! –  Nov 23 '17 at 14:52
  • One more question, there is actually a third collumn with icons to edit/delete the row. These change to the following after filtering: "× Edit definition Name: Definition: Cancel Confirm". Is there a way to have them not affected by the filter? Or atleast not have the icons replaced by this text but have them reappear? –  Nov 23 '17 at 14:55
  • You can for example add special class for columns allowed to be filtered like I did in this example. – Anwarus Nov 23 '17 at 15:07
  • This worked great! Could you help me out a bit more? Some entries in the table are clickable and will link to a page. When highlighting the link attribute disappears, is there a way to fix this? –  Dec 01 '17 at 09:49
  • You mean when searching for text to highlight which is a link? – Anwarus Dec 03 '17 at 08:08
  • Yes, that is correct. The link ability is removed upon highlighting. Also when completely removing the input in the input field, the last letter that was removed remains highlighted. I.e. when I search for: "Everest" and I remove that from the input field, all "E"'s in all cells remain highlighted. Sorry for asking so much of you :P –  Dec 03 '17 at 11:16