diff options
author | Robert Jaros <rjaros@finn.pl> | 2018-12-20 10:21:05 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2018-12-20 10:21:05 +0100 |
commit | c58e96e630dc546e66bc3ddcfd256ec30395e5cf (patch) | |
tree | 57a37a3b4eef54c00359cff504448024b7043ab9 /kvision-modules/kvision-select | |
parent | b995f4b927c230e0ae15d51920390dc6afff6caa (diff) | |
download | kvision-c58e96e630dc546e66bc3ddcfd256ec30395e5cf.tar.gz kvision-c58e96e630dc546e66bc3ddcfd256ec30395e5cf.tar.bz2 kvision-c58e96e630dc546e66bc3ddcfd256ec30395e5cf.zip |
Some fixes and new options for select component with AJAX data source.
Diffstat (limited to 'kvision-modules/kvision-select')
3 files changed, 29 insertions, 16 deletions
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<String> = 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") } |