display single entity property in text field

Hello, newbie question…

I’m trying to display a single property of a single entity in a text field - in other words the query is static and will always produce one result. I can make it work with a table, but as soon as I try to do it with a single text field I’m stuck. I’ve also tried it with a label with no joy. Can anyone help?


 <dsContext>
        <collectionDatasource id="team1Ds"
                              allowCommit="false"
                              class="com.company.ntsproto.entity.Team"
                              maxResults="1"
                              view="_local">
            <query>
                <![CDATA[select e from ntsproto$Team e where e.teamNum=1]]>
            </query>
        </collectionDatasource>
    </dsContext>
    <layout>
        <textField caption="doctor name"
                   datasource="team1Ds"
                   datatype="string"
                   description="doctor name"
                   editable="false"
                   property="doctorName"
                   required="true"/>
    </layout>

Hi Lee,
Your code should work if your “ntsproto$Team” entity really has the “doctorName” attribute. Please also remove the “datatype” attribute, it is not needed as the type is defined by the entity attribute specified in “property”.

Thanks for your help.

It seemed like it should work, but it doesn’t. A table using the same data source works fine. I’ve attached the XML with a table added in and a screenshot of the result. Any ideas?

TIA

    <dsContext>
        <collectionDatasource id="team1Ds"
                              allowCommit="false"
                              class="com.company.ntsproto.entity.Team"
                              maxResults="1"
                              view="_local">
            <query>
                <![CDATA[select e from ntsproto$Team e where e.teamNum=1]]>
            </query>
        </collectionDatasource>
    </dsContext>
    <layout>
        <textField caption="doctor name"
                   datasource="team1Ds"
                   description="doctor name"
                   editable="false"
                   property="doctorName"
                   required="true"/>
        <table height="100px"
               width="200px">
            <columns>
                <column id="doctorName"/>
                <column id="nurseName"/>
                <column id="paraEmtName"/>
                <column id="teamNum"/>
            </columns>
            <rows datasource="team1Ds"/>
        </table>
    </layout>

Screen Shot 2016-09-10 at 22.20.56

OK I’m starting to answer my own question. If I click on a row of the table, the text field then displays the correct data. So it seems like there’s a need to select the correct row within the datasource in order for the text field to display it…

Exactly, the CollectionDatasource’s getItem() method initially returns null. You can invoke ds.setItem(ds.getItems().iterator().next()) programmatically to select the first instance.

Thanks, you got me there. I’ll leave the code below for anybody else who comes across this thread. I’m a total newbie to this platform and it’s been 10 years since I wrote serious java so I’m very rusty.


package com.company.ntsproto.web.screens;

import com.haulmont.cuba.gui.components.AbstractWindow;

import com.company.ntsproto.entity.Team;
import com.haulmont.cuba.gui.data.CollectionDatasource;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.UUID;
import com.haulmont.cuba.gui.components.Component;
import java.util.Map;


public class Screen1 extends AbstractWindow {
  
  @Inject
    protected CollectionDatasource<Team, UUID> team1Ds;
  
  @Override
    public void ready() {
        super.ready();
        team1Ds.setItem(team1Ds.getItems().iterator().next());
    }
    
}