Conditionally editable DataView

0
Hi all, Im am working on a big project/application. In this application I want conditional editability on a DataView. Sadly, the DataView widget of Mendix does only have a boolean setting for editability. Since we are not committing the objects to the database, we can also not use XPath constraints on those entities. Also, setting conditional editability per attribute on that page is not an option, since there are multiple associated DataViews on that page. That would mean poor maintainability as well as the need for multiple booleans to use for the attributes. This leaves me with the alternative to create a custom widget for it. I am already quite far, but am facing an issue. The approach is that I retrieve all child widgets of the DataView the custom widget is in and then iterate over all of them, setting them all to being non-editable, using only one boolean attribute of the caller-of-the-page entity. So far so good. All the visible widgets on the page become non-editable. Sadly we also have a couple of attributes which are, because of a business scenario, not visible when opening the page. When those attribute/widgets become visible, they are always editable. Apparently the dijit function I use, getChildren(true) only retrieves the visible widgets. Does anybody know of a way to get ALL widgets of the DataView of the Caller-of-the-page?
asked
2 answers
2

I see two solutions:

  • Use 2 dataviews on the page, one editable and one non-editable, each conditionally visible with the same snippet inside them
  • Using your widget, set the "readOnly" property of a dataview, and new widgets created inside of the dataview should respect that property. I just tried this successfully in a test project

Here's how I did it:

  • I created a page with a conditionally visible field, hidden by default
  • In Chrome, I opened the page and then the dev tools
  • I selected the surrounding Data View in the Elements tab, then ran this in the console

    var a = dijit.registry.byNode($0);

    a.readOnly = true

  • When I made the hidden field visible, it rendered as a non-editable label

answered
0

To conclude on the full solution, I have implemented the following code in my custom widget:

          // find the enclosing dataview widget
            var widget = dijit.getEnclosingWidget(this.domNode.parentNode);
            if (widget){
                // get all children widgets for the enclosing widget            
                var childWidgets = widget.getChildren(true);
                if (this.enableLogging){
                    console.log('EditableByCondition widget - message: Child Widgets found: ' + childWidgets.length);
                    console.dir(childWidgets);
                }
            }
            var countAttributeWidgets=0;
            // iterate over all children and setting their editability according to boolean variable / outcome of microflow
            if (childWidgets){
                for (var i = 0; i < childWidgets.length; i++) { 
                    // disable edit on all boolean attributes, inputforms and reference lists
                    if (childWidgets[i]._attrType || childWidgets[i]._attribute || childWidgets[i]._assoc){
                        childWidgets[i].set("disabled",true);
                        countAttributeWidgets++;
                    }
                    // disable edit on all initially / conditionally hidden attributes, inputforms and reference lists
                    if (childWidgets[i]._contentNode){
                        childWidgets[i].readOnly=true;
                        countAttributeWidgets++;
                    }
                    // disable buttons on page with class set in widget/Modeler
                    if (childWidgets[i].domNode.type==="button" && dojo.hasClass(childWidgets[i].domNode,this.buttonClass)){
                        childWidgets[i].readOnly=true;
                        childWidgets[i].set("disabled",true);
                        countAttributeWidgets++;
                    }
                }
                if (this.enableLogging){
                    console.log('EditableByCondition widget - message: Attribute Child Widgets disabled: ' + countAttributeWidgets);
                }
            }
answered