aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2019-12-30 23:45:40 +0100
committerRobert Jaros <rjaros@finn.pl>2019-12-30 23:45:40 +0100
commitbcd1d9032d3b2db2c2bdccc80d93d35e69c860ff (patch)
tree3ee60e5e196808a1dba01a979143625fb7bdd77f /kvision-modules
parentd0b53c664b435d03e99f0d6c6e9ffbbc1e4929e0 (diff)
downloadkvision-bcd1d9032d3b2db2c2bdccc80d93d35e69c860ff.tar.gz
kvision-bcd1d9032d3b2db2c2bdccc80d93d35e69c860ff.tar.bz2
kvision-bcd1d9032d3b2db2c2bdccc80d93d35e69c860ff.zip
Add support for downloading of the Tabulator table content (#122)
Diffstat (limited to 'kvision-modules')
-rw-r--r--kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/Tabulator.kt82
-rw-r--r--kvision-modules/kvision-tabulator/src/main/kotlin/pl/treksoft/kvision/tabulator/js/Tabulator.kt10
2 files changed, 88 insertions, 4 deletions
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
@@ -39,6 +39,15 @@ 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.
*
* @constructor
@@ -350,6 +359,79 @@ open class Tabulator<T : Any>(
): 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
* @param position the scrolling position
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<Tabulator.ColumnDefinition>, 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