Hi, I have a problem with a table in perspective.
The table header is missing when the named query is not returning any data.
I have a script transform that requires the data to be in json format that can make this problem more difficult to solve, I have seen solutions for data in dataset format but I can't use this here.
Share this. (A script that handles datasets is very similar to a script that handles json.)
You can also do your NQ binding in a custom property, returning a dataset (so you have access to ordered column names and types), and then transforming to the table's required JSON separately.
Hi,
This is the transformation script I use, it's used to change the colors on status.
def transform(self, value, quality, timestamp):
style_green = {"backgroundColor": "#33FF57"}
style_red = {"backgroundColor": "#FF3300", "color": "#FFFFFF"}
style_yellow = {"backgroundColor": "#FFFF00"}
style_white = {"backgroundColor": "#FFFFFF"}
newData = []
#iterate over each row in 'value', which is the original result set
for row in value:
#Within the row, iterate over each column
for col in row:
#Create a variable to store the contents of the original *cell'
cell = row [col]
if col == "STATUS":
if cell == "New":
for col in row:
cell = row[col]
row[col] = {"value": cell, "style": style_yellow}
elif cell == "Active":
for col in row:
cell = row[col]
row[col] = {"value": cell, "style": style_green}
elif cell == "Failed":
for col in row:
cell = row[col]
row[col] = {"value": cell, "style": style_red}
else:
row[col] = {"value": cell, "style":style_white}
#Add the modified row to the list we initialzed earlier
newData.append(row)
return newData
type or paste code here
You can't use a dataset if you're modifying styles anyway.
But let's make this script a bit simpler !
This should do the same thing, with one exception: The style is applied to the row, not the cells. But you're applying the same style to each cell, so it should look the same.
def transform(self, value, quality, timestamp):
styles = {
'new': {"backgroundColor": "#FFFF00"},
'active': {"backgroundColor": "#33FF57"},
'failed': {"backgroundColor": "#FF3300", "color": "#FFFFFF"},
'default': {"backgroundColor": "#FFFFFF"}
}
return [
{
'value': row,
'style': styles.get(row['STATUS'].lower(), styles['default'])
} for row in value
]
If you want to still display the headers when there's no data, you'll have to return an empty row, with the proper headers. For example, with the sample data that comes with a table (+ a 'STATUS' column):
def transform(self, value, quality, timestamp):
if value:
styles = {
'new': {"backgroundColor": "#FFFF00"},
'active': {"backgroundColor": "#33FF57"},
'failed': {"backgroundColor": "#FF3300", "color": "#FFFFFF"},
'default': {"backgroundColor": "#FFFFFF"}
}
return [
{
'value': row,
'style': styles.get(row['STATUS'].lower(), styles['default'])
} for row in value
]
else:
return [
{
'country': None,
'city': None,
'population': None,
'STATUS': None
}
]
But there's a property to display a message when there's no data:
Do you really need column headers when the table is empty, or would a message be enough ?
Thanks,
I already use the emptyMessage option and it's working fine.
What I don't want is the blank space where the header is supposed to be.
Create an expression binding on enableHeader
to only show when there is data. Something like,
!isNull({this.props.data})
&&
{this.props.data} != ""
(I don't know what data
will look like in your situation.)
Data will most likely be an empty list, so I'd suggest len({this.props.data}) > 0
, which would also work for an empty string anyway.
Thanks @pascal.fragnoud and @Transistor
That solved the problem.
Good. Hit the Solution button on one or other answer to mark it solved. It then shows up as solved in the main index and a summary of the solution shows up under your original post. (I don't mind which.)