From bcd1d9032d3b2db2c2bdccc80d93d35e69c860ff Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 30 Dec 2019 23:45:40 +0100 Subject: Add support for downloading of the Tabulator table content (#122) --- .../pl/treksoft/kvision/tabulator/Tabulator.kt | 82 ++++++++++++++++++++++ .../pl/treksoft/kvision/tabulator/js/Tabulator.kt | 10 +-- 2 files changed, 88 insertions(+), 4 deletions(-) (limited to 'kvision-modules/kvision-tabulator/src/main') diff --git a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Tabulator.kt b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Tabulator.kt index 34054545..9d9b7ae8 100644 --- a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Tabulator.kt +++ b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Tabulator.kt @@ -38,6 +38,15 @@ import redux.RAction import kotlin.browser.window import pl.treksoft.kvision.tabulator.js.Tabulator as JsTabulator +/** + * Tabulator download data set option. + */ +enum class DownloadSet(internal val set: String) { + ALL("all"), + VISIBLE("visible"), + ACTIVE("active") +} + /** * Tabulator component. * @@ -349,6 +358,79 @@ open class Tabulator( printConfig: dynamic = null ): Unit? = jsTabulator?.print(activeOnly, isStyled, printConfig) + /** + * Download the table content as CSV + * @param fileName downloaded file name + * @param dataSet a data set configuration + * @param delimiter CSV delimiter + * @param includeBOM determines if BOM should be included + * @param newTab determines if download should be open in a new tab + */ + @Suppress("UnsafeCastFromDynamic") + open fun downloadCSV( + fileName: String? = null, + dataSet: DownloadSet = DownloadSet.ACTIVE, + delimiter: Char = ',', + includeBOM: Boolean = false, + newTab: Boolean = false + ): Unit? { + return if (newTab) { + jsTabulator?.downloadToTab("csv", fileName, obj { + this.delimiter = delimiter + this.bom = includeBOM + }, dataSet.set) + } else { + jsTabulator?.download("csv", fileName, obj { + this.delimiter = delimiter + this.bom = includeBOM + }, dataSet.set) + } + } + + /** + * Download the table content as JSON + * @param fileName downloaded file name + * @param dataSet a data set configuration + * @param newTab determines if download should be open in a new tab + */ + @Suppress("UnsafeCastFromDynamic") + open fun downloadJSON( + fileName: String? = null, + dataSet: DownloadSet = DownloadSet.ACTIVE, + newTab: Boolean = false + ): Unit? { + return if (newTab) { + jsTabulator?.downloadToTab("json", fileName, obj {}, dataSet.set) + } else { + jsTabulator?.download("json", fileName, obj {}, dataSet.set) + } + } + + /** + * Download the table content as HTML + * @param fileName downloaded file name + * @param dataSet a data set configuration + * @param style download a html table with matching styling + * @param newTab determines if download should be open in a new tab + */ + @Suppress("UnsafeCastFromDynamic") + open fun downloadHTML( + fileName: String? = null, + dataSet: DownloadSet = DownloadSet.ACTIVE, + style: Boolean = false, + newTab: Boolean = false + ): Unit? { + return if (newTab) { + jsTabulator?.downloadToTab("html", fileName, obj { + this.style = style + }, dataSet.set) + } else { + jsTabulator?.download("html", fileName, obj { + this.style = style + }, dataSet.set) + } + } + /** * Scroll to the row given by id. * @param row id of the row diff --git a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/js/Tabulator.kt b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/js/Tabulator.kt index ea7c917f..508b4662 100644 --- a/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/js/Tabulator.kt +++ b/kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/js/Tabulator.kt @@ -27,15 +27,17 @@ open external class Tabulator { open var options: Options = definedExternally open fun download( downloadType: dynamic /* String /* "json" */ | String /* "csv" */ | String /* "xlsx" */ | String /* "pdf" */ | (columns: Array, data: Any, options: Any, setFileContents: Any) -> Any */, - fileName: String, - params: DownloadOptions? /*= null*/ + fileName: String?, + params: DownloadOptions? /*= null*/, + set: String? ): Unit = definedExternally open fun downloadToTab( downloadType: dynamic /* String /* "json" */ | String /* "csv" */ | String /* "xlsx" */ | String /* "pdf" */ */, - fileName: String, - params: DownloadOptions? /*= null*/ + fileName: String?, + params: DownloadOptions? /*= null*/, + set: String? ): Unit = definedExternally -- cgit