Announcement

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

    Using foreign key and value for 1:many relationship.

    Hi, all.
    I will try to explain my problem with simple example using Course:Students relationship.
    I have Student json key/value ds that can be used to populate SelectItem. key is the studentID and value is studen name.

    Code:
    // StudentDS
    protected DataSourceIntegerField key;
    protected DataSourceField value;
    
    	public BaseKeyValueDataSource() {
    		key = new DataSourceIntegerField("key");
    		key.setHidden(true);
    		key.setPrimaryKey(true);
    		addField(key);
    		value = new DataSourceTextField("value");
    		addField(value);
    		setClientOnly(true);
    	}
    I have async callback that loads the json string and convert it to list of records like this:
    Code:
    public void onSuccess(List<String> jsondata) {
    			final Record[] records = new Record[jsondata.size()];
    			int i = 0;
    			for(final String json : jsondata) {
    				final JSONObject obj = (JSONObject) JSONParser.parse(json);
    				final Record r = new Record(obj.getJavaScriptObject());
    				records[i++] = r;
    			}
    			responseSuccess(records);
    		}
    So when i set my datasource to use this asynccallback to load the json strings and set the ds to some SelectItem it works properly (key is selectItem key and value is display field), but if i use this ds as foreign ds it uses key for both SelectItem key and display value.
    I created my SelecItem like this, but the problem remains.

    Code:
    // my CourseDS that is 1:many with StudentDS 
    	studentField = new DataSourceIntegerField("student", "Student");
    		studentField.setEditorType(new WebSelectItem(StudentDS.getInstance()));
    		studentField.setForeignKey("StudentDS.key");
    		addField(studentField);
    
    // WebSelectItem
    public class WebSelectItem extends SelectItem {
    
    	public WebSelectItem(DataSource ds) {
    		setValueField("key");
    		setDisplayField("value");
    		setEmptyPickListMessage("No data");
    		setPickListWidth(450);
    		
    		final ListGridField websidField = new ListGridField("key");
    		final ListGridField dscField = new ListGridField("value");
    		setPickListFields(websidField, dscField);
    		
    		setOptionDataSource(ds);
    		setAutoFetchData(true);
    	}
    }

    When i use CourseDS to populate some grid with all courses (one of the grid columns is student) i got the key(studentId) instead of value(student name).
    If i change studnetField.setForeignKey("StudentDS.key"); to
    studnetField.setForeignKey("StudentDS.value"); the data is displayed correctly, but i can't use value for ID.
Working...
X