Sorting generated columns

Hi there,

I’m having this same sorting issue. I must be missing something because I’m not sure where or how I have to define the MetaProperty to get this to work.

My generated cell is formatted correctly and displaying the correct data, but I’m not sure how to tell the system to sort it. So far all my attempts at defining the probably missing datatype have failed. I don’t get any errors, but the column I’ve generated does not respond to clicks.

public Component generateExpiryDateCell(CustomerOffer entity) {
    Label label = (Label) componentsFactory.createComponent(Label.NAME);
    label.setValue(getExpiryDate(entity));
	return label;
}

@Temporal(TemporalType.DATE)
@MetaProperty
public Date getExpiryDate(CustomerOffer entity) {
    return Date.from((LocalDateTime.ofInstant(entity.getCreateTs().toInstant(), ZoneId.systemDefault()).plusDays(entity.getOffer().getExpire_after())).atZone(ZoneId.systemDefault()).toInstant());
}

Hi,

I would be very interested in an answer to @iobercea 's question above.

Kind Regards

Hi,

In this particular case, you don’t need to use generated columns, a simple @MetaProperty that calculates dates will be enough. Also, a meta property can’t be used as a util method as in the example above, i.e. it can’t have input parameters.

Let’s assume that I have an entity CustomerOffer:

@NamePattern("%s|name")
@Table(name = "DEMO_CUSTOMER_OFFER")
@Entity(name = "demo$CustomerOffer")
public class CustomerOffer extends StandardEntity {
    private static final long serialVersionUID = -6101720560346863464L;

    @NotNull
    @Column(name = "NAME", nullable = false)
    protected String name;

    @NotNull
    @Lob
    @Column(name = "DESCRIPTION", nullable = false)
    protected String description;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    @Temporal(TemporalType.DATE)
    @MetaProperty(related = "createTs")
    public Date getExpiryDate() {
        return DateUtils.addDays(getCreateTs(), 10);
    }
}

Pay attention to the getExpiryDate which is a read-only meta property implemented with a method:

@Temporal(TemporalType.DATE)
@MetaProperty(related = "createTs")
public Date getExpiryDate() {
    return DateUtils.addDays(getCreateTs(), 10);
}

Now, I can add this property to a view:

image

And use it in screens, for instance in the browser screen:

<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        caption="msg://browseCaption"
        class="com.company.demo.web.customeroffer.CustomerOfferBrowse"
        focusComponent="customerOffersTable"
        lookupComponent="customerOffersTable"
        messagesPack="com.company.demo.web.customeroffer">
    <dsContext>
        <groupDatasource id="customerOffersDs"
                         class="com.company.demo.entity.CustomerOffer"
                         view="customerOffer-with-expirityDate">
            <query>
                <![CDATA[select e from demo$CustomerOffer e]]>
            </query>
        </groupDatasource>
    </dsContext>
    <dialogMode height="600"
                width="800"/>
    <layout expand="customerOffersTable"
            spacing="true">
        <filter id="filter"
                applyTo="customerOffersTable"
                datasource="customerOffersDs">
            <properties include=".*"/>
        </filter>
    <groupTable id="customerOffersTable"
                multiselect="true"
                width="100%">
        <actions>
            <action id="create"/>
            <action id="edit"/>
            <action id="remove"/>
        </actions>
        <columns>
            <column id="name"/>
            <column id="description"/>
            <column id="expiryDate"/>
        </columns>
            <rows datasource="customerOffersDs"/>
            <rowsCount/>
            <buttonsPanel id="buttonsPanel"
                          alwaysVisible="true">
                <button id="createBtn"
                        action="customerOffersTable.create"/>
                <button id="editBtn"
                        action="customerOffersTable.edit"/>
                <button id="removeBtn"
                        action="customerOffersTable.remove"/>
            </buttonsPanel>
        </groupTable>
    </layout>
</window>

Pay attention to the datasource view and a column definition:

...
<column id="expiryDate"/>
...

Since the expiryDate property has one of the default datatypes (in this case the DateDatatype) we have the sorting functionality out of the box.

image

Regards,
Gleb

1 Like