Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I conditionally add or remove CSS classes in Aurelia repeat.for?

Tags:

aurelia

I have an array of stuff from which I am building a <select> and I want to be able to mark the first item as disabled using CSS, however I cannot get the syntax right.

Here's what I have:

<select select2 value.bind="selectedItem">
    <option repeat.for="item of stuff" model.bind="item" class="${${first} ? 'disabled selected hidden' : ''">
        ${item.key}
    </option>
</select>

HERE's a Plunker similar enough that can be used as a test-bed.

What am I missing?

like image 200
MaYaN Avatar asked Dec 07 '15 11:12

MaYaN


1 Answers

Your example is not full, but I guess it should look like:

class="${item.first ? 'disabled selected hidden' : ''}"

Also if you have first property at VM, like stuff you write:

class="${$parent.first == item? 'disabled selected hidden' : ''}"

UPDATE:

Plunker (http://plnkr.co/edit/2xywp0)

<option repeat.for="thing of things" model.bind="thing">${thing.name} ${$first | stringify}</option>

You just have wrong syntax here: class="${${first} ? 'disabled selected hidden' : ''" should be class="${ $first ? 'disabled selected hidden' : '' }"

From Aurelia docs:

Contextual items availabe inside a repeat template:

$index - The index of the item in the array.
$first - True if the item is the first item in the array.
$last - True if the item is the last item in the array.
$even - True if the item has an even numbered index.
$odd - True if the item has an odd numbered index.
like image 108
valichek Avatar answered Oct 18 '22 02:10

valichek