From c58e96e630dc546e66bc3ddcfd256ec30395e5cf Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Thu, 20 Dec 2018 10:21:05 +0100 Subject: Some fixes and new options for select component with AJAX data source. --- .../pl/treksoft/kvision/form/select/AjaxOptions.kt | 17 ++++++++++------- .../pl/treksoft/kvision/form/select/SelectInput.kt | 19 +++++++++++-------- .../pl/treksoft/kvision/form/select/SelectOption.kt | 9 ++++++++- 3 files changed, 29 insertions(+), 16 deletions(-) (limited to 'kvision-modules/kvision-select/src/main/kotlin/pl/treksoft') diff --git a/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt b/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt index fb1ad2fd..c088f68d 100644 --- a/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt +++ b/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt @@ -77,8 +77,8 @@ enum class DataType(internal val type: String) { * option */ data class AjaxOptions( - val url: String, - val preprocessData: (dynamic) -> dynamic, + val url: String? = null, + val preprocessData: ((dynamic) -> dynamic)? = null, val beforeSend: ((JQueryXHR, dynamic) -> dynamic)? = null, val data: dynamic = null, val httpType: HttpType = HttpType.GET, @@ -89,7 +89,9 @@ data class AjaxOptions( val clearOnError: Boolean = true, val emptyRequest: Boolean = false, val requestDelay: Int = AJAX_REQUEST_DELAY, - val restoreOnError: Boolean = false + val restoreOnError: Boolean = false, + val preserveSelected: Boolean = false, + val processData: Boolean = false ) /** @@ -99,7 +101,7 @@ data class AjaxOptions( */ fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { val procData = { data: dynamic -> - val processedData = this.preprocessData(data) + val processedData = this.preprocessData?.invoke(data) ?: data if (emptyOption) { val ret = mutableListOf(obj { this.value = KVNULL @@ -115,7 +117,7 @@ fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { val language = I18n.language return obj { this.ajax = obj { - this.url = url + this.url = url ?: "/" this.method = httpType.type this.dataType = dataType.type this.data = data @@ -128,10 +130,11 @@ fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { this.clearOnEmpty = clearOnEmpty this.clearOnError = clearOnError this.emptyRequest = emptyRequest - this.preserveSelected = false + this.preserveSelected = preserveSelected this.requestDelay = requestDelay this.restoreOnError = restoreOnError this.langCode = language - this.processData = false + this.processData = processData + this.preserveSelectedOrder = true } } diff --git a/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt b/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt index 30609907..27cbb1f0 100644 --- a/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt +++ b/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt @@ -234,9 +234,10 @@ open class SelectInput( return cl } - private fun refreshSelectInput() { + protected fun refreshSelectInput() { getElementJQueryD()?.selectpicker("refresh") refreshState() + getElementJQueryD()?.trigger("change")?.data("AjaxBootstrapSelect")?.list?.cache = {} } @Suppress("ComplexMethod") @@ -321,13 +322,15 @@ open class SelectInput( @Suppress("UnsafeCastFromDynamic") private fun refreshState() { - value?.let { - if (multiple) { - getElementJQueryD()?.selectpicker("val", it.split(",").toTypedArray()) - } else { - getElementJQueryD()?.selectpicker("val", it) - } - } ?: getElementJQueryD()?.selectpicker("val", null) + if (ajaxOptions == null) { + value?.let { + if (multiple) { + getElementJQueryD()?.selectpicker("val", it.split(",").toTypedArray()) + } else { + getElementJQueryD()?.selectpicker("val", it) + } + } ?: getElementJQueryD()?.selectpicker("val", null) + } } /** diff --git a/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt b/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt index d1bb636e..da6ac405 100644 --- a/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt +++ b/kvision-modules/kvision-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectOption.kt @@ -39,7 +39,7 @@ import pl.treksoft.kvision.core.Widget */ open class SelectOption( value: String? = null, label: String? = null, subtext: String? = null, icon: String? = null, - divider: Boolean = false, disabled: Boolean = false, + divider: Boolean = false, disabled: Boolean = false, selected: Boolean = false, classes: Set = setOf() ) : Widget(classes) { @@ -67,6 +67,10 @@ open class SelectOption( * Determines if the option should be disabled. */ var disabled by refreshOnUpdate(disabled) + /** + * Determines if the option is selected. + */ + var selected by refreshOnUpdate(selected) override fun render(): VNode { return if (!divider) { @@ -95,6 +99,9 @@ open class SelectOption( if (disabled) { sn.add("disabled" to "disabled") } + if (selected) { + sn.add("selected" to "selected") + } } else { sn.add("data-divider" to "true") } -- cgit