diff --git a/grails-app/assets/javascripts/forms-knockout-bindings.js b/grails-app/assets/javascripts/forms-knockout-bindings.js index abba7af..5707d60 100644 --- a/grails-app/assets/javascripts/forms-knockout-bindings.js +++ b/grails-app/assets/javascripts/forms-knockout-bindings.js @@ -679,7 +679,10 @@ var options = _.defaults(valueAccessor() || {}, defaults); $(element).select2(options).change(function(e) { - model($(element).val()); + if (ko.isWritableObservable(model)) { // Don't try and write the value to a computed. + model($(element).val()); + } + }); if (options.preserveColumnWidth) { diff --git a/src/main/groovy/au/org/ala/ecodata/forms/EditModelWidgetRenderer.groovy b/src/main/groovy/au/org/ala/ecodata/forms/EditModelWidgetRenderer.groovy index a3b44ba..39cd4a8 100644 --- a/src/main/groovy/au/org/ala/ecodata/forms/EditModelWidgetRenderer.groovy +++ b/src/main/groovy/au/org/ala/ecodata/forms/EditModelWidgetRenderer.groovy @@ -134,10 +134,13 @@ public class EditModelWidgetRenderer implements ModelWidgetRenderer { context.writer << "" } + private static boolean isReadOnly(WidgetRenderContext context) { + context.model.readonly || context.dataModel.computed + } @Override void renderSelectMany(WidgetRenderContext context) { - if (context.model.readonly) { + if (isReadOnly(context)) { renderSelectManyAsString(context) } else { @@ -147,16 +150,23 @@ public class EditModelWidgetRenderer implements ModelWidgetRenderer { @Override void renderSelect2Many(WidgetRenderContext context) { - context.databindAttrs.add 'options', context.source + '.constraints' - context.databindAttrs.add 'optionsValue', context.source + '.constraints.value' - context.databindAttrs.add 'optionsText', context.source + '.constraints.text' - String options = "{value: ${context.source}, tags:true, allowClear:false}" - if (context.model.displayOptions) { - options = "_.extend({value:${context.source}}, ${context.source}.displayOptions)" + if (isReadOnly(context)) { + renderSelectManyAsString(context) + } + else { + context.databindAttrs.add 'options', context.source + '.constraints' + context.databindAttrs.add 'optionsValue', context.source + '.constraints.value' + context.databindAttrs.add 'optionsText', context.source + '.constraints.text' + + String options = "{value: ${context.source}, tags:true, allowClear:false}" + if (context.model.displayOptions) { + options = "_.extend({value:${context.source}}, ${context.source}.displayOptions)" + } + context.databindAttrs.add 'multiSelect2', options + context.writer << "" } - context.databindAttrs.add 'multiSelect2', options - context.writer << "" + } @Override