【问题标题】:use jquery .filter() to filter by multiple criteria使用 jquery .filter() 按多个条件过滤
【发布时间】:2018-05-30 18:15:25
【问题描述】:

我有想要按数据属性过滤的 div。第一个过滤器是由文本输入过滤的 data-title 属性。我要实现的第二个过滤器是通过 data-tags 属性。数据标签的一个例子是:data-tags="js, php, css"

当用户单击标签按钮时,该标签将添加到标签数组中。我希望能够通过属性、数据标题和数据标签来过滤 div。

这是一个示例 div

<div id="work-card-0" class="work-card col-md-3 text-center" data-title="Test project" data-tags="js,php,css" style=""><img class="work-img img-responsive" src="images/skier.png"><h3 class="work-title">Test project</h3><p class="work-p">This is a sample project</p><a class="btn btn-primary" href="http://google.com" target="_blank">View</a></div>

这里是jquery

    $(document).ready(function() {

  $.getJSON('work.json', function(data) {
   $.each(data, function(key, value) {
    $.each(value, function(k, v) {
     $("#work-section").append("<div id='work-card-" + v.id + "' class='work-card col-md-3 text-center' data-title='" + v.title + "' data-tags='" + v.tag + "'><img class='work-img img-responsive' src='" + v.image + "'><h3 class='work-title'>" + v.title + "</h3><p class='work-p'>" + v.desc + "</p><a class='btn btn-primary' href='" + v.link + "' target='_blank'>View</a></div>");
    });
   });
  });



  $("#filter").on('keyup', function() {
   $("#filter-form").submit();
  });

  var tags = [];
  $(".tag").on('click', function() {
   var tag = $(this).attr('value');
   if (tags.indexOf(tag) == -1) tags.push(tag);
   getTags();
  });

  function getTags() {
   $("#tag-holder ul").empty();
   for (var i = 0; i < tags.length; i++) {
    $("#tag-holder ul").append("<li value='" + i + "'>" + tags[i] + "</li>");
   }
  }

  //remove tags
  $("#tag-holder ul").on('click', 'li', function() {
   var tag = $(this).attr('value');
   tags.splice(tag, 1);
   getTags();
  });

  $("#filter-form").on('submit', function(e) {
   e.preventDefault();
   getTags();
   var search = $("#filter").val();

   $.ajax({
    url: 'work.html'
   }).done(function() {

    $(".work-card").hide();
    $(".work-card").filter(function() {
     return ($(this).data('title').toLowerCase().indexOf(search.toLowerCase()) > -1);
    }).filter(function() {
     // filter here by tag
    }).show();
   });
  });


  $("#reset-btn").on('click', function() {
   $("#filter").val("");
   tags = [];
   getTags();
   $(".work-card").show();
  });

 });

过滤器表单具有标签的输入和所有按钮。当它被提交时,div 被过滤。标题过滤器有效。我如何也可以按数据标签进行过滤。我希望过滤器按两个数据属性进行过滤

【问题讨论】:

    标签: jquery


    【解决方案1】:

    这不是最有效的方法,因为 jQuery 中的过滤器很棒,但我认为这是一个简单的解决方案,可以快速帮助您...

    $(document).ready(function() {
    	var searchedTags = ["html", "js"];
    	
    	$(".work-card").hide();
    	$(".work-card").filter(function() {
    		return ($(this).data('title').toLowerCase().indexOf("test") > -1);
    	}).filter(function() {
    		var result = false;
    		var tags = $(this).data("tags").split(",");
    		if (searchedTags && tags && (tags instanceof Array)) {
    			for(var i in searchedTags) {
    				var searchedTag = (searchedTags[i]).toLowerCase();
    				console.log("  Searched Tag: " + searchedTags[i]);
    				result = result || ($.inArray(searchedTags[i], tags) != -1);
    			}
    		}
    		return result;
    	}).show();
    	
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!DOCTYPE html>
    <html>
    <meta charset="utf-8">
    <head>
    </head>
    <body>
    <div id="work-card-0" class="work-card col-md-3 text-center" data-title="Test project 1" data-tags="css" style=""><h3 class="work-title">Test project 1 (css)</h3><p class="work-p">This is a sample project</p><a class="btn btn-primary" href="http://google.com" target="_blank">View</a></div>
    <div id="work-card-1" class="work-card col-md-3 text-center" data-title="Test project 2" data-tags="css,js" style=""><img class="work-img img-responsive" src="images/skier.png"><h3 class="work-title">Test project 2 (css/js)</h3><p class="work-p">This is a sample project</p><a class="btn btn-primary" href="http://google.com" target="_blank">View</a></div>
    <div id="work-card-2" class="work-card col-md-3 text-center" data-title="Test project 3" data-tags="php,html" style=""><img class="work-img img-responsive" src="images/skier.png"><h3 class="work-title">Test project 3 (php/html)</h3><p class="work-p">This is a sample project</p><a class="btn btn-primary" href="http://google.com" target="_blank">View</a></div>
    <div id="work-card-3" class="work-card col-md-3 text-center" data-title="Test project 4" data-tags="php,html" style=""><img class="work-img img-responsive" src="images/skier.png"><h3 class="work-title">Test project 4 (js)</h3><p class="work-p">This is a sample project</p><a class="btn btn-primary" href="http://google.com" target="_blank">View</a></div>
    </body>
    </html>

    使用 jQuery 自定义过滤器的一个有趣的方法,我认为过滤器实现可以改进但现在太累了:

    $(document).ready(function() {
    
      var searchedTags = ["js", "css"];
    
      $.expr[':'].jobWorkFilter = function(elem, index, match) {
        var result = false;
    		var tags = $(elem).data("tags").split(",");
    		if (searchedTags && tags && (tags instanceof Array)) {
    			for(var i in searchedTags) {
    				var searchedTag = (searchedTags[i]).toLowerCase();
    				console.log("  Searched Tag: " + searchedTags[i]);
    				result = result || ($.inArray(searchedTags[i], tags) != -1);
    			}
    		}
    		return result;
      };
        
      $(".work-card")
        .hide()
        .filter(".work-card[data-title*='Test']:jobWorkFilter()")
        .show();
        
    });
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </head>
    <body>
    <div id="work-card-0" class="work-card col-md-3 text-center" data-title="Test project 1" data-tags="css" style=""><h3 class="work-title">Test project 1 (css)</h3><p class="work-p">This is a sample project</p><a class="btn btn-primary" href="http://google.com" target="_blank">View</a></div>
    <div id="work-card-1" class="work-card col-md-3 text-center" data-title="Test project 2" data-tags="css,js" style=""><img class="work-img img-responsive" src="images/skier.png"><h3 class="work-title">Test project 2 (css/js)</h3><p class="work-p">This is a sample project</p><a class="btn btn-primary" href="http://google.com" target="_blank">View</a></div>
    <div id="work-card-2" class="work-card col-md-3 text-center" data-title="Test project 3" data-tags="php,html" style=""><img class="work-img img-responsive" src="images/skier.png"><h3 class="work-title">Test project 3 (php/html)</h3><p class="work-p">This is a sample project</p><a class="btn btn-primary" href="http://google.com" target="_blank">View</a></div>
    <div id="work-card-3" class="work-card col-md-3 text-center" data-title="Test project 4" data-tags="php,html" style=""><img class="work-img img-responsive" src="images/skier.png"><h3 class="work-title">Test project 4 (js)</h3><p class="work-p">This is a sample project</p><a class="btn btn-primary" href="http://google.com" target="_blank">View</a></div>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 2018-12-05
      • 2017-06-08
      • 2019-06-26
      • 1970-01-01
      相关资源
      最近更新 更多