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

質問編集履歴

1

質問本文に情報が無いものが含まれていたため、修正しました。

2020/10/24 07:19

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- 再度検索すると以前の検索情報が残り、表示されたままになってしまう(検索時にリセットさせたい
1
+ 検索毎に以前のデータをリセットさせたい
body CHANGED
@@ -1,23 +1,24 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- vue.jsbootstrapを使用して自作サイトを作成しています。
3
+ vue.jsbootstrap、jsonを使用して自作サイト(スマートフォン版)を作成しています。
4
4
 
5
+ **<サイト内容>**
5
- 内容は、Selectタグ(見頃、花の名前、場所)のoptionタグのアイテムを選択し、検索ボタンをクリックすると、自作のjsonデータから条件に一致したものを表示させるというものです。
6
+ inputタグのテキスト入力、Selectタグ(見頃、花の名前、場所)のoptionタグのアイテムを選択し、検索ボタンをクリックすると、自作のjsonデータから条件に一致したものを表示させるというものです。
6
7
 
8
+ **<実現したいこと>**
7
- 実現したいことは、検索時に、以前の検索結果をリセットして表示させないようにしたいです。
9
+ 1度検索したアイテムが再検索時にも引き継がれてしまうため検索毎に、以前の検索結果をリセットして表示させないようにしたいです。
8
10
 
9
11
  ### 試したこと
10
12
 
11
- リセットボタンを作成しクリック時にリセットさせという方法はあったのですが、サイト的にこれ以上ボタンを増やしたくないと考えており
13
+ リセット専用ボタンを作成る方法はあったのですが、サイト的にこれ以上ボタンを増やしたくないと考えております。
12
14
 
13
- 他に方法がありましたら、アドバイス頂けると幸いです。
15
+ 他に方法がありましたら、アドバイス頂けると幸いです。よろしくお願いしますm(__)m
14
16
 
15
- よろしくお願いします。
17
+ ### 該当のソースコード(文字数制限があるため、一部省略)
16
18
 
17
- ### 該当のソースコード(長文のため、一部省略しています)
19
+ ```html
18
20
 
19
- ```html
20
- <!DOCTYPE html>
21
+ </html><!DOCTYPE html>
21
22
  <html lang="ja">
22
23
 
23
24
  <head>
@@ -25,78 +26,68 @@
25
26
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
26
27
 
27
28
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
28
- <link rel="stylesheet" href="css/main.css">
29
- <link rel="stylesheet" href="css/sticky-footer.css">
30
29
 
31
30
  <title>花ナビ</title>
32
31
  </head>
33
32
 
34
33
  <body>
35
34
  <div id="flower">
36
- <header>
37
- <nav class="navbar navbar-expand-sm navbar-light bg-light">
38
- <a href="#" class="navbar-brand">花ナビ</a>
39
-
40
- <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#nav-bar">
41
- <span class="navbar-toggler-icon"></span>
42
- </button>
43
- </nav>
44
- </header>
45
-
46
- <section class="main">
47
- <h1 class="main-img text-center h2"><strong>\ 花を楽しもう! /</strong></h1>
48
- </section>
35
+ <!-- 検索フォーム -->
49
36
  <section class="search-box container">
50
37
  <div class="row">
51
38
  <div class="form-group col-4">
52
39
  <label for="exampleFormControlSelect1">見頃</label>
53
40
  <select class="form-control" id="exampleFormControlSelect1" v-model="searchParams.season">
54
- <option value="0">すべて</option>
41
+ <option value="0">すべて</option>
55
- <option value="spring">春の花</option>
42
+ <option value="spring">春の花</option>
56
- <option value="summer">夏の花</option>
43
+ <option value="summer">夏の花</option>
57
- <option value="fall">秋の花</option>
44
+ <option value="fall">秋の花</option>
58
- <option value="winter">冬の花</option>
45
+ <option value="winter">冬の花</option>
59
- </select>
46
+ </select>
60
47
  </div>
61
48
  <div class="form-group col-4">
62
49
  <label for="exampleFormControlSelect1">花の名前</label>
63
50
  <select class="form-control" id="exampleFormControlSelect1" v-model="searchParams.flowerId">
64
- <option value="0">すべて</option>
51
+ <option value="0">すべて</option>
65
- <option value="1">サクラ</option>
52
+ <option value="1">サクラ</option>
66
- <option value="2">ネモフィラ</option>
53
+ <option value="2">ネモフィラ</option>
67
- <option value="3">シバザクラ</option>
68
- <option value="4">ロベリア</option>
69
- <option value="5">オステオスペルマム</option>
70
- <option value="6">クリスマスローズ</option>
71
- </select>
54
+ </select>
72
55
  </div>
73
56
  <div class="form-group col-4">
74
57
  <label for="exampleFormControlSelect1">場所</label>
75
58
  <select class="form-control" id="exampleFormControlSelect1" v-model="searchParams.place">
76
- <option value="0">すべて</option>
59
+ <option value="0">すべて</option>
77
- <option value="1">北海道</option>
60
+ <option value="1">北海道</option>
78
- <option value="2">東北</option>
61
+ <option value="2">東北</option>
79
- <option value="3">甲信越、北陸</option>
80
- <option value="4">関東</option>
81
- <option value="5">関西</option>
82
- <option value="6">四国</option>
83
- <option value="7">中国</option>
84
- <option value="8">九州</option>
85
- <option value="9">沖縄</option>
86
- </select>
62
+ </select>
87
63
  </div>
88
64
  </div>
65
+ <input class="form-control" v-model="searchParams.freeword" type="text" placeholder="フリーワード">
89
66
  <div class="py-3">
90
67
  <button class="btn btn-success btn-block" @click="searchSpot()">この条件で検索</button>
91
68
  </div>
92
69
  </section>
70
+
71
+ <!-- 架空DOM生成(季節の花/見頃) -->
72
+ <section class="flower-item py-2 px-2">
73
+ <div class="flower-ttl container mb-3" v-for="season in seasonDatas" v-bind:key="season.id">
74
+ <div class="row text-center pt-2">
75
+ <h1 class="col-7 h5" v-bind:name="season.name">{{ season.name }}</h1>
76
+ <p class="flower-season col-5 p-1 small" v-bind:flowering="season.flowering">{{ season.flowering }}</p>
77
+ </div>
78
+ <div class="flower-txt d-flex flex-row">
79
+ <p class="small" v-bind:desc="season.desc">{{ season.desc }}</p>
80
+ <img class="ml-2" v-bind:src="season.imgSrc" v-bind:alt="season.name">
81
+ </div>
82
+ </div>
83
+ </section>
93
84
 
94
85
  <!-- 架空DOM生成(花の名前) -->
95
86
  <section class="flower-item py-2 px-2">
96
87
  <div class="flower-ttl container mb-3" v-for="item in itemDatas" v-bind:key="item.id">
97
88
  <div class="row text-center pt-2">
98
89
  <h1 class="col-7 h5" v-bind:name="item.name">{{ item.name }}</h1>
99
- <p class="flower-season col-5 p-1 small" v-bind:flowering ="item.flowering">{{ item.flowering }}</p>
90
+ <p class="flower-season col-5 p-1 small" v-bind:flowering="item.flowering">{{ item.flowering }}</p>
100
91
  </div>
101
92
  <div class="flower-txt d-flex flex-row">
102
93
  <p class="small" v-bind:desc="item.desc">{{ item.desc }}</p>
@@ -105,23 +96,23 @@
105
96
  </div>
106
97
  </section>
107
98
 
108
- <!-- 架空DOM生成(見頃) -->
99
+ <!-- 架空DOM生成(フリーワード) -->
109
100
  <section class="flower-item py-2 px-2">
110
- <div class="flower-ttl container mb-3" v-for="season in seasonDatas" v-bind:key="season.id">
101
+ <div class="flower-ttl container mb-3" v-for="item in itemWordDatas" v-bind:key="item.id">
111
102
  <div class="row text-center pt-2">
112
- <h1 class="col-7 h5" v-bind:name="season.name">{{ season.name }}</h1>
103
+ <h1 class="col-7 h5" v-bind:name="item.name">{{ item.name }}</h1>
113
- <p class="flower-season col-5 p-1 small" v-bind:flowering ="season.flowering">{{ season.flowering }}</p>
104
+ <p class="flower-season col-5 p-1 small" v-bind:flowering="item.flowering">{{ item.flowering }}</p>
114
105
  </div>
115
106
  <div class="flower-txt d-flex flex-row">
116
- <p class="small" v-bind:desc="season.desc">{{ season.desc }}</p>
107
+ <p class="small" v-bind:desc="item.desc">{{ item.desc }}</p>
117
- <img class="ml-2" v-bind:src="season.imgSrc" v-bind:alt="season.name">
108
+ <img class="ml-2" v-bind:src="item.imgSrc" v-bind:alt="item.name">
118
109
  </div>
119
110
  </div>
120
111
  </section>
121
-
112
+
122
113
  <!-- 架空DOM生成(場所) -->
123
114
  <section class="flowerSpot container p-4" v-for="spot in spotDatas" v-bind:key="spot.id">
124
- <!--カルーセル作成-->
115
+ <!-- カルーセル作成 -->
125
116
  <div id="example-3" class="carousel slide" data-ride="false" style="width:400px">
126
117
  <ol class="carousel-indicators">
127
118
  <li data-target="#example-3" data-slide-to="0" class="active"></li>
@@ -139,8 +130,7 @@
139
130
  <img v-bind:src="spot.imgSrc_3" v-bind:alt="spot.imgAlt_3" width="330" height="180">
140
131
  </div>
141
132
  </div>
142
- </div>
133
+ </div>
143
-
144
134
  <div class="flowerSpot-ttl">
145
135
  <h1 class="mb-0 pt-2 h6">{{ spot.title }}</h1>
146
136
  </div>
@@ -154,27 +144,67 @@
154
144
  <p class="small m-0 pt-2">{{ spot.desc }}</p>
155
145
  </div>
156
146
  </section>
157
-
158
- <footer class="footer">
147
+
159
- <div class="container">
160
- <p class="text-muted">©2020 hana_nav.</p>
161
- </div>
162
- </footer>
163
148
  </div>
164
149
  <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
165
-
166
150
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
167
-
168
151
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
169
-
170
152
  <script defer src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
171
-
172
153
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
173
154
  <script src="js/main.js"></script>
174
155
  </body>
175
156
 
176
157
  </html>
158
+ ```
159
+ ```js
160
+ new Vue({
161
+ el: "#flower",
162
+ data: {
163
+ searchParams: {
164
+ season: '0',
165
+ flowerId: '0',
166
+ place: '0',
167
+ freeword: null,
168
+ },
169
+ seasonDatas: [],
170
+ itemDatas: [],
171
+ spotDatas: [],
172
+ itemWordDatas: [],
173
+ },
177
174
 
175
+ methods: {
176
+ // 検索ボタンクリック時に作動
177
+ searchSpot() {
178
+ // 「フリーワード(item.json用)」での検索表示
179
+ $.getJSON("json/item.json", (data) => {
180
+
181
+ const result = data.filter((v) => {
182
+ return v.name.includes(this.searchParams.freeword);
183
+ });
184
+ this.itemWordDatas = result;
185
+ });
186
+
187
+ // 「季節の花」と「見頃」での検索表示
188
+ $.getJSON("json/item.json", (data) => {
189
+
190
+ const result = data.filter((v) => {
191
+ return v.season === this.searchParams.season;
192
+ });
193
+ this.seasonDatas = result;
194
+ });
195
+
196
+ // 「花の名前」での検索表示
197
+ $.getJSON("json/item.json", (data) => {
198
+
199
+ const result = data.filter((v) => {
200
+ return v.id === Number(this.searchParams.flowerId);
201
+ });
202
+ this.itemDatas = result;
203
+ });
204
+ },
205
+ },
206
+ });
207
+
178
208
  ```
179
209
  ```itemJson
180
210
  [
@@ -183,7 +213,7 @@
183
213
  "name": "サクラ",
184
214
  "season": "spring",
185
215
  "flowering": "3月上旬〜4月下旬",
186
- "desc": "サクラは卒業から入学の時期に咲く、日本の春を代表する樹木です。特徴として開花している花は比較的短い枝についています。花の色は白や薄桃色、濃い桃色などをしており、花びらも一重や八重があります。また、桜の種類によっては、赤い果実(さくらんぼ)を6月頃付けるものもあります。",
216
+ "desc": "サクラは卒業から入学の時期に咲く、日本の春を代表する樹木です。",
187
217
  "imgSrc": "images/flower_item/img_01.jpg"
188
218
  },
189
219
  {
@@ -191,7 +221,7 @@
191
221
  "name": "ネモフィラ",
192
222
  "flowering": "4月上旬〜5月中旬",
193
223
  "season": "spring",
194
- "desc": "ネモフィラは森の妖精のような、澄んだブルーの花が愛らしく、春の花壇やコンテナの寄植えなどに多く利用される人気の花です。細かく切れ込んだ葉が密に茂り、自然に分枝して咲きながら大きくこんもりと成長します。",
224
+ "desc": "ネモフィラは森の妖精のような、澄んだブルーの花",
195
225
  "imgSrc": "images/flower_item/img_02.jpg"
196
226
  }
197
227
  ]
@@ -210,7 +240,7 @@
210
240
  "title": "ファーム富田",
211
241
  "url": "https://www.farm-tomita.co.jp",
212
242
  "access": "〒071-0704 北海道空知郡中富良野町基線北15",
213
- "desc": "ラベンダーが織りなすグラデーションが魅力的な花畑や、七色の花々を敷き詰めた色彩豊かな花畑など、季節ごとに見頃が異なる12の花畑が広がっています園内には、雄大な十勝岳連峰を眺められるテラスや、食事やソフトクリームなどのスイーツを提供しているカフェも複数あり、ひと休みしながらゆっくりと花巡りが楽しめます。"
243
+ "desc": "ラベンダーが織りなすグラデーションが魅力的な花畑。"
214
244
  },
215
245
  {
216
246
  "id": 2,
@@ -224,7 +254,8 @@
224
254
  "title": "みちのくあじさい園",
225
255
  "url": "https://www.ichitabi.jp/spot/data.php?p=36",
226
256
  "access": "〒021-0221 岩手県一関市舞川原沢111",
227
- "desc": "山あじさいをメインとした全国的にも珍しい「みちのくあじさい園」は、山あじさい、ウツギ類、エゾあじさい、ガクあじさい、西洋あじさい、玉あじさいなど原種品種数は日本有数を誇り、約400種4万株のあじさいが咲き誇ります約15ヘクタールもの杉林には「くれないコース」と「奥姫コース」「健脚コース」の三つの散策コースがあり、6月下旬頃からの1ヵ月間、多種多様なあじさいを眺めながら散策を楽しむことができます。開花期間に合わせて「みちのくあじさいまつり」も開催され、毎年多くの人々で賑わいます。"
257
+ "desc": "山あじさいをメインとした全国的にも珍しい「みちのくあじさい園」。"
228
258
  }
229
259
  ]
260
+
230
261
  ```