From 745743976feadb20f3aa7e3ee4a3dfcfd0d22a35 Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Sun, 17 May 2020 21:12:13 +0500 Subject: [PATCH] Ability to specify table header when creating table. --- elements/table.go | 18 ++++++++++++++++++ examples/helloworld/main.go | 10 ++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/elements/table.go b/elements/table.go index 095c387..a19f2d6 100644 --- a/elements/table.go +++ b/elements/table.go @@ -43,6 +43,8 @@ type TableOptions struct { IsHoverable bool IsNarrow bool IsStriped bool + + Header []string } // Table is a controlling structure for "table" HTML element. @@ -108,5 +110,21 @@ func (t *Table) initialize(tableOpts *TableOptions) { t.InitializeGeneric() + if t.options != nil { + if len(t.options.Header) != 0 { + theader := NewTHead() + t.AddChild(theader) + + trow := NewTR() + theader.AddChild(trow) + + for _, header := range t.options.Header { + th := NewTH() + th.SetTextContent(header) + trow.AddChild(th) + } + } + } + t.Object = js.Global.Get(common.HTMLElementDocument).Call(common.JSCallCreateElement, common.HTMLElementTable) } diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index 6543a55..71899ad 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -100,6 +100,8 @@ func constructTable(mainDiv *elements.Div) { IsFullWidth: true, IsHoverable: true, IsNarrow: true, + + Header: []string{"Header", "Line"}, }) tableDiv.AddChild(testTable) @@ -109,14 +111,6 @@ func constructTable(mainDiv *elements.Div) { tableHeaderLine := elements.NewTR() tableHeader.AddChild(tableHeaderLine) - thlOne := elements.NewTH() - thlOne.SetTextContent("Header") - tableHeaderLine.AddChild(thlOne) - - thlTwo := elements.NewTH() - thlTwo.SetTextContent("line") - tableHeaderLine.AddChild(thlTwo) - tableBody := elements.NewTBody() testTable.AddChild(tableBody)