Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout options conditional css

I have an observable array of objects

question = {
        ownerUserName: item.id,
        text: item.text,
        dataType: item.dataType,
        personalized: item.personalized,
        status: item.status,
        actionUserName: item.actionUserName
    }

And a select with options from this array:

<select id="questId" style="width: 425px" data-bind="options: questionList, optionsText: 'text'">

How with the help of knockout can I make so that if question.personalized == "Y" color of the text of this question would be green?

like image 992
Skeeve Avatar asked Apr 17 '13 12:04

Skeeve


2 Answers

You can use foreach instead of usual options binding. Something like

<style>
   .highlighted{
      background-color: red;
   }
</style
<select id="questId" style="width: 425px" data-bind="foreach: questionList">
   <option data-bind="text: text, class: {highlighted: personalized == 'Y'}">
</select>
like image 43
demkalkov Avatar answered Oct 11 '22 15:10

demkalkov


Your best bet is the css binding

A quick adaptation of the documentation to your need would be

<div data-bind="text: personalized, css: personalizedStatus">
   Profit Information
</div>

<script type="text/javascript">
    question.personalizedStatus = ko.computed(function() {
        return this.personalized() == "Y" ? "green" : "red";
    }, question);

</script>
<style>
    .green {color:green;}
    .red{color:red;}
</style>
like image 74
Alexandros B Avatar answered Oct 11 '22 15:10

Alexandros B