Downloading object-table data

Is there an easy way to let my app users download the data in an object table as a CSV file, on a desktop app?

The easiest way is to access one of two built-in variables exposed by the object-table component, namely $filteredData or filteredDisplayData. The former holds all the objects bound to the query / array “powering” the object-table and ignores pagination and any special display formatting, whereas the latter holds an array that represents only what is currently visible in the object-table, taking into account formating, pagination, sorting and filtering.

For example

    <heading>RECENT FIELD TICKETS</heading>
    <object-table label="Select one to view details" query="tickets" empty-message="Your items will appear here" limit="5">
        <column heading="Date">{date}</column>
        <column filter="true" heading="Customer">{customer}</column>
        <column filter="true" heading="Pad">{pad_name}</column>
        <column heading="Well">{well_name}</column>
        <column filter="true" style-background-color="$:$object.rig().color" heading="Rig">{rig}</column>
        <column filter="true" heading="Supervisor">{user}</column>
        <column filter="true" style-background-color="$:sharedStatusColor($object)" heading="Status">{status}</column>
        <column style-align="right" heading="Total">${$:sharedNumberWithCommas($object.ticket_total, 2)}</column>
        
        <action on-press="link.ticket.overview($selection)" />
        
        <button-group>
            <button label="Export to CSV" icon="fa-download" on-press="$:exportToCSV($filteredData, filteredDisplayData, controls, 'recent_field_tickets.csv')" validate="false" style="outline" />
        </button-group>
    </object-table>

function exportToCSV(filteredData, filteredDisplayData, controls, filename) {
    var allData = CSV.stringify(filteredData);
    saveFile(allData, filename);

    // OR //
    
    var displayData = CSV.stringify(filteredDisplayData);
    saveFile(displayData, filename);
}

Given a table like this:

Would produce the following two potential CSV files:
All Data


Display Data

2 Likes