Convert Table filtered data to downloaded excel sheet

Hello guys,
I succeeded to download the table data [self.props.data] to excel sheet using a button as below :

But I failed to download the filtered data [props.filter.results.data]of the table using a button .
Can you help me in this as I find the filtered data is not a dataset so how to fix my code .

# implement your handler here
fileName = 'HighBay Main Search '+self.props.filter.text+'.xls'
excelbytes = system.dataset.dataSetToHTML(1, self.props.data, )
system.perspective.download(fileName, excelbytes)

Do you need to specify payload = {} in your sendMessage?

I found the solution.

props.filter.results.data is a dictionary that should be converted to dataset.

Blockquote

myList =
for key in value:
Header=key.keys()
myList.append(key.values())
myorder = [5,6, 2,1, 3, 0,4]
myList = [[sublst[i] for i in myorder] for sublst in myList]
Header = [Header[i] for i in myorder]

return system.dataset.toDataSet(Header, myList)

Blockquote

You can actually use system.dataset.filterColumns() to reorder the columns of a dataset.

header = []
data = []
for key in value:
    if not header:
        header = key.keys()
    data.append(key.values())
return system.dataset.filterColumns(system.dataset.toDataSet(header,data),[5,6,2,1,3,0,4])

Also, be carful with scope. In your script the list Header is only defined inside of the for loop. If for some reason value is None or empty, then your script will result in an error.

Also, since the column names should always match, there is no need to get the column names more than once.

3 Likes

Don't use blockquote to format code. Use the preformatted text button.

This will preserve indentation (essential for Python) and apply syntax highlighting.