Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout CSS styling, through a function as well as a binding

I know that you can use knockout to set the class as per a function return like so;

<span data-bind="css: styling()"></span>

You can also set them inline with a true/false value.

<span data-bind="css: {className: $parent.active() == $data}"></span>

Is there a way to do both in the one binding? My example is that I want to return a custom class depending on the type of the item displayed by the span element (there will be many of these) but I also want them to be highlighted if they are selected.

It seems that you cannot use functions on the right hand side of the object, and having two cause the second to overwrite the first.

Any ideas?

like image 270
Shadow Avatar asked Aug 20 '13 08:08

Shadow


People also ask

What is binding in Knockout?

A binding context is an object that holds data that you can reference from your bindings. While applying bindings, Knockout automatically creates and manages a hierarchy of binding contexts. The root level of the hierarchy refers to the viewModel parameter you supplied to ko. applyBindings(viewModel) .

What is two way binding in KnockoutJS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.

What are the types of binding supported by KnockoutJS?

Binding Values The binding value can be a single value, literal, a variable or can be a JavaScript expression.

Which function is used to activate KnockoutJS?

To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.


1 Answers

With this sample you will be able to specify the classes of each item.

View

<div data-bind="foreach: items" >
    <span data-bind="css : $parent.styling($data), text: $data"></span>
</div>

JS :

var vm = {
    items : ['a','b','c' ],

    styling : function(item ) {
        if(item =='a') return 'active';
        if(item =='b') return 'active bold';
        return '';
    }    
};

ko.applyBindings(vm);

See fiddle


Edit:

@Shadow : You right, if you pass a value to a computed it is to set (or write) it.

I think this would match your needs. When on an item the toggle function is called. Then the active observable will be changed and finally the styling computed.

JS

var Item =  function(data) {
    var self = this;
    self.name = data;
    self.active = ko.observable(false);
    self.styling = ko.computed(function(item ) {
        if(self.active()) return 'active';
        return '';  
    });
    self.toggle =  function(){
        self.active(!self.active());
    }
};

var vm = {
    items : [new Item('a'),new Item('b'), new Item('c') ]        
};


ko.applyBindings(vm);

View

<div data-bind="foreach: items" >
    <span data-bind="css : styling, text: name, click : toggle"></span>
    <br/>
</div>

See fiddle

like image 143
Damien Avatar answered Oct 05 '22 22:10

Damien