How to set default value to Drop Down?

Hi guys,

I have table called DetailPurchased, one of the its column named MASTER_STATUS_ID which has Association relationship with table MasterStatus. MasterStatus contains data : “Sold” and “Available”. DetailPurchased UI have MasterStatus drop down as one of its UI component. Want to ask how to set default value to that Drop Down with “Available” when the UI open?

Thanks in advance.

Kind regards,
MD

1 Like

Hi Murti,

You can set initial values for entity attributes in a number of ways, see examples here. In your case perhaps the best way is to set masterStatus attribute of the DetailPurchased entity in the initNewItem() method of the DetailPurchased editor screen. The value will appear in the linked visual component.

Well, I know I am doing something wrong. I just can’t place my finger on it.

I have an edit screen with a dropdown list that is an association to entity called “AccountingType”

The users want the default value of the dropdown list to be “Invoice”. However I can’t seem to get the logic quite right.

I would like it to be set to “Invoice” or if invoice doesn’t exist in the table, to set it to null.


package com.company.salestracking.web.saleslineitem;

import com.haulmont.cuba.gui.components.AbstractEditor;
import com.company.salestracking.entity.SalesLineItem;

public class SalesLineItemEdit extends AbstractEditor<SalesLineItem> {

    @Override
    protected void initNewItem(SalesLineItem item) {

        item.setAccountingType("Invoice");
}

Hi Thomas,

If you need to work with screen datasources, you should move your logic to the ready() method, because in initNewItem() they are not initialized yet. So it should be something like this:

public class SalesLineItemEdit extends AbstractEditor {

@Override
protected void ready() {
    SalesLineItem item = getItem();
    if (PersistenceHelper.isNew()) {
        AccountType type = accountTypeDs.getItems().stream()
            .filter(type -> "Invoice".equals(type.getName()))
            .findAny()
            .orElse(null);
        item.setAccountingType(type);
    }
}

}

1 Like

Thanks, I can see I was going down a wrong path.

I am getting the error below when I try to launch from studio however.
I feel like I should be passing something into PersistenceHelper.isNew() but am unsure what that would be.


error: method isNew in class PersistenceHelper cannot be applied to given types;
        if (PersistenceHelper.isNew()) {
                             ^
  required: Object
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error
 FAILED

package com.company.salestracking.web.saleslineitem;


import com.haulmont.cuba.gui.components.AbstractEditor;
import com.company.salestracking.entity.SalesLineItem;
//added code begins
import com.haulmont.cuba.core.entity.IdProxy;
import com.company.salestracking.entity.AccountingType;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.core.global.PersistenceHelper;
import javax.inject.Inject;
//added code ends

public class SalesLineItemEdit extends AbstractEditor<SalesLineItem> {

    @Inject
    protected CollectionDatasource<AccountingType, IdProxy> accountingTypeDs;

    @Override
    public void ready() {
        SalesLineItem item = getItem();
        if (PersistenceHelper.isNew()) {
            AccountingType type = accountingTypeDs.getItems().stream()
                    .filter(accountingTypeName -> "Invoice".equals(accountingTypeName.getAccountingTypeName()))
                    .findAny()
                    .orElse(null);
            item.setAccountingType(type);
        }
    }
}

Hi,

PersistenceHelper.isNew requires an argument: cuba/PersistenceHelper.java at master · cuba-platform/cuba · GitHub

It should probably be PersistenceHelper.isNew(item)

Bye
Mario

1 Like

Hi,

PersistenceHelper.isNew() requires an entity instance as parameter. I think the correct code should be: PersistenceHelper.isNew(item)

1 Like

Sorry, I answered the same question. Page refresh issue. :slight_smile:

Thanks everyone. That did the trick .
The help on this forum is an enormous resource.


package com.company.salestracking.web.saleslineitem;


import com.haulmont.cuba.gui.components.AbstractEditor;
import com.company.salestracking.entity.SalesLineItem;
//added code begins
import com.haulmont.cuba.core.entity.IdProxy;
import com.company.salestracking.entity.AccountingType;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.core.global.PersistenceHelper;
import javax.inject.Inject;
//added code ends

public class SalesLineItemEdit extends AbstractEditor<SalesLineItem> {

    @Inject
    private CollectionDatasource<AccountingType, IdProxy> accountingTypesDs;

    @Override
    public void ready() {
        SalesLineItem item = getItem();
        if (PersistenceHelper.isNew(item)) {
            AccountingType type = accountingTypesDs.getItems().stream()
                    .filter(accountingTypeName -> "Invoice".equals(accountingTypeName.getAccountingTypeName()))
                    .findAny()
                    .orElse(null);
            item.setAccountingType(type);
        }
    }
}![alt](@2017-04-26%2009_11_34-Sales%20Tracking.png)

2017-04-26 09_11_34-Sales Tracking