質問編集履歴
4
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -27,6 +27,7 @@
|
|
27
27
|
sort_by = request.args.get("sort_by")
|
28
28
|
if sort_by== "number":
|
29
29
|
posts = Post.query.order_by('number').all()
|
30
|
+
return render_template('index.html', posts=posts)
|
30
31
|
```
|
31
32
|
ソートをする箇所のHTMLです。
|
32
33
|
```html
|
3
記述内容の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -69,7 +69,6 @@
|
|
69
69
|
</form>
|
70
70
|
```
|
71
71
|
|
72
|
-
posts = Post.query.filter(Post.number >
|
72
|
+
posts = Post.query.filter(Post.number > min_number_int)の結果が出てindex.htmlを表示している状態から、ドロップダウンのソートを選んだ場合に検索結果に対してorderbyしたいのですが、
|
73
|
-
ドロップダウンのソートを選んだ場合にその結果をorderbyしたいのですが、
|
74
|
-
posts = Post.query.filter(Post.number >
|
73
|
+
posts = Post.query.filter(Post.number > min_number_int)の結果を保持したまま、再ドロップダウンからsort by number を選択した際に、posts = Post.query.order_by('number').all()で、postsの内容を上書きしないためにはどうしたら良いでしょうか。
|
75
74
|
|
2
記述内容の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,9 +15,9 @@
|
|
15
15
|
```python
|
16
16
|
@bp.route('/', methods=['POST'])
|
17
17
|
def post():
|
18
|
-
number:str = request.form.get('number')
|
18
|
+
min_number:str = request.form.get('min_number')
|
19
|
-
number_int = int(number)
|
19
|
+
min_number_int = int(min_number)
|
20
|
-
posts = Post.query.filter(Post.number > number)
|
20
|
+
posts = Post.query.filter(Post.number > min_number_int)
|
21
21
|
return render_template('index.html', posts=posts)
|
22
22
|
```
|
23
23
|
これはドロップダウンのソートをした時の実装です。
|
@@ -28,8 +28,48 @@
|
|
28
28
|
if sort_by== "number":
|
29
29
|
posts = Post.query.order_by('number').all()
|
30
30
|
```
|
31
|
+
ソートをする箇所のHTMLです。
|
32
|
+
```html
|
33
|
+
<select name="sort_by" id="sort_by" class="form-select " aria-label="Default select example" style="width:150px;">
|
34
|
+
<option value="" >Default</option>
|
35
|
+
<option value="number">Sort by number</option>
|
36
|
+
</select>
|
37
|
+
</div>
|
38
|
+
<script type="text/javascript">
|
39
|
+
var sort_by = document.getElementById('sort_by');
|
40
|
+
sort_by.onchange = function() {
|
41
|
+
var sort_by_paramater = this.options[this.selectedIndex].value;
|
42
|
+
document.location.href = '/?sort_by='+sort_by_paramater;
|
43
|
+
};
|
44
|
+
|
45
|
+
window.onload=function(){
|
46
|
+
let params = (new URL(document.location)).searchParams;
|
47
|
+
let name = params.get('sort_by');
|
48
|
+
var sort_by_elems = document.getElementById('sort_by');
|
49
|
+
checkSelect(sort_by_elems,name);
|
50
|
+
}
|
51
|
+
function checkSelect(obj,val){
|
52
|
+
for(var i=0;i<obj.length;i++){
|
53
|
+
if(obj[i].value==val){
|
54
|
+
obj[i].selected=true;
|
55
|
+
break;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
</script>
|
60
|
+
|
61
|
+
```
|
62
|
+
検索をしている箇所です
|
63
|
+
```html
|
64
|
+
<form action="/" method="POST" enctype="multipart/form-data">
|
65
|
+
<input type="text" placeholder="min_number" name="min_number" class="form-control mr-2" aria-label="Sizing example input"
|
66
|
+
aria-describedby="inputGroup-sizing-sm">
|
67
|
+
<input type="submit" class="btn base-button d-block w-100" value="検索">
|
68
|
+
</div>
|
69
|
+
</form>
|
70
|
+
```
|
31
71
|
|
32
72
|
posts = Post.query.filter(Post.number >5)の結果が出てindex.htmlを表示している状態から、
|
33
73
|
ドロップダウンのソートを選んだ場合にその結果をorderbyしたいのですが、
|
34
|
-
posts = Post.query.filter(Post.number >5)の結果はど
|
74
|
+
posts = Post.query.filter(Post.number >5)の結果を保持したまま、再ドロップダウンからsort by number を選択した際に、posts = Post.query.order_by('number').all()で、postsの内容を上書きしないためにはどうしたら良いでしょうか。
|
35
75
|
|
1
該当コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,7 +15,9 @@
|
|
15
15
|
```python
|
16
16
|
@bp.route('/', methods=['POST'])
|
17
17
|
def post():
|
18
|
+
number:str = request.form.get('number')
|
19
|
+
number_int = int(number)
|
18
|
-
posts = Post.query.filter(Post.number >
|
20
|
+
posts = Post.query.filter(Post.number > number)
|
19
21
|
return render_template('index.html', posts=posts)
|
20
22
|
```
|
21
23
|
これはドロップダウンのソートをした時の実装です。
|