2

I am creating an HTML table dynamically from JSON objects. I am trying to create a filter for one of the table columns. It works fine but I cannot make it work with a multiselect function.

I tried adding this line:

$('.One1').multiselect();

in my script, in different places and it did not work.

var data = {"headers":["One","Two","Three","Four","Five","Six","Seven","Number1","Number2"],"rows":[["One1","Two1","Three1","Four1","Five1","Six1","Seven1",11,1000],["One1","Two1","Three2","Four1","Five1","Six1","Seven1", 22, 2000],["One2","Two1","Three1","Four2","Five1","Six1","Seven1", 77, 99]]};


 //////First table - Three1 for Views
 var table1 = '<div class = "filter_box"></div><div class="row"><div class="col-lg-6" style="background-color: #e90649; width: 117px;">&nbsp;</div><div class="col-lg-6" style="max-width: 100px; padding-left: 10px; font-size: 2vw;">Table<br/><br/><span style="font-size: 1vw;">One1, Three1, Five1</span></div><div class="col-lg-6"><div class="container"><table><thead><tr></tr><tr><th>One</th><th>Two</th><th>Three</th><th>Four</th><th>Five</th><th>Six</th><th>Seven</th><th>Number1</th><th>Number2</th></tr></thead><tbody>';
 var One = '<div class = "filter"><select class="One1" data-col="0"><option value="a">' + "One" + '</option>'; 

 for (i = 0; i < data.rows.length; i++) 
 {

table1 +="<tr><td>" + data.rows[i][0] + "</td><td>" + data.rows[i][1] + "</td><td>" + data.rows[i][2] + "</td><td>" + data.rows[i][3] + "</td><td>" + data.rows[i][4] + "</td><td>" + data.rows[i][5] + "</td><td>" + data.rows[i][6] + "</td><td>" + data.rows[i][7].toLocaleString() + "</td><td>" + data.rows[i][8].toLocaleString() + "</td><td>" + "</td></tr>";
  // Interactive filters

        One +='<option value="' + i + '">' + data.rows[i][0] + '</option>';
  }

 table1 += '</tbody></table></div></div></div>';
  One +='</select></div>';  
 $("#one").html(table1);
  $(".filter_box").html(One);
  
  ////First table - Filter
  
  $('.One1').change(function () {
      var values = [];

      $('.One1').each(function () {
      
          var colIdx = $(this).data('col');
          
          $(this).find('option:selected').each(function () {
              if ($(this).val() != "") values.push({
                  text: $(this).text(),
                  colId: colIdx
              });
          });
      });
      One1('#one table > tbody > tr', values);
  });

  function One1(selector, values) {
      $(selector).each(function () {
          var sel = $(this);
          var txt = sel.find('td:eq(0)').text().trim();
          var hwMatches = values.filter(function(ele, idx) {
              return ele.text == txt;
          }).length;
          sel.toggle(hwMatches > 0 || values.length == 0);
      });
  };

                 
 //$('.One1').multiselect();
         
@import url('https://fonts.googleapis.com/css?family=Roboto');

body, * { 
    margin: 0;
    color:#fff;
    font-family: Roboto; 
    text-transform:capitalize;
    }
.row {
    display: table;
    width: 100%;
    height: 241px; 
    background-color:#454545;
}
.row > .col-lg-6 {
    display: table-cell;
    vertical-align: middle;
}
.container {
  /*display: flex;*/
  flex-wrap: wrap;
}
.container > div {
  padding: 15px;
  margin: 5px;
  flex: 0 0 calc(100% - 20px);
  text-align: left;
}

/*img {
    padding-left: 7%;
    max-height:55px;
    width:auto;
}*/
td{
  padding: 2px 2px;
  text-align: center;
  margin: 6px 0;
  border: none;
}
table{
  width: 100%;
  background-color:#454545;
  font-weight:500;
  border-collapse: separate;
  border-spacing:0.3em 1.1em;
  color: #fff;
  border: 0;
}
tr{
    font-size: 1.1em;
}
th {
    color: #CCC;
    font-size: 0.7em;
  }
 
#one,#two,#three,#four{
    padding-top:2%;
  }
  
    .filter
    {
     margin:1px;
        width:20%;
        text-align:center;
        display:inline-block;    
    }
    .filter_box
    {
     text-align:center;
        display:inline-block;
     width:100%;
    }

.filter select, .multiselect dropdown-toggle btn btn-default {
  min-width:120px;
  -webkit-appearance: button;
  -webkit-border-radius: 2px;
  -webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
  -webkit-padding-end: 20px;
  -webkit-padding-start: 2px;
  -webkit-user-select: none;
  background-image: url(http://i62.tinypic.com/15xvbd5.png), -webkit-linear-gradient(#FAFAFA, #F4F4F4 40%, #E5E5E5);
  background-position: 97% center;
  background-repeat: no-repeat;
  border: 1px solid #AAA;
  color: #000;
  font-size: inherit;
  margin: 3px;
  overflow: hidden;
  padding: 1px 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/*
tr th:nth-child(5) {
    background: red;
    display:none;
} 

tr td:nth-child(5) {
    background: red;
    display:none;
} 
*/
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>


<div id="one"></div>
Datacrawler
  • 2,780
  • 8
  • 46
  • 100

2 Answers2

2

When you declare your select, you can specify it is multiple:

var One = '<div class = "filter"><select class="One1" data-col="0"><option value="a">' + "One" + '</option>';

Change it to:

var One = '<div class = "filter"><select multiselect class="One1" data-col="0"><option value="a">' + "One" + '</option>';

Result:

var data = {"headers":["One","Two","Three","Four","Five","Six","Seven","Number1","Number2"],"rows":[["One1","Two1","Three1","Four1","Five1","Six1","Seven1",11,1000],["One1","Two1","Three2","Four1","Five1","Six1","Seven1", 22, 2000],["One2","Two1","Three1","Four2","Five1","Six1","Seven1", 77, 99]]};


 //////First table - Three1 for Views
 var table1 = '<div class = "filter_box"></div><div class="row"><div class="col-lg-6" style="background-color: #e90649; width: 117px;">&nbsp;</div><div class="col-lg-6" style="max-width: 100px; padding-left: 10px; font-size: 2vw;">Table<br/><br/><span style="font-size: 1vw;">One1, Three1, Five1</span></div><div class="col-lg-6"><div class="container"><table><thead><tr></tr><tr><th>One</th><th>Two</th><th>Three</th><th>Four</th><th>Five</th><th>Six</th><th>Seven</th><th>Number1</th><th>Number2</th></tr></thead><tbody>';
 var One = '<div class = "filter"><select multiple class="One1" data-col="0"><option value="a">' + "One" + '</option>'; 

 for (i = 0; i < data.rows.length; i++) 
 {

table1 +="<tr><td>" + data.rows[i][0] + "</td><td>" + data.rows[i][1] + "</td><td>" + data.rows[i][2] + "</td><td>" + data.rows[i][3] + "</td><td>" + data.rows[i][4] + "</td><td>" + data.rows[i][5] + "</td><td>" + data.rows[i][6] + "</td><td>" + data.rows[i][7].toLocaleString() + "</td><td>" + data.rows[i][8].toLocaleString() + "</td><td>" + "</td></tr>";
  // Interactive filters

        One +='<option value="' + i + '">' + data.rows[i][0] + '</option>';
  }

 table1 += '</tbody></table></div></div></div>';
  One +='</select></div>';  
 $("#one").html(table1);
  $(".filter_box").html(One);
  
  ////First table - Filter
  
  $('.One1').change(function () {
      var values = [];

      $('.One1').each(function () {
      
          var colIdx = $(this).data('col');
          
          $(this).find('option:selected').each(function () {
              if ($(this).val() != "") values.push({
                  text: $(this).text(),
                  colId: colIdx
              });
          });
      });
      One1('#one table > tbody > tr', values);
  });

  function One1(selector, values) {
      $(selector).each(function () {
          var sel = $(this);
          var txt = sel.find('td:eq(0)').text().trim();
          var hwMatches = values.filter(function(ele, idx) {
              return ele.text == txt;
          }).length;
          sel.toggle(hwMatches > 0 || values.length == 0);
      });
  };

                 
 //$('.One1').multiselect();
@import url('https://fonts.googleapis.com/css?family=Roboto');

body, * { 
    margin: 0;
    color:#fff;
    font-family: Roboto; 
    text-transform:capitalize;
    }
.row {
    display: table;
    width: 100%;
    height: 241px; 
    background-color:#454545;
}
.row > .col-lg-6 {
    display: table-cell;
    vertical-align: middle;
}
.container {
  /*display: flex;*/
  flex-wrap: wrap;
}
.container > div {
  padding: 15px;
  margin: 5px;
  flex: 0 0 calc(100% - 20px);
  text-align: left;
}

/*img {
    padding-left: 7%;
    max-height:55px;
    width:auto;
}*/
td{
  padding: 2px 2px;
  text-align: center;
  margin: 6px 0;
  border: none;
}
table{
  width: 100%;
  background-color:#454545;
  font-weight:500;
  border-collapse: separate;
  border-spacing:0.3em 1.1em;
  color: #fff;
  border: 0;
}
tr{
    font-size: 1.1em;
}
th {
    color: #CCC;
    font-size: 0.7em;
  }
 
#one,#two,#three,#four{
    padding-top:2%;
  }
  
    .filter
    {
     margin:1px;
        width:20%;
        text-align:center;
        display:inline-block;    
    }
    .filter_box
    {
     text-align:center;
        display:inline-block;
     width:100%;
    }

.filter select, .multiselect dropdown-toggle btn btn-default {
  min-width:120px;
  -webkit-appearance: button;
  -webkit-border-radius: 2px;
  -webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
  -webkit-padding-end: 20px;
  -webkit-padding-start: 2px;
  -webkit-user-select: none;
  background-image: url(http://i62.tinypic.com/15xvbd5.png), -webkit-linear-gradient(#FAFAFA, #F4F4F4 40%, #E5E5E5);
  background-position: 97% center;
  background-repeat: no-repeat;
  border: 1px solid #AAA;
  color: #000;
  font-size: inherit;
  margin: 3px;
  overflow: hidden;
  padding: 1px 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/*
tr th:nth-child(5) {
    background: red;
    display:none;
} 

tr td:nth-child(5) {
    background: red;
    display:none;
} 
*/
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>


<div id="one"></div>
FcoRodr
  • 1,583
  • 7
  • 15
  • Rodriguez It changes, but now it messes up with the way the dropdown looks. Why is that happening? – Datacrawler Oct 13 '17 at 11:00
  • 1
    Multiselect's default appearance is a list where you can select multiple choices by pressing Ctrl or Shift keys. That's the way a multiselect looks different from a simple ` – FcoRodr Oct 13 '17 at 11:04
  • Rodriguez I want to keep the dropdown as is and have the multiselect option. I think I made it work here: https://jsfiddle.net/radomer/h08djybL/ Still checking. – Datacrawler Oct 13 '17 at 11:10
  • 1
    That js based solution looks much better for sure! Keep up the good work!! – FcoRodr Oct 13 '17 at 11:12
  • Rodriguez The next step is to remove the duplicates from the values shown in the options of the select. I have raised another question for this. Thanks for the reply :) – Datacrawler Oct 13 '17 at 11:16
0

For example if you want to filter "One1" in a table, you can simply use something like: $('td:contains("One1")').parent().hide(); and $('td:contains("One1")').parent().show(); as a filter. If you want to filter specific column, combine something like this with your selector: $('table tr > td:nth-child(yourColumnNumberShouldBeFiltered), table tr > th:nth-child(yourColumnNumberShouldBeFiltered)'). These expressions may be used in a function binded to 'change' event of your multiselect.

UPDATE: Above method, because of using :contains(), filter all of "One1", "One12", and so on! For exact match filteration you can use something like this:

$('table tr > td:nth-child(yourColumnNumberShouldBeFiltered)').filter(function() {
    return $(this).text() == "yourExactTextToFilter";
}).parent().hide();

$('table tr > td:nth-child(yourColumnNumberShouldBeFiltered)').filter(function() {
    return $(this).text() == "yourExactTextToFilter";
}).parent().show();

or for better performance of getting exact matches, see this link: Select element by exact match of its content