最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Knockout options conditional css - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Apr 17, 2013 at 12:27 SkeeveSkeeve 1,2553 gold badges16 silver badges31 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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.puted(function() {
        return this.personalized() == "Y" ? "green" : "red";
    }, question);

</script>
<style>
    .green {color:green;}
    .red{color:red;}
</style>

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>

There is also option to set data-bind="style:{color: value ? 'green' : null}"

It's not the best option (the best is to set new class) but it's possible

发布评论

评论列表(0)

  1. 暂无评论