How to set CSS attribute (not StyleName) via Controller?

Hello altogether!

I found setStyleName() to programmatically set the name of a CSS class to a component.
Is there also a method to set the CSS attribute?

What’s behind: I want to set a margin for depending on some database value. E.g. margin-left:123px, where 123 comes from a certain db value.

Thanks in advance,
HP

Hello!

You can use HtmlAttributes bean:

@Autowired
private HtmlAttributes htmlAttributes;

@Subscribe
public void onInit(InitEvent event) {
    htmlAttributes.applyCss(testButton, "color:red;");
}

Also, in the screen descriptor you can use attribute css:

<button id="testButton" css="color:red;" />
1 Like

Hello all

	@Install(to = "artLotsTable.quantity", subject = "columnGenerator")
	private Component artLotsTableQuantityColumnGenerator(ArtLot artLot) {
		Component elem = new Table.PlainTextCell( artLot.getQuantity().toString() );
		htmlAttributes.applyCss( elem, "color:red;");
		return elem;
	}

how to make this work in a column of the browse screen ?

Best regards

Felix

Hello!

PlainTextCell is simplified component for showing text in columns cells and it does not work with HtmlAttributes.

You can use Label component instead or use styleProvider that returns selector for row or cell.
Also see example of style-provider: UI sampler::table-style-provider.

Hi Roman

Thank you for your tip. Following code is working.


	@Install(to = "artLotsTable.quantity", subject = "columnGenerator")
	private Component artLotsTableQuantityColumnGenerator(ArtLot artLot)
	{
		TextField elem = uiComponents.create(TextField.class);
		htmlAttributes.applyCss( elem, "text-align: right; width:50px;background-color:red;color:white;");
		elem.setValue(artLot.getQuantity().toString());
		return elem;
	}

I would prefer to use the styleProvider. BUT

Where does the application load the correct css ? When I create a custom theme, there are a lot of dependencies unsolved.

For example in the newstyle.css

@import "../helium/helium";

the system does not find the helium css’s and on the marketplace with jmix there are no css addons anymore.

How to proceed ?

Best regards

Felix