|
#1
|
|||
|
|||
|
I'm trying to find a simple way to construct a DynamicForm and include only the fields from the data source where canEdit="true". The reason for this, in part, is that when un-editable fields are included in the form they are also included in the update when the form is saved. I can step through the DataSourceFields and pick out the ones where canEdit="true" but then I have to create a new FormItem for each one, add it to an array and then setFields for the DynamicForm. Is there a simpler way. Maybe just a helper method to create a FormItem from a DataSourceField. There must be such a method somewhere since that is essential what a DynamicForm does. It just does it for all of the data source fields and I only want to do it for a subset.
|
|
#2
|
|||
|
|||
|
If you are specifying FormItems on a DynamicForm that has a DataSource, you can specify the name of the DataSource field only and the FormItem "inherits" all settings from the DataSource field. So this is basically minimal already - a one-liner per FormItem you want to use.
This pattern applies across all DataBoundComponents. See also useAllDataSourceFields. |
|
#3
|
|||
|
|||
|
Is there a known problem with DataSource.getFields()? It is returning an array with one element with getName()=null. The DynamicForm shows all dozen or so fields, but the following code inserted just after the ds is assigned to the form ...
itemForm.setDataSource(ipItemMasterDS); DataSourceField[] dsFields = ipItemMasterDS.getFields(); System.out.println("Number of fields = " + dsFields.length); Shows "Number of fields = 1" ?? Last edited by jay.l.fisher; 26th Nov 2009 at 12:17.. |
|
#4
|
|||
|
|||
|
Yes (already fixed). Use getFieldNames() and getField() instead if you need to iterate over them.
|
|
#5
|
|||
|
|||
|
That helps, but now DataSourceField.getCanEdit() is returning false for every field even though many of them are certainly editable.
|
|
#6
|
|||
|
|||
|
You're probably seeing that for fields where the value is unset, meaning that editability will be dynamically determined in the context of the actual databound component. The API needs to be changed to reflect this as null rather than false, but you can either set canEdit to true on editable fields or call a getAttribute() variant to get the underlying value.
|
|
#7
|
|||
|
|||
|
I still can't seem to make this work. Here is the code (quite a bit more than the one line you suggested was needed). Can you spot the error? And is there a simpler way to do this? I just want a submit button and the fields from the data source where canEdit="true". The editable fields show up on the form now, but they are not editable. Just the label and the field value as text.
Code:
// Define the item edit form
ArrayList<FormItem> formFields = new ArrayList<FormItem>();
String[] dsFields = ipItemMasterDS.getFieldNames();
for (int i=0; i<dsFields.length;i++) {
DataSourceField dsField = ipItemMasterDS.getField(dsFields[i]);
if (dsField.getCanEdit()) {
FormItem f = new FormItem();
f.setName(dsField.getName());
formFields.add(f);
}
}
FormItem[] editFieldsArray = new FormItem[formFields.size()+1];
SubmitItem submit = new SubmitItem();
submit.setTitle("Save Changes");
editFieldsArray[0]=submit;
for (int i=1; i<formFields.size()+1;i++) {
editFieldsArray[i]=formFields.get(i-1);
}
itemForm.setDataSource(ipItemMasterDS);
itemForm.setFields(editFieldsArray);
|
|
#8
|
|||
|
|||
|
One line per field specified on the form if you are specifying them by hand.
It's not clear what's wrong now, it likely has something to do with your DataSource, which you did not show. If you were to, for example, specify editorTypes as StaticTextItem, you would get this effect. |
|
#9
|
|||
|
|||
|
There are two simple text fields and one with a valueMap, but none have an editorType specified. Here are the relevant fields from the data source. Those same fields also appear in a ListGrid and FilterBuilder and the look and work correctly there.
Code:
<field name="IDES" type="string" length="25" title="Description" canFilter="true" canEdit="true"/>
<field name="IVST" type="string" length="15" title="Vendor Style" canEdit="true"/>
<field name="IATT01" type="integer" title="Brand" detail="true"
align="left" canEdit="true">
<valueMap>
<value ID="1">Brand One</value>
<value ID="2">Brand Two</value>
</valueMap>
</field>
|
|
#10
|
|||
|
|||
|
A little more info ... For the text fields, if I add an explicit editorType="TextItem" to the datasource definition the DynamicForm does show them as editable TextItems. Same for the valueMap fields. If I explicitly set editorType="SelectItem" they show up as expected. From what I understand that should not be necessary.
Last edited by jay.l.fisher; 27th Nov 2009 at 10:50.. |