Question
· May 19, 2020

Getting the HTML contents of a TablePane

Hello!

I have a table pane component on my page.  I want to get the contents of the table to send embedded within an email.  Is there a simple (or not simple) way to get the HTML of the rows and columns to send this way?

Discussion (7)3
Log in or sign up to continue

Hi Arnold,

I do exactly that with the built in tablePane method %DrawHTML()

My method uses code like this:

//get tablePane component - copy to new object? (pointers, etc.)
#dim tblPane as %ZEN.Component.tablePane
set tblPane=%page.%GetComponentById("tblPaneData")
// you have access to the columns here, if you need to change things up
set numCols = tblPane.columns.Size
set lastCol = tblPane.columns.GetAt(numCols)
set hidden = lastCol.hidden
set lastCol.hidden=1

// I'm writing this to a file, for use in a moment...
$$$ThrowOnError(File.Open("WNS",10)) Use File.Name

//Draw HTML headers - Note: HTML is not my strong point
"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 transitional//EN"">",!
"<html>",!

//custom style just for this table (see elements in browser debugger! )

//I must have looked at the tablePane in the browser debugger
"<style>",!
  W "h1,h2 {",!
  W "font-size: 20px;",!
  W "font-weight:bold;",!
  W "font-family: verdana;",!
  W "text-align: left;",!
"}",!
// etc. etc.
"</style>",!

"<head>",!
 W "<meta http-equiv=""Content-Type"" content=""text/css"" charset=""UTF-8"">",!
"</head>",!
"<body>",!

//Write specific table header
set header=$g(%session.Data("Heading One Here"))
"<h1>"_header_"</h1>",!
"<h2>Heading Two Here</h2>",!

//write tablepane element to file
do tblPane.%DrawHTML()

//Finish HTML
" </body>",!
"</html>"

//Close File
do File.Close()

 

Perhaps you can return  string of HTML for your email. We use the html  converted to PDF using a third party converter  tool.  This lets the user print the table.

I hope this helps!

Thanks Laura! I tried this but I get an error when I execute it.  Its almost like its trying to run the output HTML on the page, which it may be?  I was just trying to store the HTML in a variable but it includes all the related JS for the table as well.

#dim tblPane as %ZEN.Component.tablePane
set tblPane = %page.%GetComponentById("userTable")
set tblContent = tblPane.%DrawHTML()

Hi Arnold,

Regarding you error, looks like Cristiano is correct when he said that %DrawHTML writes only to a device, which is what I am doing (%File) so that works for me.  Drawing it to a string won't work. A very roundabout way would be to write the HTML to a file, and then read the file into the body of an email ... 

As for getRowData(), you'd want to start in a ClientMethod (js) and loop through the table's rows, getting the data for each row, and sending that to the server, perhaps to be stored in a global until you're done? Perhaps passing a large JSON would be better, but I don't have that experience. When you're done on the client, you'd go back to the server, loop through your global, and build your email body.  However, I know from experience that getRowData() is very slow, and should be used to get only the 1 row that you need -- it's not good for looping through to get the all of the data.

In that case, perhaps you should revisit Crisitano's suggestion of just running the table's query again, and using the resulting ResultSet to get and print out the data in a pretty HTML string.  I haven't used %DisplayFormatted, but that might have lots of options for you.