Modifying values inside data-bind in knockout

Sreevisakh

I was developing a page using knockout. I have bind an array to the html as follows

data:

{'options':[{'name':'hi'},{'name':'hello'}])

html:

 <div data-bind="foreach: options">
       <div data-bind="text: name, css: name"></diV>
    </div>

Each option have an image and i am using css sprites. and the class name are like

div.option-<name>

css:

div.option-hi{
  background-color:black;
}
div.option-hello{
  background-color:black;
}

So this will only work if I can append

"div.option-"

to the name attribute inside data-bind. Is there anyway I can achieve this without modifying the array and adding one more attribute in to the populate array

nemesv

If you don't plan to change your array values or wrap them into viewmodels which will do the additional logic, then you can write expressions also directly in the bindings.

So you can do the string concatenation inside the css binding:

<div data-bind="foreach: options">
    <div data-bind="text: name, css: 'option-' + name"></div>
</div>

Demo JSFiddle.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting data bind values to strings inside foreach knockout

From Dev

Knockout Modifying ObservableArray inside Computed

From Dev

Knockout Modifying ObservableArray inside Computed

From Dev

Knockout - data-bind overriging other elements inside an html element

From Dev

Can't bind data inside a tag that has data binded already Knockout js

From Dev

Bind modal data to knockout model

From Dev

knockout data-bind not working?

From Dev

Knockout Mapping data bind not working

From Dev

Knockout data-bind click

From Dev

Knockout data-bind in function

From Dev

In knockout data-bind attr bind not working?

From Dev

Is it possible to data-bind to a select dropdown with knockout while inside a foreach binding on a html table?

From Dev

Data-Bind List-View using Knockout inside a Collapsible Set

From Dev

Modifying values inside an action using action filters

From Dev

Modifying sysctl values inside lxc container

From Dev

knockout data-bind for radio button

From Dev

knockout data-bind syntax in js ( not HTML)

From Dev

knockout js: data-bind click not working

From Dev

Data-bind for style not working in knockout js

From Dev

Knockout data-bind attribute in razor syntax

From Dev

Checkbox will not stay checked with the data-bind in Knockout

From Dev

knockout javascript data-bind dropdown list

From Dev

Knockout component data bind from jquery

From Dev

how to dataurl with knockout data-bind

From Dev

How to bind Knockout model data to Multiple tables

From Dev

Knockout component data bind from jquery

From Dev

data bind if does show div knockout js

From Dev

Cleaner data-bind within Knockout?

From Dev

Knockout data-bind css if function

Related Related

HotTag

Archive