SmartGWT TreeGrid disable lazy loading

336 views Asked by At

I had a consultant from Isomorphic begin development of a webapp, and deliver a foundation. The treegrid that is essential to the navigation of the app has the nodes loaded on demand. I need to figure out how to change this to load all children from the start.

Here is the ds.xml code:

<DataSource serverType="sql" dbName="CSODatabaseCities"
    ID="Sensor"
    schema="dynamic"
    tableName="sensor_data">

    <fields>

        <field name="nodeId" type="int" />
        <field name="nodeName" type="text" />
        <field name="number" type="int" />
        <field name="title" type="text" />
        <field name="multiplier" type="float" />
        <field name="offset" type="float" />
        <field name="latitude" type="float"/>
        <field name="longitude" type="float"/>
        <field name="controlUrl" type="text" />

        <field name="structureType" type="text" />
        <field name="sensorType" type="text" >
            <valueMap>
                <value ID="d">Depth</value>
                <value ID="q">Flow</value>
                <value ID="rg">Rain Gauge</value>
                <value ID="t">Temperature</value>
                <value ID="v">Velocity</value>
            </valueMap>
        </field>

        <field name="criticalLow" type="float" />
        <field name="criticalHigh" type="float" />

        <field name="units" type="text" />
        <field name="latestValue" type="float" title="Reading" format="#.###"/>
        <field name="lastCollected" type="datetime" />
        <field name="percentUtilization" type="float" format="##.##'%'" title="Utilization" />
        <field name="percentUtilizationImageUrl" type="image">
            <customSelectExpression>
            CASE 
              WHEN percentUtilization &lt; 0 
                THEN CONCAT('structure/', structureType, '_0.bmp')
              WHEN percentUtilization &gt; 100 
                THEN CONCAT('structure/', structureType, '_100.bmp')
              WHEN structureType = 'raingauge' AND percentUtilization BETWEEN 0 AND 10 
                THEN CONCAT('structure/', structureType, '_10.bmp')      
              ELSE 
                CONCAT('structure/', structureType, '_', ROUND(percentUtilization, -1),'.bmp')      
            END
            </customSelectExpression>
        </field>

        <field name="parentId" type="text" title="Group" />
        <field name="sensorId" type="text" customSelectExpression="CONCAT('c',nodeId, '_', number)" />
        <field name="isFolder" hidden="true" canFilter="false" customSelectExpression="false" />

    </fields>

    <operationBinding operationType="fetch" operationId="fetchByParentGroup">
       <script language="groovy"><![CDATA[ 
           if (criteria.get('parentId', '/') == '/') {
               dsRequest.setOperationId('fetchSensorGroups');
           } else {
               dsRequest.setOperationId(null);
           }
           return dsRequest.execute();
       ]]></script>

    </operationBinding>

    <operationBinding operationType="fetch" operationId="fetchSensorGroups" >
       <selectClause>'/' AS parentId, TRIM(descr) AS sensorId, TRIM(descr) AS title, true AS isFolder, COUNT(*) AS sensorCount</selectClause>
       <tableClause>${rawValue.schema}.inodes</tableClause>
       <whereClause>
        sensType1 != 'No Sensor'
          OR sensType2 != 'No Sensor'
          OR sensType3 != 'No Sensor'
          OR sensType4 != 'No Sensor'
       </whereClause>
       <groupClause>descr</groupClause>
    </operationBinding>

    <operationBinding operationType="fetch" qualifyColumnNames="false">
        <tableClause>
        (
          SELECT TRIM(i.descr) AS parentId, i.id AS nodeId, i.name AS nodeName, 
            1 AS number, i.sensType1 AS title, i.a1 AS multiplier, i.b1 AS offset, 
            i.lat AS latitude, i.lon AS longitude, i.controlURL as controlUrl,
            s.structure_type_s1 AS structureType, s.sensor_type_s1 AS sensorType, 
            s.critical_low_s1 AS criticalLow, s.critical_high_s1 AS criticalHigh, 
            s.sensor1_units AS units, lv.sens1 AS latestValue, lv.time AS lastCollected,
            (lv.sens1 / s.critical_high_s1) * 100 AS percentUtilization
          FROM #schema.inodes i
            INNER JOIN #schema.inodes_structure_data s
              ON i.id = s.id
            LEFT JOIN #schema.inodes_latest_values lv
              ON i.id = lv.node_id
           WHERE i.sensType1 != 'No Sensor'

          UNION

          SELECT TRIM(i.descr) AS nodeGroup, i.id AS nodeId, i.name AS nodeName, 
            2 AS sensorNumber, i.sensType2 AS title, i.a2 AS multiplier, i.b2 AS offset, 
            i.lat AS latitude, i.lon AS longitude, i.controlURL as controlUrl,
            s.structure_type_s2 AS structureType, s.sensor_type_s2 AS sensorType, 
            s.critical_low_s2 AS criticalLow, s.critical_high_s2 AS criticalHigh, 
            s.sensor2_units AS units, lv.sens2 AS latestValue, lv.time AS lastCollected,
            (lv.sens2 / s.critical_high_s2) * 100
          FROM #schema.inodes i
            INNER JOIN #schema.inodes_structure_data s
              ON i.id = s.id
            LEFT JOIN #schema.inodes_latest_values lv
              ON i.id = lv.node_id
           WHERE i.sensType2 != 'No Sensor'

          UNION

          SELECT TRIM(i.descr) AS nodeGroup, i.id AS nodeId, i.name AS nodeName, 
            3 AS sensorNumber, i.sensType3 AS title, i.a3 AS multiplier, i.b3 AS offset,
            i.lat AS latitude, i.lon AS longitude, i.controlURL as controlUrl, 
            s.structure_type_s3 AS structureType, s.sensor_type_s3 AS sensorType, 
            s.critical_low_s3 AS criticalLow, s.critical_high_s3 AS criticalHigh, 
            s.sensor3_units AS units, lv.sens3 AS latestValue, lv.time AS lastCollected,
            (lv.sens3 / s.critical_high_s3) * 100
          FROM #schema.inodes i
            INNER JOIN #schema.inodes_structure_data s
              ON i.id = s.id
            LEFT JOIN #schema.inodes_latest_values lv
              ON i.id = lv.node_id
           WHERE i.sensType3 != 'No Sensor'

          UNION

          SELECT TRIM(i.descr) AS nodeGroup, i.id AS nodeId, i.name AS nodeName, 
            4 AS sensorNumber, i.sensType4 AS title, i.a4 AS multiplier, i.b4 AS offset, 
            i.lat AS latitude, i.lon AS longitude, i.controlURL as controlUrl,
            s.structure_type_s4 AS structureType, s.sensor_type_s4 AS sensorType, 
            s.critical_low_s4 AS criticalLow, s.critical_high_s4 AS criticalHigh, 
            s.sensor4_units AS units, lv.sens4 AS latestValue, lv.time AS lastCollected,
            (lv.sens4 / s.critical_high_s4) * 100
          FROM #schema.inodes i
            INNER JOIN #schema.inodes_structure_data s
              ON i.id = s.id
            LEFT JOIN #schema.inodes_latest_values lv
              ON i.id = lv.node_id
           WHERE i.sensType4 != 'No Sensor'

        ) sensor_data
        </tableClause>

    </operationBinding>

    <operationBinding operationType="add" requires="false" />
    <operationBinding operationType="remove" requires="false" />
    <operationBinding operationType="update" requires="false" />

</DataSource>

Here is the code relevant to setting up the treegrid:

Tree tree = new Tree();
        tree.setModelType(TreeModelType.PARENT);
        tree.setRootValue("/");
        tree.setIdField("sensorId");  

        treeGrid.setFetchOperation("fetchByParentGroup");
        //treeGrid.setLoadDataOnDemand(false);
        treeGrid.setDataProperties(tree);
        treeGrid.setSort(new SortSpecifier("title", SortDirection.ASCENDING));

        treeGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
        treeGrid.setShowPartialSelection(true);
        treeGrid.setCascadeSelection(true);

        treeGrid.setNodeIcon("[SKINIMG]SchemaViewer/simpleType.png");
        treeGrid.setFolderIcon("[SKINIMG]SchemaViewer/complexType.gif");

        treeGrid.setShowOpenIcons(false);
        treeGrid.setShowDropIcons(false);
        treeGrid.setClosedIconSuffix("");

        /*
         * Load selected children on demand and update listPane with currently
         * selected items
         */
        treeGrid.addSelectionUpdatedHandler(new SelectionUpdatedHandler() {
            @Override
            public void onSelectionUpdated(SelectionUpdatedEvent event) {

                final TreeNode updated = treeGrid.getRecord(treeGrid.getEventRow());
                final TreeNode[] children = treeGrid.getTree().getChildren(updated);

                if (updated.getAttributeAsBoolean("isFolder") && children.length == 0) {
                    treeGrid.getData().loadChildren(updated, new DSCallback() {
                        @Override
                        public void execute(DSResponse dsResponse, Object data,
                            DSRequest dsRequest) {
                            treeGrid.selectRecords(dsResponse.getData());
                        }
                    });
                } else {
                    RecordList selected = new RecordList(treeGrid.getSelectedRecords());
                    listGrid.setData(selected.findAll(new AdvancedCriteria("isFolder",
                        OperatorId.NOT_EQUAL, true)));

                }

                listGrid.selectRecord(0);
            }
        });

I have been trying for hours to figure this out, but I cannot for the life of me figure out how to keep the same appearance (grouping) and load all sensors from the start.

Here is an image of the current appearance, and the sensors arent fetched until the parent is expanded.

enter image description here

Any light shed on the subject would be greatly appreciated.

1

There are 1 answers

1
gpapaz On BEST ANSWER

The setLoadDataOnDemand method is a great way to load all children from the start:

treeGrid.setLoadDataOnDemand(Boolean.FALSE);