Cursussen/Courses Codesnippets     Top 
B4A-database - Updates


Updates
You can provide an export to a HTML string from the visible data in the table. This HTML string can be displayed in a web view or saved in a HTML file.
Add a button (btnhtml) in the MainPage layout next to the export database button.
Put a webview (wvhtml) on top of the table views and give it the same dimensions as the table views. Make sure to set visible to false.
From the layout generate the members: btnhtml and Click event and wvhtml.
The following code makes the web view visible and shows the HTML string in the web view.
The query in the ExportToHtml subroutine limits the number of records from the table to show.
The query in the comment line will select all records from the table.
EDIT 2022-10-02: when the search field is used the query can be built with the BuildQuery method from the table.
See below for the code. The old code is placed in comments.
Private Sub btnhtml_Click
	B4XTbl1.mBase.Visible = False
	B4XTbl2.mBase.Visible = False
	wvhtml.Visible = True
	Private htmlstring As String
	htmlstring = ExportToHtml(B4XTbl1)
	wvhtml.LoadHtml(htmlstring)
End Sub
Private Sub ExportToHtml (tbl As B4XTable) As String
	Dim HtmlCSS As String = $"
        table {width: 100%;border: 1px solid #cef;text-align: left; }
        th { font-weight: bold;    background-color: #acf;    border-bottom: 1px solid #cef; }
        td,th {    padding: 4px 5px; }
        .odd {background-color: #def; } 
        .odd td {border-bottom: 1px solid #cef; }
        a { text-decoration:none; color: #000;}"$
	Dim sb As StringBuilder
	sb.Initialize
	sb.Append("").Append(CRLF)
	sb.Append("").Append(CRLF)
	sb.Append("").Append(CRLF)
	For Each col As B4XTableColumn In tbl.Columns
		sb.Append("")
	Next
	sb.Append("")
	sb.Append("").Append(CRLF)
	' limit the number of records (remove if all the records are wanted.
	'Dim startrow As Int = tbl.FirstRowIndex
	'Dim numrows As Int = tbl.RowsPerPage
	'Log("startrow: " & startrow)
	'Log("numrows: " & numrows)
	'Dim rs As ResultSet = tbl.sql1.ExecQuery("SELECT * FROM data limit " & startrow & "," & numrows)
	'Dim rs As ResultSet = tbl.sql1.ExecQuery("SELECT * FROM data")
	Dim obj() As Object = tbl.BuildQuery(True)
	Dim sql As String = obj(0)
	Dim values As List = obj(1)
	Dim rs As ResultSet = tbl.sql1.ExecQuery2(sql,values)
	Dim row As Int
	Do While rs.NextRow
		If row Mod 2 = 0 Then
			sb.Append("")
		Else
			sb.Append("")
		End If
		For i = 0 To rs.ColumnCount - 1
			sb.Append("")
		Next
		sb.Append("").Append(CRLF)
		row = row + 1
	Loop
	rs.Close
	sb.Append("
").Append($"$Xml{col.Title}"$).Append("
") sb.Append($"$Xml{rs.GetString2(i)}"$) sb.Append("
") Return sb.ToString End Sub
To return to the structure or table view you can add these lines of code in the subroutine from the switch.
Note: in B4J this subroutine has a different name.
A tap on the switch will show the structure if the switch is turned on.
Private Sub Switch1_CheckedChange(Checked As Boolean)
	If Checked Then
		B4XTbl1.mBase.Visible = False
		B4XTbl2.mBase.Visible = True
		wvhtml.Visible = False
		show_structure
	Else
		B4XTbl1.mBase.Visible = True
		B4XTbl2.mBase.Visible = False
		wvhtml.Visible = False
		fill_B4XTable
	End If
End Sub
The display of the records in the web view also allows you to scroll horizontally and vertically. A pinch operation allows you to zoom in or out.


X

Paragraphs

Updates