teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

修正

2020/11/04 02:17

投稿

MH00214
MH00214

スコア53

title CHANGED
File without changes
body CHANGED
@@ -1,93 +1,1 @@
1
- ## 問題
2
- 現在、ニュースを表示する一覧ページと詳細ページを作成しています。
3
- 一覧ページの方は
4
- * Railsのインスタンス変数から受け取ったデータをHTMLで表示している
5
- * ページ上部にセレクトボックスがあり、それの選択内容によってカテゴリごとの表示切り替えを行っている(プレスリリース、を選択したらプレスリリース関連のものだけ表示される)
6
- * 表示の切り替えはCSS+jQueryで実装。基本的にはactiveクラスの付け外しで対応。
7
-
8
- 詳細ページの方は
9
- * 普通に記事の内容が表示される
10
- * 一番上にカテゴリを表すボタンがあり、**そこをクリックすると「そのカテゴリを選択した状態」の一覧ページに遷移する**
11
-
12
- という仕様です。ここを実現したいのですが、方法が見つかりません。
13
-
14
- 試しに詳細ページの方に
15
-
16
- ```HTML
17
- <p class="back">サンプル</p>
18
- ```
19
- として
20
-
21
- ```javascript
22
- $(function() {
23
- $(".back").on("click", function() {
24
- window.history.back();
25
- })
26
- });
27
- ```
28
-
29
- としたのですが、optionタグの選択肢だけ切り替わったまま、できる必要のない要素まで出てしまっています。
30
- セレクトボックスは下記のようにしており
31
-
32
- ```HTML
33
- <select name="news-category-select-box" class="news-category-select-box">
34
- <option value="all">すべて</option>
35
- <option value="media">メディア</option>
36
- <option value="press_release">プレスリリース</option>
37
- </select>
38
- ```
39
-
40
- ニュースの覧デタは下のようにRailsを使てデータ表示してます。
1
+ 部不具合を含んだコドや誤った述があたため、質問削除ました。再度調べ直し投稿します。
41
-
42
- ```HTML
43
- <div class="news-content-cards">
44
- <% news.each do |article| %>
45
- <%= link_to article.url(relative: true), class: "news-content-card news-content-#{cms_fragment_content(:category_en, article)} active" do %>
46
- <% if cms_fragment_content(:hero_image, article).present? %>
47
- <%= image_tag cms_fragment_content(:hero_image, article).first.variant(resize: '480x287').processed, class: "news-content-card-image", alt: "#{cms_fragment_content(:title, article)}>" %>
48
- <% else %>
49
- <%= image_tag "pages/cms/news/image.png", class: "news-content-card-image" %>
50
- <% end %>
51
- <div class="news-content-card-info">
52
- <p class="news-date"><%= cms_fragment_content(:publish_date, article) %></p>
53
- <h3 class="news-title"><%= cms_fragment_content(:title, article) %></h3>
54
- <p class="news-category"><%= cms_fragment_content(:category_jp, article) %></p>
55
- </div>
56
- <% end %>
57
- <% end %>
58
- </div>
59
- ```
60
-
61
- `news-content-#{cms_fragment_content(:category_en, article)} のクラス名の時にactiveをつける形で、表示、非表示を切り替えています。
62
-
63
- ```jQuery
64
- $(function () {
65
- const eliminateRightMargin = function () {
66
- const rowCount = 3;
67
- const activeClassCount = $(".active").length
68
- const lineCount = Math.floor(activeClassCount / rowCount);
69
- for (let i = 1; i <= lineCount; i++) {
70
- $(".active").eq(rowCount * i - 1).addClass("eliminate-right-margin")
71
- }
72
- }
73
-
74
- $(".news-category-select-box").change(function () {
75
- const $newsContentCard = $(".news-content-card");
76
- const $selectBoxValue = $(this).val();
77
- $newsContentCard.removeClass("active");
78
- $newsContentCard.removeClass("eliminate-right-margin");
79
- if ($selectBoxValue === "all") {
80
- $newsContentCard.addClass("active");
81
- eliminateRightMargin();
82
- }
83
- $(".news-content-" + $selectBoxValue).addClass("active");
84
- eliminateRightMargin();
85
- });
86
-
87
- $(window).on("load", function () {
88
- eliminateRightMargin();
89
- });
90
- });
91
- ```
92
-
93
- 知見のある方おりましたら教えていただけますと助かります。