Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- $('.filter').change(function () {
- var values = [];
- $('.filter option:selected').each(function () {
- if ($(this).val() != "") values.push($(this).text());
- });
- filter('table > tbody > tr', values);
- });
- function filter(selector, values) {
- $(selector).each(function () {
- var sel = $(this);
- var hide = false;
- $.each(values, function (i, val) {
- if (sel.text().search(new RegExp("\b"+val+"\b")) < 0) hide = true;
- });
- hide ? sel.hide() : sel.show();
- });
- }
- });
- <select class="filter">
- <option value="">None</option>
- <option value="a">wood</option>
- </select>
- <select class="filter">
- <option value="">None</option>
- <option value="1">blue</option>
- <option value="2">green</option>
- <option value="3">red</option>
- </select>
- <table>
- <tbody>
- <tr>
- <td>Wood comes from trees</td>
- <td>11</td>
- <td>blue</td>
- </tr>
- <tr>
- <td>Some wood is hard</td>
- <td>512</td>
- <td>green</td>
- </tr>
- <tr>
- <td>Some people like woodwork</td>
- <td>51</td>
- <td>red</td>
- </tr>
- <tr>
- <td>Some wood is green</td>
- <td>12</td>
- <td>blue</td>
- </tr>
- </tbody>
- </table>
- <select class="filter" data-col="0">
- <option value="">None</option>
- <option value="a">wood</option>
- </select>
- <select class="filter" data-col="2">
- <option value="">None</option>
- <option value="1">blue</option>
- <option value="2">green</option>
- <option value="2">red</option>
- </select>
- $(document).ready(function () {
- $('.filter').change(function () {
- var values = [];
- $('.filter').each(function () {
- var colIdx = $(this).data('col');
- $(this).find('option:selected').each(function () {
- if ($(this).val() != "") values.push( {
- text: $(this).text(),
- colId : colIdx
- });
- });
- });
- filter('table > tbody > tr', values);
- });
- function filter(selector, values) {console.log(values);
- $(selector).each(function () {
- var sel = $(this);
- var tokens = sel.text().trim().split('n');
- var toknesObj = [], i;
- for(i=0;i<tokens.length;i++){
- toknesObj[i] = {
- text:tokens[i].trim(),
- found:false
- };
- }
- var show = false;
- //console.log(toknesObj);
- $.each(values, function (i, val) {
- if (toknesObj[val.colId].text.search(new RegExp("\b"+val.text+"\b")) >= 0) {
- toknesObj[val.colId].found = true;
- }
- });
- console.log(toknesObj);
- var count = 0;
- $.each(toknesObj, function (i, val) {
- if (val.found){
- count+=1;
- }
- });
- show = (count === values.length);
- show ? sel.show() : sel.hide();
- });
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement