Private Sub Test8 ' Magma (edited)
Dim trTemplate As Tag
trTemplate = Tr.init
Td.up(trTemplate)
Td.up(trTemplate)
Td.up(trTemplate)
Dim Cache As Map = CreateMap("trTemplate": trTemplate)
Dim Data As List = Array(CreateMap("name": "AAA"), CreateMap("name": "BBB"), CreateMap("name": "CCC"))
Dim tbody1 As Tag = Tbody.init
For Each item As Map In Data
' use DeepCloneTag <> Clone
Dim trRow1 As Tag = Cache.Get("trTemplate").As(Tag).DeepCloneTag(Cache.Get("trTemplate").As(Tag))
trRow1.Child(1).text2(item.Get("name")) ' use text2 to be sure
LogColor(trRow1.Build, -16776961)
trRow1.up(tbody1)
Next
LogColor(tbody1.Build, -65536)
End Sub
You mean the b4xlib on GitHub?
I attach here.
Dim trTemplate As Tag
trTemplate.Initialize("tr")
Dim trRow1 As Tag = trTemplate.Clone
trRow1.add(Html.create("td").text("1"))
trRow1.add(Html.create("td").text("AAA"))
trRow1.add(Html.create("td").text("0001"))
Dim trRow2 As Tag = trTemplate.Clone
trRow1.add(Html.create("td").text("2"))
trRow1.add(Html.create("td").text("bbb"))
trRow1.add(Html.create("td").text("0002"))
Log(trRow1.Build)
Log(trRow2.Build)
Private Sub Test5
' Create a dummy data
Dim rows As List
rows.Initialize
rows.Add(CreateMap("id": 1, "code": "T001", "name": "Teddy Bear"))
rows.Add(CreateMap("id": 2, "code": "H001", "name": "Hammer"))
rows.Add(CreateMap("id": 3, "code": "T002", "name": "Optimus Prime"))
' Create a tbody tag
Dim tbody1 As Tag = Tbody.init
For Each row As Map In rows
Dim tr1 As Tag = CreateProductsRow(row)
tr1.up(tbody1)
Next
Log(tbody1.Build) ' Print results to Logs
End Sub
Private Sub CreateProductsRow (data As Map) As Tag
Dim id As Int = data.Get("id")
Dim code As String = data.Get("code")
Dim name As String = data.Get("name")
' Generate the row tag and store for reuse
If context.ContainsKey("row") = False Then
context.Put("row", CreateProductsRowTemplate)
End If
' Retrieve the tag
Dim row1 As Tag = context.Get("row")
' Update tag with new data
row1.Child(0).text2(id)
row1.Child(1).text2(code)
row1.Child(2).text2(name)
Dim td3 As Tag = row1.Child(3)
Dim anchor1 As Tag = td3.Child(0)
anchor1.hxGet($"/api/products/edit/${id}"$)
Dim anchor2 As Tag = td3.Child(1)
anchor2.hxGet($"/api/products/delete/${id}"$)
Return row1
End Sub
Private Sub CreateProductsRowTemplate As Tag
Dim tr1 As Tag = Tr.init
tr1.add(Td.cls("align-middle").sty("text-align: right"))
tr1.add(Td.cls("align-middle"))
tr1.add(Td.cls("align-middle"))
Dim td3 As Tag = Td.cls("align-middle text-center px-1 py-1").up(tr1)
Dim anchor1 As Tag = Anchor.cls("edit text-primary mx-2").up(td3)
anchor1.hxGet("")
anchor1.hxTarget("#modal-content")
anchor1.hxTrigger("click")
anchor1.data("bs-toggle", "modal")
anchor1.data("bs-target", "#modal-container")
anchor1.add(Icon.cls("bi bi-pencil"))
anchor1.attr("title", "Edit")
Dim anchor2 As Tag = Anchor.cls("delete text-danger mx-2").up(td3)
anchor2.hxGet("")
anchor2.hxTarget("#modal-content")
anchor2.hxTrigger("click")
anchor2.data("bs-toggle", "modal")
anchor2.data("bs-target", "#modal-container")
anchor2.add(Icon.cls("bi bi-trash3"))
anchor2.attr("title", "Delete")
Return tr1
End Sub
Private Sub Test6
' Create a dummy data
Dim rows As List
rows.Initialize
rows.Add(CreateMap("id": 1, "code": "T001", "name": "Teddy Bear"))
rows.Add(CreateMap("id": 2, "code": "H001", "name": "Hammer"))
rows.Add(CreateMap("id": 3, "code": "T002", "name": "Optimus Prime"))
' Create a tbody tag
Dim tbody1 As Tag = Tbody.init
For Each data As Map In rows
Dim id As Int = data.Get("id")
Dim code As String = data.Get("code")
Dim name As String = data.Get("name")
Dim tr1 As Tag = Tr.init
tr1.add(Td.cls("align-middle").sty("text-align: right")).text(id)
tr1.add(Td.cls("align-middle")).text(code)
tr1.add(Td.cls("align-middle")).text(name)
Dim td3 As Tag = Td.cls("align-middle text-center px-1 py-1").up(tr1)
Dim anchor1 As Tag = Anchor.cls("edit text-primary mx-2").up(td3)
anchor1.hxGet($"/api/products/edit/${id}"$)
anchor1.hxTarget("#modal-content")
anchor1.hxTrigger("click")
anchor1.data("bs-toggle", "modal")
anchor1.data("bs-target", "#modal-container")
anchor1.add(Icon.cls("bi bi-pencil"))
anchor1.attr("title", "Edit")
Dim anchor2 As Tag = Anchor.cls("delete text-danger mx-2").up(td3)
anchor2.hxGet($"/api/products/delete/${id}"$)
anchor2.hxTarget("#modal-content")
anchor2.hxTrigger("click")
anchor2.data("bs-toggle", "modal")
anchor2.data("bs-target", "#modal-container")
anchor2.add(Icon.cls("bi bi-trash3"))
anchor2.attr("title", "Delete")
tr1.up(tbody1)
Next
Log(tbody1.Build)
End Sub
Try Put the template tag inside a Map and then Get back the value.B4X:Dim trTemplate As Tag trTemplate.Initialize("tr") Dim trRow1 As Tag = trTemplate.Clone trRow1.add(Html.create("td").text("1")) trRow1.add(Html.create("td").text("AAA")) trRow1.add(Html.create("td").text("0001")) Dim trRow2 As Tag = trTemplate.Clone trRow1.add(Html.create("td").text("2")) trRow1.add(Html.create("td").text("bbb")) trRow1.add(Html.create("td").text("0002")) Log(trRow1.Build) Log(trRow2.Build)
edit my code... to understand it...Try Put the template tag inside a Map and then Get back the value.
Here is the edited version to let you understand what I mean:edit my code... to understand it...
Private Sub Test8 ' Magma (edited)
Dim trTemplate As Tag
trTemplate = Tr.init
Td.up(trTemplate)
Td.up(trTemplate)
Td.up(trTemplate)
Dim Cache As Map = CreateMap("trTemplate": trTemplate)
Dim Data As List = Array(CreateMap("name": "AAA"), CreateMap("name": "BBB"), CreateMap("name": "CCC"))
Dim tbody1 As Tag = Tbody.init
For Each item As Map In Data
Dim trRow1 As Tag = Cache.Get("trTemplate").As(Tag).Clone
trRow1.Child(1).text(item.Get("name"))
LogColor(trRow1.Build, -16776961)
trRow1.up(tbody1)
Next
LogColor(tbody1.Build, -65536)
End Sub
trRow1.Child(1).text2(item.Get("name"))
Private Sub Test8 ' Magma (edited)
Dim trTemplate As Tag
trTemplate = Tr.init
Td.up(trTemplate)
Td.up(trTemplate)
Td.up(trTemplate)
Dim Cache As Map = CreateMap("trTemplate": trTemplate)
Dim Data As List = Array(CreateMap("name": "AAA"), CreateMap("name": "BBB"), CreateMap("name": "CCC"))
Dim tbody1 As Tag = Tbody.init
For Each item As Map In Data
' use DeepCloneTag <> Clone
Dim trRow1 As Tag = Cache.Get("trTemplate").As(Tag).DeepCloneTag(Cache.Get("trTemplate").As(Tag))
trRow1.Child(1).text2(item.Get("name")) ' use text2 to be sure
LogColor(trRow1.Build, -16776961)
trRow1.up(tbody1)
Next
LogColor(tbody1.Build, -65536)
End Sub
Public Sub DeepCloneTag(originalTag As Tag) As Tag
Dim newTag As Tag
newTag.Initialize(originalTag.TagName)
' copy attributes
Dim originalAttributes As Map = originalTag.Attributes
For Each key As String In originalAttributes.Keys
Dim value As String = originalAttributes.Get(key)
newTag.Attributes.Put(key, value)
Next
' copy styles
Dim styleString As String = originalTag.StylesAsString
If styleString.Length > 0 Then
newTag.addStyle(styleString)
End If
' copy classes
Dim classString As String = originalTag.ClassesAsString
If classString.Length > 0 Then
newTag.addClass(classString)
End If
' copy mode, flat, indentString
newTag.Mode = originalTag.Mode
newTag.Flat = originalTag.Flat
' Deep clone all children ?
Dim originalChildren As List = originalTag.Children
For i = 0 To originalChildren.Size - 1
Dim Child1 As Object = originalChildren.Get(i)
If Child1 Is Tag Then
' if tag: recursive deep clone
Dim clonedChild As Tag = DeepCloneTag(Child1)
newTag.add(clonedChild)
Else
' if string just add..
newTag.Children.Add(Child1)
End If
Next
Return newTag
End Sub
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?