Announcement

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

    Another nested datasources question

    Hi All,

    I am trying to implement an XML based Datasource and can't get it right.

    Here is my requirement.

    The XML I have looks like that

    <Root>
    <Attributes>
    <Attribute ID="1' name="attr1" value="">
    <AllowedValues>
    <AllowedValue value="val1"/>
    <AllowedValue value="val2"/>
    </AllowedValues>
    </Attribute>
    <Attribute ID="2' name="attr2" value="">
    <AllowedValues>
    <AllowedValue value="val3"/>
    <AllowedValue value="val4"/>
    </AllowedValues>
    </Attribute>
    </Attributes>
    </Root>

    What I need to display is a LisrGrid with 2 columns - AttributeName and AttributeValue. Each AttributeValue cell has a selectItem editor that contains the list of allowed values for this attribute. So in this example allowed values for attr1 would be val1 and val2 and for attr2 - val3 and val4.

    Whatever I try doesn't produce result I want. At best I have only first allowed value for each attribute - val1 and val3 correspondingly.
    What is the right way to do it right?

    Thanks in advance?

    #2
    It depends: allowed values belong to a list defined somewhere (that could be addressed to with another DataSource) or they are sort list of values specific for each property not worth putting out of the code?

    In the second case I'd suggest to attach a cellClickHandler that evaluates the value map on the basis of the selected attribute.

    In the first case I'd save attribute values in a DataSource and I would build a filter based on the attribute name.

    Yary

    Comment


      #3
      Originally posted by yary
      It depends: allowed values belong to a list defined somewhere (that could be addressed to with another DataSource) or they are sort list of values specific for each property not worth putting out of the code?

      In the second case I'd suggest to attach a cellClickHandler that evaluates the value map on the basis of the selected attribute.

      In the first case I'd save attribute values in a DataSource and I would build a filter based on the attribute name.

      Yary
      Yary,

      Thanks for the response. It is definitely the first case. The list of allowed values is dynamic and potentially quite long.
      Can you elaborate how would you define the DataSource for this case. It somewhat resembles the example from your other post where you suggest using 2 DataSources, but not quite. The datasource(s) should reflect hierarchical structure and bring many AllowedValues for each Attribute. And this part doesn't work for me. Post http://forums.smartclient.com/showthread.php?t=9659 suggests that there is some problem in SmartGWT with valueXPath not bringing multiple values. It is a year old post - has it been fixed since? If not - what is the workaround? I don't think I can use the solution from this post that manipulates JXPath directly. All this should happen on the client. I would really appreciate if somebody could post a code fragment that reads hierarchical data and bring multiple values for each attribute? Thank a lot

      Comment


        #4
        Hi eugenen, this is what I would do if I had to start from the beginning:

        1) I would define server-side 3 tables: Attributes( attributeID, description) Values( valueID, description) and Attribute_Values( attributeID, valueID) that links attributes and values in a relationship (1,*).

        2) I would define client-side 2 DataSources: attributeDS and valueDS, ValueDS should have a multiple attribute that points to attributeDS, here the code to do that:

        Code:
                DataSourceField attributes = new DataSourceField( "attributes", FieldType.TEXT, "Attributes this value is available for");
                attributes.setValueXPath( "attributes/attributeID" );
                attributes.setMultiple( true );
                attributes.setTypeAsDataSource( attributeDS );
                attributes.setForeignKey( attributeDS.getID() + ".attributeID" );
        The server will send attributes as:

        Code:
        <attribute>
          <attributeID>1</attributeID>
          <description>An attribute</description>
        <attribute>
        and values as:

        Code:
        <value>
          <valueID>1</valueID>
          <description>A value</description>
          <attributes>
            <attributeID>1</attributeID>
            <attributeID>2</attributeID>
            ...
          </attributes>
        <value>
        If you set correctly the foreign key, ..., you won't expect any issue about multiple attributes.

        3) In the datagrid you set attributeDS as datasource and you will have the complete list of attributes, or you filter them conveniently.

        4) In the datagrid you define a field for selecting the value.
        This code could help:
        Code:
                attributeValue.setOptionDataSource( valueDS );
                attributeValue.setValueField( "valueID" );
                attributeValue.setDisplayField( "description" );
                attributeValue.setEmptyCellValue( "Choose a value from the list" );
        5) You add to attributeValue field a selector with defined filter method set.

        Code:
                SelectItem selector = new SelectItem();
                selector.setValueField( "valueID" );
                selector.setDisplayField( "description" );
                setFilterLocally( true );
                setPickListFilterCriteriaFunction(
                    new FilterCriteriaFunction() {
                    @Override
                        public Criteria getCriteria() {
                            Record record = getSelectedRecord();//record selected in grid
                            //if record is null return new Criteria( "valueID", "-1" );
                            String attributeID = record.getAttribute("attributeID")
                            AdvancedCriteria attribute = new AdvancedCriteria(
                                "attibutes",
                                OperatorId.REGEXP,
                                "^([0-9]+,)*" + attributeID + "(,[0-9]+)*$"
                            );
                            return attribute;
                        }
                    }
                );
        
                attributeValue.setEditorType( selector );
        Note that the selector will use the DataSource of the underlying GridField and does not need to set one.
        Note that with the regexp I defined in any case I overcome the problem of multiple values, but I had to define it that way because if I put a condition in filter like new Criteria( "attributeID", "1"), it matches not only the wanted attributes, but also 1.* (like 100, 11, ...), which is not expected and not wanted!

        Yary

        Comment


          #5
          Thank you very much Yary. It works.
          Last edited by eugenen; 14 Feb 2011, 00:22.

          Comment

          Working...
          X