Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to Reset to Default ViewState?

    I'm trying to reset ListGrid.ViewState to the original ViewState per initial grid draw, prior to any user changes.

    JavaDoc reads "Default value is null." From that I inferred that "setViewState(null)" should work, but it does not. Neither does setViewState("") or setViewState(initialViewState)

    I've added some code to the BuiltInDS to demonstrate the issue.

    DEMO STEPS
    Select Animals DataSource
    Make ViewState changes (doesn't matter which)
    Save ViewState (demo button)
    Make more ViewState Changes

    THIS WORKS AS EXPECTED
    Restore ViewState (throws occassional JS Warning, see below)

    THIS DOESN'T: No effect, no JS Warning
    Clear ViewState
    --------

    Any guidance appreciated, thanks.

    Code:
    package com.smartgwt.sample.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.smartgwt.client.core.KeyIdentifier;
    import com.smartgwt.client.data.DSCallback;
    import com.smartgwt.client.data.DSRequest;
    import com.smartgwt.client.data.DSResponse;
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.data.Record;
    import com.smartgwt.client.types.SelectionStyle;
    import com.smartgwt.client.types.SortArrow;
    import com.smartgwt.client.util.PageKeyHandler;
    import com.smartgwt.client.util.Page;
    import com.smartgwt.client.util.SC;
    import com.smartgwt.client.widgets.IButton;
    import com.smartgwt.client.widgets.Label;
    import com.smartgwt.client.widgets.events.ClickEvent;
    import com.smartgwt.client.widgets.events.ClickHandler;
    import com.smartgwt.client.widgets.form.DynamicForm;
    import com.smartgwt.client.widgets.grid.ListGrid;
    import com.smartgwt.client.widgets.grid.ListGridField;
    import com.smartgwt.client.widgets.grid.ListGridRecord;
    import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
    import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
    import com.smartgwt.client.widgets.layout.HLayout;
    import com.smartgwt.client.widgets.layout.VStack;
    import com.smartgwt.client.widgets.viewer.DetailViewer;
    
    /**
     * Entry point classes define <code>onModuleLoad()</code>.
     */
    public class BuiltInDS implements EntryPoint {
        private ListGrid boundList;
        private DynamicForm boundForm;
        private IButton saveBtn;
        private DetailViewer boundViewer;
        private IButton newBtn;
    
        private String initialViewState;
        private String viewState;
    
        /**
         * This is the entry point method.
         */
        public void onModuleLoad() {
            KeyIdentifier debugKey = new KeyIdentifier();
            debugKey.setCtrlKey(true);
            debugKey.setKeyName("D");
    
            Page.registerKey(debugKey, new PageKeyHandler() {
                public void execute(String keyName) {
                    SC.showConsole();
                }
            });
    
            final ListGrid grid = new ListGrid();
            grid.setLeft(20);
            grid.setTop(75);
            grid.setWidth(130);
            grid.setLeaveScrollbarGap(false);
            grid.setShowSortArrow(SortArrow.NONE);
            grid.setCanSort(false);
            grid.setFields(new ListGridField("dsTitle", "Select a DataSource"));
            grid.setData(new ListGridRecord[] { new DSRecord("Animals", "animals"),
                    new DSRecord("Office Supplies", "supplyItem"), new DSRecord("Employees", "employees") });
            grid.setSelectionType(SelectionStyle.SINGLE);
            grid.addRecordClickHandler(new RecordClickHandler() {
                public void onRecordClick(RecordClickEvent event) {
                    DSRecord record = (DSRecord) event.getRecord();
                    bindComponents(record.getDsName());
    ///////////////////////////  DEMO CODE  /////////////////////////////////////
                    initialViewState = boundList.getViewState();
                    SC.say("Initial ViewState<br />" + initialViewState);
    ///////////////////////////  DEMO CODE  /////////////////////////////////////
                }
            });
    
            grid.draw();
    
            VStack vStack = new VStack();
            vStack.setLeft(175);
            vStack.setTop(75);
            vStack.setWidth("70%");
            vStack.setMembersMargin(20);
    
            Label label = new Label();
            label.setContents("<ul>" + "<li>select a datasource from the list at left to bind to these components</li>"
                    + "<li>click a record in the grid to view and edit that record in the form</li>"
                    + "<li>click <b>New</b> to start editing a new record in the form</li>"
                    + "<li>click <b>Save</b> to save changes to a new or edited record in the form</li>"
                    + "<li>click <b>Clear</b> to clear all fields in the form</li>"
                    + "<li>click <b>Filter</b> to filter (substring match) the grid based on form values</li>"
                    + "<li>click <b>Fetch</b> to fetch records (exact match) for the grid based on form values</li>"
                    + "<li>double-click a record in the grid to edit inline (press Return, or arrow/tab to another record, to save)</li>"
                    + "</ul>");
            vStack.addMember(label);
    
            boundList = new ListGrid();
            boundList.setHeight(200);
            boundList.setCanEdit(true);
    
            boundList.addRecordClickHandler(new RecordClickHandler() {
                public void onRecordClick(RecordClickEvent event) {
                    Record record = event.getRecord();
                    boundForm.editRecord(record);
                    saveBtn.enable();
                    boundViewer.viewSelectedData(boundList);
                }
            });
            vStack.addMember(boundList);
    
            boundForm = new DynamicForm();
            boundForm.setNumCols(6);
            boundForm.setAutoFocus(false);
            vStack.addMember(boundForm);
    
            HLayout hLayout = new HLayout(10);
            hLayout.setMembersMargin(10);
            hLayout.setHeight(22);
    
            saveBtn = new IButton("Save");
            saveBtn.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    boundForm.saveData(new DSCallback() {
                        @Override
                        public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest) {
                            if (dsResponse.getStatus() == DSResponse.STATUS_SUCCESS) {
                                // if the save succeeded, clear the UI
                                boundForm.clearValues();
                                saveBtn.disable();
                            }
                        }
                    });
                }
            });
            hLayout.addMember(saveBtn);
    
            newBtn = new IButton("New");
            newBtn.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    boundForm.editNewRecord();
                    saveBtn.enable();
                }
            });
            hLayout.addMember(newBtn);
    
            IButton clearBtn = new IButton("Clear");
            clearBtn.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    boundForm.clearValues();
                    saveBtn.disable();
                }
            });
            hLayout.addMember(clearBtn);
    
            IButton filterBtn = new IButton("Filter");
            filterBtn.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    boundList.filterData(boundForm.getValuesAsCriteria());
                    saveBtn.disable();
                }
            });
            hLayout.addMember(filterBtn);
    
            IButton fetchBtn = new IButton("Fetch");
            fetchBtn.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    boundList.fetchData(boundForm.getValuesAsCriteria());
                    saveBtn.disable();
                }
            });
            hLayout.addMember(fetchBtn);
    
    ///////////////////////////  DEMO CODE  /////////////////////////////////////
            IButton saveViewState = new IButton("Save ViewState");
            saveViewState.addClickHandler(new ClickHandler() {
    
                @Override
                public void onClick(ClickEvent event) {
    
                    viewState = boundList.getViewState();
    
                    SC.say("ViewState Saved!<br />" + viewState);
                }
            });
    
            hLayout.addMember(saveViewState);
    
            IButton restoreViewState = new IButton("Restore Viewstate");
            restoreViewState.addClickHandler(new ClickHandler() {
    
                @Override
                public void onClick(ClickEvent event) {
    
                    boundList.setViewState(viewState);
    
                    SC.say("Restore ViewState<br />" + viewState);
                }
            });
            hLayout.addMember(restoreViewState);
    
            IButton clearViewState = new IButton("Clear ViewState");
            clearViewState.addClickHandler(new ClickHandler() {
    
                @Override
                public void onClick(ClickEvent event) {
    
                    // Try these, one at-a-time
                    String tryThis;
    //                tryThis = null;
    //                tryThis = "";
                    tryThis = initialViewState;
    
                    boundList.setViewState(tryThis);
    
                    SC.say("Clear ViewState<br />'" + tryThis + "'");
    
                }
            });
            hLayout.addMember(clearViewState);
    
    ///////////////////////////  DEMO CODE  /////////////////////////////////////
    
            vStack.addMember(hLayout);
    
            boundViewer = new DetailViewer();
            vStack.addMember(boundViewer);
    
            vStack.draw();
        }
    
        private void bindComponents(String dsName) {
            DataSource ds = DataSource.get(dsName);
            boundList.setDataSource(ds);
            boundViewer.setDataSource(ds);
            boundForm.setDataSource(ds);
            boundList.fetchData();
            newBtn.enable();
            saveBtn.disable();
        }
    }

    Sometimes see JS warnings like this, but not always...
    Code:
    15:37:04.827:MUP3:WARN:drawing:isc_globalWarn_body:Attempt to draw child of an undrawn parent - ignoring
        Canvas.readyToDraw() on [Layout ID:isc_globalWarn_body] @ ISC_Core.js:2614:76
        Canvas.draw(_1=>undef) on [Layout ID:isc_globalWarn_body] @ ISC_Core.js:2619:94
        Window._matchBodyWidth() on [Dialog ID:isc_globalWarn] @ ISC_Containers.js:143:268
        Window.layoutChildren(_1=>"resized", _2=>-150, _3=>0, _4=>undef) on [Dialog ID:isc_globalWarn] @ ISC_Containers.js:141:23
        Canvas._completeResizeBy(_1=>undef, _2=>undef) on [Dialog ID:isc_globalWarn] @ ISC_Core.js:3136:64
        Canvas.resizeBy(_1=>-150, _2=>null, _3=>undef, _4=>undef, _5=>undef, _6=>undef) on [Dialog ID:isc_globalWarn] @ ISC_Core.js:3134:6
        [c]Class.invokeSuper(_1=>[Class Window], _2=>"resizeBy", _3=>-150, _4=>null, _5=>undef, _6=>undef, _7=>undef, _8=>undef, _9=>undef, _10=>undef, _11=>undef, _12=>undef, _13=>undef) on [Dialog ID:isc_globalWarn] @ ISC_Core.js:313:162
        Window.resizeBy(_1=>-150, _2=>null, _3=>undef, _4=>undef, _5=>undef) on [Dialog ID:isc_globalWarn] @ ISC_Containers.js:194:13
        Canvas.resizeTo(_1=>360, _2=>undef, _3=>undef, _4=>undef, _5=>undef, _6=>undef) on [Dialog ID:isc_globalWarn] @ ISC_Core.js:3148:446
        [c]Class.invokeSuper(<no args: recursion>)  on [Dialog ID:isc_globalWarn] @ ISC_Core.js:313:162
        Window.resizeTo(_1=>360, _2=>undef, _3=>undef, _4=>undef, _5=>undef) on [Dialog ID:isc_globalWarn] @ ISC_Containers.js:193:13
        Canvas.setWidth(_1=>360) on [Dialog ID:isc_globalWarn] @ ISC_Core.js:2885:640
        Class.setProperties() on [Dialog ID:isc_globalWarn] @ ISC_Core.js:371:109
        _3.showMessage(_1=>"Clear ViewState<br />'({field:[{name:"co..."[178], _2=>Obj{title:Note}) on [Dialog ID:isc_globalWarn] @ ISC_Containers.js:247:6
        <anonymous>(_1=>"Clear ViewState<br />'({field:[{name:"co..."[178], _2=>"say", _3=>undef, _4=>Obj{title:Note}) @ ISC_Containers.js:260:17
        <anonymous>(_1=>"Clear ViewState<br />'({field:[{name:"co..."[178], _2=>undef, _3=>undef) @ ISC_Containers.js:261:119
        StatefulCanvas.handleActivate(_1=>Obj, _2=>undef) on [IButton ID:isc_IButton_7] @ ISC_Foundation.js:234:108
        StatefulCanvas.handleClick(_1=>Obj, _2=>undef) on [IButton ID:isc_IButton_7] @ ISC_Foundation.js:235:13
        [c]EventHandler.bubbleEvent(_1=>[IButton ID:isc_IButton_7], _2=>"click", _3=>undef, _4=>undef, _5=>undef) on [Class EventHandler] @ ISC_Core.js:2149:89
        [c]EventHandler.handleClick(_1=>[IButton ID:isc_IButton_7], _2=>undef) on [Class EventHandler] @ ISC_Core.js:1984:50
        EventHandler._handleMouseUp(_1=>[object MouseEvent], _2=>undef) on [Class EventHandler] @ ISC_Core.js:1968:11
        [c]EventHandler.handleMouseUp(_1=>[object MouseEvent], _2=>undef) on [Class EventHandler] @ ISC_Core.js:1959:57
        [c]EventHandler.dispatch(_1=>[c]EventHandler.handleMouseUp(), _2=>[object MouseEvent]) on [Class EventHandler] @ ISC_Core.js:2236:108
        HTMLDocument.eval(event=>[object MouseEvent]) @ [no file]:3:123

    Chrome Version 77.0.3865.90 (Official Build) (64-bit)

    SmartClient Version: v12.0p_2019-10-06/PowerEdition Deployment (built 2019-10-06)

    #2
    We're not able to reproduce the issue you reported, using your sample code and the exact version of SmartGWT mentioned. The "Clear ViewState" button restores the original state as expected.

    Comment


      #3
      Attempting to set back the ViewState captured on initial page load, Grouping does not appear to clear?

      I achieved what I needed with a combination of
      Code:
      // ViewState
      ungroup();
      clearSort();
      setFields(makeArrayOfListGridFields()); // Resets initial field settings (order, width, etc.)
      deselectAllRecords();
      
      // Criteria & Data
      fetchData(defaultCriteriaAsDefinedElsehwere);
      I guess when I setFields() again, the original ListGridFields are orphaned in memory? Not sure how that works in JavaScript. Don't see it as a concern, as I don't imagine users doing this more than a couple times per session.

      With regard to saving Criteria, I was pleasantly surprised at how trivial you've made it.
      Code:
      // Save Criteria as JSON encoded String
      Record record = new Record();
      record.setAttribute(USER_PREFERENCES.EMPLOYEE_ID, employee.getEmployeeId());
      String userPrefFilterCriteria = filterEditorCriteria.asString(); // JSON-like encoding but w/ enhancements for date round-tripping
      record.setAttribute(USER_PREFERENCES.FILTER_CRITERIA, userPrefFilterCriteria);
      
      
      dsUserPrefs.updateData(record); // Or can save to cookie
      
      // Retrieve User Settings at Page Load
      initialDisplayCriteria = new Criteria(JSON.decode(aboveJsonStringRetrievedFromSqlElsewhere));
      Thanks!

      Comment

      Working...
      X