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

回答編集履歴

1

改良版に変更(以前の回答は質問文に移動済み)

2018/02/08 10:37

投稿

teretail
teretail

スコア23

answer CHANGED
@@ -1,61 +1,108 @@
1
+ 関数にControlを渡してコンテンツを追加する方法から、
1
- 想定通りの動きはできました。
2
+ コンテンツを追加したControlを返す方法に変えました。
2
- ただし1行1テーブルで構成されるので重いです・・・
3
+ これにより葉の部分はテーブルにまとめられるようになりましたので、若干軽量化きたと思いま
3
4
 
4
5
  ```vbnet
5
- Private Sub SetContents(ByVal control As ControlCollection, ByVal header As DataRow, ByVal content As DataTable)
6
+ Private Function CreateAccordion(ByVal header As DataRow, ByVal content As DataRow()) As Control
6
7
 
7
- Dim accordion As AjaxControlToolkit.Accordion
8
+ CreateAccordion = Nothing
8
- Dim accordionPane As AjaxControlToolkit.AccordionPane
9
9
 
10
+ Dim contentDT As DataTable = Nothing
11
+ Dim accordion As AjaxControlToolkit.Accordion
12
+ Dim accordionPane As AjaxControlToolkit.AccordionPane
10
- Dim table As Table
13
+ Dim table As Table = Nothing
11
- Dim tableRow As TableRow
14
+ Dim headerRow As TableRow
15
+ Dim children As DataRow() = Nothing
16
+ Dim pattern As String
12
17
 
13
- Try
18
+ Try
14
19
 
15
- ' Accordion Header
20
+ ' Accordion Header
21
+ headerRow = CreateRow(header) ' TableRowを生成する関数(省略)
22
+
23
+ If ((Not content Is Nothing) AndAlso (content.Length > 0)) Then
24
+ ' Get Children
25
+ contentDT = content.CopyToDataTable
26
+ pattern = "xxxx" ' patternは抽出条件(省略)
27
+ children = contentDT.Select(pattern)
28
+ End If
29
+
30
+ If ((Not children Is Nothing) AndAlso (children.Length > 0)) Then
31
+ ' Have children
32
+
33
+ accordion = New AjaxControlToolkit.Accordion
34
+ accordion.ID = "accordion_" + header("xxx")
35
+ accordion.ContentCssClass = "accordionContent"
36
+ accordion.HeaderCssClass = "accordionHeader"
37
+ accordion.RequireOpenedPane = False
38
+ accordionPane = New AjaxControlToolkit.AccordionPane
39
+ accordionPane.ID = "accordionPane_" + header("xxx")
40
+
41
+ ' Add Header
16
42
  table = New Table
17
- tableRow = CreateRow(header) ' TableRowを生成する関数(省略)
43
+ table.ID = "table_" + header("xxx")
18
- table.Controls.Add(tableRow)
44
+ table.Controls.Add(headerRow)
45
+ accordionPane.HeaderContainer.Controls.Add(table)
19
46
 
20
- contentRows = content.Select(pattern) ' patternは抽出条件(省略)
47
+ table = Nothing
21
48
 
22
- If (contentRows.Length = 0) Then
49
+ For Each row As DataRow In children
23
- ' コンテンツなし
24
- control.Add(table)
25
50
 
26
- Else
27
- ' コンテンツあり
51
+ pattern = "xxxx" ' patternは抽出条件(省略)
52
+ Dim control As Control = CreateAccordion(row, contentDT.Select(pattern))
28
53
 
29
- accordion = New AjaxControlToolkit.Accordion
54
+ If (TypeOf control Is TableRow) Then
30
- accordion.ID = "accordion_" + header("xxx")
31
- accordion.ContentCssClass = "accordionContent"
32
- accordion.HeaderCssClass = "accordionHeader"
33
- accordion.RequireOpenedPane = False
55
+ ' Child-Node is TableRow
34
- accordionPane = New AjaxControlToolkit.AccordionPane
35
- accordionPane.ID = "accordionPane_" + header("xxx")
36
56
 
37
- ' ヘッダー追加
57
+ If (table Is Nothing) Then
38
- accordionPane.HeaderContainer.Controls.Add(table)
58
+ ' Table is not created yet
39
59
 
40
- ' コンテンツの追加は、再帰呼出しによって行う
41
- For Each row As DataRow In contentRows
60
+ table = New Table
42
61
 
43
- SetContents(accordionPane.ContentContainer.Controls, row, content)
62
+ End If
44
63
 
45
- Next
64
+ ' Add Child-Node
65
+ table.Controls.Add(control)
46
66
 
47
- ' ペインの追加
67
+ Else
48
- accordion.Panes.Add(accordionPane)
68
+ ' Child-Node is Accordion
49
69
 
50
- ' 作成したアコーディオンを追加する
51
- control.Add(accordion)
70
+ If (Not table Is Nothing) Then
71
+ ' table is created -> add first
52
72
 
73
+ accordionPane.ContentContainer.Controls.Add(table)
74
+ table = Nothing
75
+
76
+ End If
77
+
78
+ ' Add Child-Node
79
+ accordionPane.ContentContainer.Controls.Add(control)
80
+
81
+ End If
82
+
83
+ Next
84
+
85
+ If (Not table Is Nothing) Then
86
+ ' table still remain
87
+ accordionPane.ContentContainer.Controls.Add(table)
88
+
53
89
  End If
54
90
 
91
+ accordion.Panes.Add(accordionPane)
55
- Catch ex As Exception
92
+ CreateAccordion = accordion
56
- Throw
57
- End Try
58
93
 
59
- End Sub
94
+ Else
95
+ ' Have no children
60
96
 
97
+ CreateAccordion = headerRow
98
+
99
+ End If
100
+
101
+ Catch ex As Exception
102
+ Throw
103
+ End Try
104
+
105
+ End Function
61
- ```
106
+ ```
107
+ これで締めさせていただきます。
108
+ ありがとうございました。