Announcement

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

    Data Source field backed by Data Source

    Hi,

    Is it possible to have a data source field which are backed by data source? I am looking for a case where a form has a field which allow lookup using ComboBoxItem (which uses a data source to fetch data from db)?

    #2
    Yes, if you just declare a foreignKey relationship, that's what will happen (except it will default to a SelectItem, which you can override with editorType="ComboBoxItem"). The explicit setting for this is SelectItem/ComboBox.setOptionDataSource().

    Comment


      #3
      Thanks. Although "foreignKey" setting didn't work for me. But i found below ds field attribute on a thread and they works fine.

      <field name="empId" type="integer" title= "Emp" editorType="ComboBoxItem" optionDataSource="employees" valueField="id" displayField="Name">
      <!-- <pickListCriteria> <status>Protected</status> </pickListCriteria>-->
      </field>

      But now is it possible to add more display fields here through ds.xml file (similar to setPickListFields())?

      And can this <pickListCriteria> could be velocity template to use other fields from same data source?

      Comment


        #4
        That's not correct usage (there is no DataSourceField.optionDataSource property) and will result in undesirable side-effects - it effectively sets ListGridField.optionDataSource (loads entire related DataSource - only OK for smaller datasets).

        The "foreignKey" declaration is the correct approach - just look for typos to see why it didn't work before.

        Instead of <pickListCriteria> (also incorrect usage), you can set optionOperationId to use specific operation from the related DataSource which can have special <criteria> (using Velocity templates), limited <outputs>, etc.

        Comment


          #5
          Hey! It works.

          With my previous sample on data binding (Obviouslly with your help) i thinki can deal with criteria.

          The only question is how to get pick list grid for combobox. Any ds field attribute called "pickListFields"? Just a wild guess. :)

          Comment


            #6
            @Isomorphic - Any pointer on how to add multiple option columns on a form field which is based with foreign key relation ship?

            Comment


              #7
              The standard way to do this is to create an instance of SelectItem or ComboBoxItem and call setPickListFields() on it, then associate that with the DataSourceField via a call to setEditorType().

              It is actually possible to drive this from the .ds.xml file as well, but we can't recommend doing so until you're willing to dive more deeply into the underlying SmartClient engine - you'll need to understand quite a few more things to do this properly than with the standard approach mentioned above.

              Comment


                #8
                We would prefer to drive that picklist from da.xml files. We have requirement where a wizard has to open up different forms based on user's earlier selections.

                If we would decide to do it in GWt java code then we might ended up building form class structure and that would increase with number of forms. But if we could do it with ds.xml file then it would be as simple as creating a form and setting up data source. I think it would be too much code to go other way around.

                Please let me know if you might have any reference doc and i would create an example before i would send out my finding on this evaluation.

                Comment


                  #9
                  Originally posted by Isomorphic
                  The standard way to do this is to create an instance of SelectItem or ComboBoxItem and call setPickListFields() on it, then associate that with the DataSourceField via a call to setEditorType().

                  It is actually possible to drive this from the .ds.xml file as well, but we can't recommend doing so until you're willing to dive more deeply into the underlying SmartClient engine - you'll need to understand quite a few more things to do this properly than with the standard approach mentioned above.
                  I think i understand what you mean by smartClient engine. Every field in .ds.xml file represent a field (which is a javascript object essentially) and all xml attribute are available as javascript object attributes and and we can set any property/function using this technique.

                  Btw, i got it working. Thanks a lot for your pointer.

                  One more thing - Is it possible to use one ds field's value to other field's validation logic (Using velocity template) ?

                  Comment


                    #10
                    Nice job - yes that's what was meant.

                    Two nuances to consider:

                    1. what you're doing is "cheating" by providing component-specific properties on a DataSourceField, which is really meant as a shared definition for all components. Every DataBoundComponent (ListGrid, DynamicForm, DetailViewer, Calendar, TileGrid, etc) that binds to this DataSource is going to receive those properties. In most cases they will be ignored by the component, but in some cases they could have unintended consequences (field.optionDataSource is the classic example - different meaning for a FormItem vs a ListGridField).

                    So this is fine as long as you know exactly what you are doing.

                    The cleaner way to do this is to extend the data binding system. For example, subclass DynamicForm, and when the default binding is complete, go through the DataSource definition / generated fields and augment them with additional settings based on organization-specific properties you add to DataSource fields. Then use your subclassed DynamicForm pervasively so the additional databinding features you've added are always available.

                    2. When you specify properties that aren't official DataSourceField properties you are using the schemaless transform mode of the Component XML system. So be aware of the need (documented under Component XML / Component Schema) to explicitly specify types in some situations (eg boolean fields).

                    Finally, on your validation/Velocity question - yes, related fields are available as the "record" Velocity variable. See this sample.

                    Comment


                      #11
                      Thanks. It clear up the picture little bit more in my head.

                      On a same topic - What's your recontamination to deal with situation where this pick list might have to use more fields (Apart from the foreignKey) as option criteria?

                      What i am considering is to seup an javascript function like below. Is it the right way to use other ds fields as criteria?
                      Although this function is not working right now as Smart GWT is treating returned value as string and expects a Map.

                      <field name="empId" type="integer" title= "Test Emp LookUp" width="250"
                      foreignKey="employees.id" editorType="ComboBoxItem" optionOperationId="fetchCurrentEmployees"
                      pickListWidth="650" valueField="empId" displayField="displayName" required="true"
                      optionCriteria="function () { var salaryVal = this.form.getValue('salary'); return [{salary: salaryVal}];}"
                      />
                      <field name="salary" type="integer" hidden="true"/>


                      I also noticed that hidden fields are not available in DynamicForm (although they are available in data source). Is there any property that can include these hidden fields into data bound form as well? Or am i missing something?

                      Comment


                        #12
                        Use an override of getPickListFilterCriteria instead of this approach. See the DataBound dependent select examples (for both grid and form) for intended usage.

                        "hidden" on a DataSource field means "never show to user". If you want some other behavior, consider detail="true" and also the ability to use or not use fields on a component-specific basis (see Data Binding chapter of the QuickStart Guide).

                        Comment


                          #13
                          I tried to override getPickListFilterCriteria() but the methof is not being invoked. I also tried with other methods but all overridden below, non of them is being invoked.

                          Just to provide context - This field is data source field but i am overriding it from visual field by calling setFields () on form.

                          Code:
                          final ComboBoxItem employeeComboBoxItem = new ComboBoxItem(dataSourceField.getJsObj()){
                          	@Override public Criteria getOptionCriteria() {
                          		GWT.log("Called getOptionCriteria()");
                          		return super.getOptionCriteria();
                          	}
                          	@Override protected Criteria getPickListFilterCriteria() {
                          		GWT.log("Called getPickListFilterCriteria()");
                          		return super.getPickListFilterCriteria();
                          	}
                          	
                          	@Override public void fetchData() {
                          		GWT.log("Called fetchData()");
                          		super.fetchData();
                          	}
                          	
                          	@Override public void fetchData(DSCallback callback) {
                          		GWT.log("Called fetchData(callback)");
                          		super.fetchData(callback);
                          	}
                          	
                          	@Override public void fetchData(DSCallback callback, DSRequest requestProperties) {
                          		GWT.log("Called fetchData(callback, requestProperties)");
                          		super.fetchData(callback, requestProperties);
                          	}
                          };

                          A small observation, i don't know if it's the intended behavior or a bug. Please advise.
                          Details- I have a field in data source file which is mapped with forigien key and this field come back as selectItem which is fine. But if you override it with a ComboBoxItem in your code then this field should render as combobox but no it stay as select till then you don't specify editorType="ComboBoxItem" attribute that means form.setFields() does not override data bound field instead just copy/merge the additional attributes/functions. Is it the intended behavior?
                          Last edited by learn_smartgwt; 15 Dec 2010, 07:11.

                          Comment


                            #14
                            Override getPickListFilterCriteria on the ComboBoxItem (where the method exists) not the DataSourceField (where it doesn't). Try not to let the shortcut of specifying certain properties on DataSourceFields confuse you - it has limited applicability and does not apply to Java methods at all.

                            Yes, the data binding system is designed around letting you overlay component-specific properties over generic DataSource schema to maximize reuse. See the Data Binding chapter of the QuickStart Guide for an overview.

                            Comment


                              #15
                              Thanks. All worked fine. You know what the problem was creating ComboBox using a JavaScript Object.
                              For example -
                              Creating the ComboBoxItem like below (Using constructor) does not override the data source field.
                              ComboBoxItem comboBoxItem = new ComboBoxItem(dataSourceField.getJsObj())

                              But if change the definition like below - all work out the way you pointed out and described in documentation.
                              ComboBoxItem comboBoxItem = new ComboBoxItem(dataSourceField.getName())

                              So the lesson is if you wana override the data bound field then create the visual field with same name instead of using constructor shortcut by passing a Javascript object.

                              I will play around with forms little bit more specially with validation and will let you if i might need help.

                              Thanks for your support.

                              Comment

                              Working...
                              X