【问题标题】:Filter Elements in a Rails App with HTML/CSS/JS使用 HTML/CSS/JS 在 Rails 应用程序中过滤元素
【发布时间】:2020-03-14 06:12:33
【问题描述】:

按照 w3schools 说明创建 HTML/CSS/JS 过滤器后:https://www.w3schools.com/howto/howto_js_filter_elements.asp

最初没有显示任何项目。我认为只有在 btns[i].addEventListener("click", function() { 触发过滤开始工作。我认为这与 JavaScript 在 Rails 应用程序或 JavaScript 中的执行方式有关,因为我在这方面缺乏足够的经验。

提前感谢您的帮助!

这是我目前正在使用的代码:

应用程序.js

// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//

//= require jquery
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require rrt
//= require_tree .


filterSelection("all")
function filterSelection(c) {
  var x, i;
  x = document.getElementsByClassName("filterDiv");
  if (c == "all") c = "";
  // Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
  for (i = 0; i < x.length; i++) {
    w3RemoveClass(x[i], "show");
    if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
  }
}

// Show filtered elements
function w3AddClass(element, name) {
  var i, arr1, arr2;
  arr1 = element.className.split(" ");
  arr2 = name.split(" ");
  for (i = 0; i < arr2.length; i++) {
    if (arr1.indexOf(arr2[i]) == -1) {
      element.className += " " + arr2[i];
    }
  }
}

// Hide elements that are not selected
function w3RemoveClass(element, name) {
  var i, arr1, arr2;
  arr1 = element.className.split(" ");
  arr2 = name.split(" ");
  for (i = 0; i < arr2.length; i++) {
    while (arr1.indexOf(arr2[i]) > -1) {
      arr1.splice(arr1.indexOf(arr2[i]), 1);
    }
  }
  element.className = arr1.join(" ");
}

// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function() {
    var current = document.getElementsByClassName("active");
    current[0].className = current[0].className.replace(" active", "");
    this.className += " active";
  });
}

index.html.erb 我的过滤器显示给用户的地方


  <% if logged_in? %>
  <% content_for :actions do %>
    <%= link_to "/admin/portfolio_item/new", class: 'btn btn-primary btn-large pull-right' do %>
      <%= fa_icon_tag "plus" %> Referenz anlegen
      <% end %>
      <% end %>
      <% end %>

      <div align="center" id="myBtnContainer">
         <button class="btn active" onclick="filterSelection('all')"> Alle</button>
         <button class="btn" onclick="filterSelection('wohnen')"> Wohnen</button>
         <button class="btn" onclick="filterSelection('kultur')"> Kultur</button>
         <button class="btn" onclick="filterSelection('gewerbe')"> Gewerbe</button>
         <button class="btn" onclick="filterSelection('verwaltung')"> Verwaltung</button>
      </div>
      <br/>

      <div class="container">
        <div class="row">
            <% @portfolio_items.each do |portfolio_item| %>
              <div class="portfolio_item col-12 col-md-4 filterDiv <%= portfolio_item.category_filter %>">
                <%= image_tag(portfolio_item.image_url(:blog_list), :class => 'py-3', :width => '300', :height => '200') %>

                <h4><%= link_to portfolio_item.main_title, portfolio_item_path(portfolio_item) %></h4>
                <p><%= portfolio_item.main_subtitle %></br><b> <%= portfolio_item.job_type %></b></p>
              </div>
              <% end %>



        </div>
      </div>

my_rrt_overrides.scss # 我正在使用Rapid Rails Themes,这是自定义css的推荐位置。


.container {
  overflow: hidden;
}

.filterDiv {
  display: none; /* Hidden by default */
}

/* The "show" class is added to the filtered elements */
.show {
  display: block;
}

【问题讨论】:

    标签: javascript jquery html css ruby-on-rails


    【解决方案1】:

    感谢 kiso.io 的 John McDowell...

    在新文件 custom.js 中并从 application.js 中删除 require_tree,因为我正在使用 turbolinks filterSelection("all") 需要像这样包装......

    $(document).on('turbolinks:load', function() {
      filterSelection("all")
    })
    
    
    function filterSelection(c) {
      var x, i;
      x = document.getElementsByClassName("filterDiv");
      if (c == "all") c = "";
      // Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
      for (i = 0; i < x.length; i++) {
        w3RemoveClass(x[i], "show");
        if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
      }
    }
    
    // Show filtered elements
    function w3AddClass(element, name) {
      var i, arr1, arr2;
      arr1 = element.className.split(" ");
      arr2 = name.split(" ");
      for (i = 0; i < arr2.length; i++) {
        if (arr1.indexOf(arr2[i]) == -1) {
          element.className += " " + arr2[i];
        }
      }
    }
    
    // Hide elements that are not selected
    function w3RemoveClass(element, name) {
      var i, arr1, arr2;
      arr1 = element.className.split(" ");
      arr2 = name.split(" ");
      for (i = 0; i < arr2.length; i++) {
        while (arr1.indexOf(arr2[i]) > -1) {
          arr1.splice(arr1.indexOf(arr2[i]), 1);
        }
      }
      element.className = arr1.join(" ");
    }
    
    // Add active class to the current control button (highlight it)
    var btnContainer = document.getElementById("myBtnContainer");
    var btns = btnContainer.getElementsByClassName("btn");
    for (var i = 0; i < btns.length; i++) {
      btns[i].addEventListener("click", function() {
        var current = document.getElementsByClassName("active");
        current[0].className = current[0].className.replace(" active", "");
        this.className += " active";
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 2015-03-10
      • 2011-11-13
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      相关资源
      最近更新 更多