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

質問編集履歴

3

2022/02/02 03:43

投稿

meron88
meron88

スコア50

title CHANGED
File without changes
body CHANGED
@@ -1,129 +1,132 @@
1
- ### 前提・実現したいこと
2
- ・選択ボタンをクリックされたものを選択済みのテーブルにデータを表示させるという動きを作成しているのですが、
3
- 一度選択ボタンをクリックされたメニューはボタンを非活性にし、
4
- もし選択済みテーブルの解除ボタンをクリックしたら、クリックされたデータの選択ボタンを再度使用できるような作りをしたいのですが、
5
-
6
- 現在だと非活性はできるのですが、解除ボタンを押した際に、再度選択ボタンを使用できるようになっていません。
7
- js初心者の為、データの移動等いいやり方が思いつきません。
8
- 皆様の知恵をお貸しいただきたいです。よろしくお願いいたします。
9
-
10
- ### 該当のソースコード
11
-
12
- ```html
13
- <div class="container table-responsive mb-3" style="max-height: 60vh; overflow-auto: scroll;">
14
- <table class="table table-hover table-bordered">
15
- <thead class="thead-light" style="position: sticky; top: 0; z-index: 1;">
16
- <tr>
17
- <th scope="col" class="text-center">選択</th>
18
- <th scope="col" class="text-center">メニュー名</th>
19
- <th scope="col" class="text-center">メニューCD</th>
20
- </tr>
21
- </thead>
22
- <tbody>
23
- <tr>
24
- <td class="text-center"><button class="btn btn-success ml-2" type="button" onclick="selectStore(this)">選択</button></td>
25
- <td>カレー</td>
26
- <td>A11</td>
27
- </tr>
28
- <tr>
29
- <td class="text-center"><button class="btn btn-success ml-2" type="button" onclick="selectStore(this)">選択</button></td>
30
- <td>ハンバーグ</td>
31
- <td>A12</td>
32
- </tr>
33
- <tr>
34
- <td class="text-center"><button class="btn btn-success ml-2" type="button" onclick="selectStore(this)">選択</button></td>
35
- <td>カツ丼</td>
36
- <td>B3</td>
37
- </tr>
38
- <tr>
39
- <td class="text-center"><button class="btn btn-success ml-2" type="button" onclick="selectStore(this)">選択</button></td>
40
- <td>焼き魚</td>
41
- <td>C1</td>
42
- </tr>
43
- </tbody>
44
- </table>
45
- </div>
46
-
47
- <h5>選択済み店舗</h5>
48
- <div class="container table-responsive" style="max-height: 60vh; overflow-auto: scroll;">
49
- <table class="table table-hover table-bordered" id="targetTable">
50
- <thead class="thead-light" style="position: sticky; top: 0; z-index: 1;">
51
- <tr>
52
- <th scope="col" class="text-center">解除</th>
53
- <th scope="col" class="text-center">メニュー名</th>
54
- <th scope="col" class="text-center">メニューCD</th>
55
- </tr>
56
- </thead>
57
- <tbody>
58
- </tbody>
59
- </table>
60
- </div>
61
- ```
62
- ```javaScript
63
- <script type="application/javascript">
64
-
65
- function selectStore(obj) {
66
-
67
- let Row = obj.closest('tr')
68
- let Cells = Row.getElementsByTagName("td")
69
- let name = Cells[1].innerText
70
- let code = Cells[2].innerText
71
- this.newRow(name, code)
72
- obj.disabled = true
73
-
74
- }
75
-
76
- function newRow(name, code) {
77
-
78
- let table = document.getElementById('targetTable')
79
- let newRow = table.insertRow()
80
- let btn = newRow.insertCell(0)
81
- newRow.innerHTML = '<td style="text-align: center;"><button class="btn btn-secondary ml-2 RowBtn" type="button" onclick="deleteStore(this)">解除</button></td>'
82
-
83
- let newCell = newRow.insertCell(1)
84
- let newText = document.createTextNode(name)
85
- newCell.setAttribute("id", "selectName")
86
- newCell.appendChild(newText)
87
-
88
- newCell = newRow.insertCell(2)
89
- newText = document.createTextNode(code)
90
- newCell.setAttribute("id", "selectCd")
91
- newCell.appendChild(newText)
92
-
93
- const input = document.createElement("input")
94
- input.setAttribute("type", "hidden")
95
- input.setAttribute("name", "menu_name[]")
96
- input.setAttribute("class", "menuName")
97
- input.value = name
98
- document.getElementById("inputForm").appendChild(input)
99
-
100
- const input2 = document.createElement("input")
101
- input2.setAttribute("type", "hidden")
102
- input2.setAttribute("name", "menu_cd[]")
103
- input2.setAttribute("class", "menuCd")
104
- input2.value = code
105
- document.getElementById("inputForm").appendChild(input2)
106
-
107
- }
108
-
109
- function deleteStore(obj) {
110
- let tr = obj.parentNode.parentNode;
111
- tr.parentNode.deleteRow(tr.sectionRowIndex);
112
-
113
- let Row = obj.closest('tr')
114
- let Cells = Row.getElementsByTagName("td")
115
- let deletedMenuName = Cells[1].innerText
116
- let deletedMenuCd = Cells[2].innerText
117
-
118
- for (let element of [...document.getElementsByClassName('menuCd')]) {
119
- if (element.value == deletedMenuName)
120
- element.remove();
121
- }
122
- for (let element of [...document.getElementsByClassName('menuName')]) {
123
- if (element.value == deletedMenuCd)
124
- element.remove();
125
- }
126
- }
127
-
128
- </script>
1
+ ### 前提・実現したいこと
2
+ **選択ボタン**をクリックされたものを選択済みのテーブルにデータを表示させるという動きを作成しているのですが、
3
+ 一度**選択ボタン**をクリックされたメニューはボタンを非活性にし、
4
+ もし選択済みテーブルの**解除ボタン(グレーのボタン)**をクリックしたら、クリックされたデータの**選択ボタン(緑色のボタン)**を再度使用できるような作りをしたいのですが、
5
+
6
+ 現在だと非活性はできるのですが、解除ボタンを押した際に、再度選択ボタンを使用できるようになっていません。
7
+ js初心者の為、データの移動等いいやり方が思いつきません。
8
+ 皆様の知恵をお貸しいただきたいです。よろしくお願いいたします。
9
+
10
+
11
+
12
+
13
+ ### 該当のソースコード
14
+
15
+ ```html
16
+ <div class="container table-responsive mb-3" style="max-height: 60vh; overflow-auto: scroll;">
17
+ <table class="table table-hover table-bordered">
18
+ <thead class="thead-light" style="position: sticky; top: 0; z-index: 1;">
19
+ <tr>
20
+ <th scope="col" class="text-center">選択</th>
21
+ <th scope="col" class="text-center">メニュー名</th>
22
+ <th scope="col" class="text-center">メニューCD</th>
23
+ </tr>
24
+ </thead>
25
+ <tbody>
26
+ <tr>
27
+ <td class="text-center"><button class="btn btn-success ml-2" type="button" onclick="selectStore(this)">選択</button></td>
28
+ <td>カレー</td>
29
+ <td>A11</td>
30
+ </tr>
31
+ <tr>
32
+ <td class="text-center"><button class="btn btn-success ml-2" type="button" onclick="selectStore(this)">選択</button></td>
33
+ <td>ハンバーグ</td>
34
+ <td>A12</td>
35
+ </tr>
36
+ <tr>
37
+ <td class="text-center"><button class="btn btn-success ml-2" type="button" onclick="selectStore(this)">選択</button></td>
38
+ <td>カツ丼</td>
39
+ <td>B3</td>
40
+ </tr>
41
+ <tr>
42
+ <td class="text-center"><button class="btn btn-success ml-2" type="button" onclick="selectStore(this)">選択</button></td>
43
+ <td>焼き魚</td>
44
+ <td>C1</td>
45
+ </tr>
46
+ </tbody>
47
+ </table>
48
+ </div>
49
+
50
+ <h5>選択済み店舗</h5>
51
+ <div class="container table-responsive" style="max-height: 60vh; overflow-auto: scroll;">
52
+ <table class="table table-hover table-bordered" id="targetTable">
53
+ <thead class="thead-light" style="position: sticky; top: 0; z-index: 1;">
54
+ <tr>
55
+ <th scope="col" class="text-center">解除</th>
56
+ <th scope="col" class="text-center">メニュー名</th>
57
+ <th scope="col" class="text-center">メニューCD</th>
58
+ </tr>
59
+ </thead>
60
+ <tbody>
61
+ </tbody>
62
+ </table>
63
+ </div>
64
+ ```
65
+ ```javaScript
66
+ <script type="application/javascript">
67
+
68
+ function selectStore(obj) {
69
+
70
+ let Row = obj.closest('tr')
71
+ let Cells = Row.getElementsByTagName("td")
72
+ let name = Cells[1].innerText
73
+ let code = Cells[2].innerText
74
+ this.newRow(name, code)
75
+ obj.disabled = true
76
+
77
+ }
78
+
79
+ function newRow(name, code) {
80
+
81
+ let table = document.getElementById('targetTable')
82
+ let newRow = table.insertRow()
83
+ let btn = newRow.insertCell(0)
84
+ newRow.innerHTML = '<td style="text-align: center;"><button class="btn btn-secondary ml-2 RowBtn" type="button" onclick="deleteStore(this)">解除</button></td>'
85
+
86
+ let newCell = newRow.insertCell(1)
87
+ let newText = document.createTextNode(name)
88
+ newCell.setAttribute("id", "selectName")
89
+ newCell.appendChild(newText)
90
+
91
+ newCell = newRow.insertCell(2)
92
+ newText = document.createTextNode(code)
93
+ newCell.setAttribute("id", "selectCd")
94
+ newCell.appendChild(newText)
95
+
96
+ const input = document.createElement("input")
97
+ input.setAttribute("type", "hidden")
98
+ input.setAttribute("name", "menu_name[]")
99
+ input.setAttribute("class", "menuName")
100
+ input.value = name
101
+ document.getElementById("inputForm").appendChild(input)
102
+
103
+ const input2 = document.createElement("input")
104
+ input2.setAttribute("type", "hidden")
105
+ input2.setAttribute("name", "menu_cd[]")
106
+ input2.setAttribute("class", "menuCd")
107
+ input2.value = code
108
+ document.getElementById("inputForm").appendChild(input2)
109
+
110
+ }
111
+
112
+ function deleteStore(obj) {
113
+ let tr = obj.parentNode.parentNode;
114
+ tr.parentNode.deleteRow(tr.sectionRowIndex);
115
+
116
+ let Row = obj.closest('tr')
117
+ let Cells = Row.getElementsByTagName("td")
118
+ let deletedMenuName = Cells[1].innerText
119
+ let deletedMenuCd = Cells[2].innerText
120
+
121
+ for (let element of [...document.getElementsByClassName('menuCd')]) {
122
+ if (element.value == deletedMenuName)
123
+ element.remove();
124
+ }
125
+ for (let element of [...document.getElementsByClassName('menuName')]) {
126
+ if (element.value == deletedMenuCd)
127
+ element.remove();
128
+ }
129
+ }
130
+
131
+ </script>
129
132
  ```

2

2021/12/03 18:18

投稿

meron88
meron88

スコア50

title CHANGED
File without changes
body CHANGED
@@ -1,16 +1,12 @@
1
1
  ### 前提・実現したいこと
2
- **選択ボタン**をクリックされたものを選択済みのテーブルにデータを表示させるという動きを作成しているのですが、
2
+ ・選択ボタンをクリックされたものを選択済みのテーブルにデータを表示させるという動きを作成しているのですが、
3
- 一度**選択ボタン**をクリックされたメニューはボタンを非活性にし、
3
+ 一度選択ボタンをクリックされたメニューはボタンを非活性にし、
4
- もし選択済みテーブルの**解除ボタン(グレーのボタン)**をクリックしたら、クリックされたデータの**選択ボタン(緑色のボタン)**を再度使用できるような作りをしたいのですが、
4
+ もし選択済みテーブルの解除ボタンをクリックしたら、クリックされたデータの選択ボタンを再度使用できるような作りをしたいのですが、
5
5
 
6
6
  現在だと非活性はできるのですが、解除ボタンを押した際に、再度選択ボタンを使用できるようになっていません。
7
7
  js初心者の為、データの移動等いいやり方が思いつきません。
8
8
  皆様の知恵をお貸しいただきたいです。よろしくお願いいたします。
9
9
 
10
- ![イメージ説明](0a0dfb38687b2dbab29df05c0f2d0e6d.png)
11
-
12
-
13
-
14
10
  ### 該当のソースコード
15
11
 
16
12
  ```html

1

タグ追加しました

2021/12/03 18:17

投稿

meron88
meron88

スコア50

title CHANGED
File without changes
body CHANGED
File without changes