Knockout binding using key-value pairs


Ashishraaj

I'm doing knockout with BreezeJs to retrieve and store data from a local database. The problem I am facing here is the key value binding in the menu. What I do is display and select the "Name" property from the select menu and store the corresponding "Id" property into my database.

until now i am trying

var operators = [
{id: 0, name: 'addition'},
{id: 1, name: 'subtraction'},
{id: 2, name: 'division'},
{id: 3, name: 'multiplication'}
   ];

//my html bindings

<select data-bind="options: $root.operators , optionText: 'name', optionvalue: 'Id', value: operator"/>

// Here 'operator' is the colomnName in the database, which I am trying to store as integer format.

But as per my code it shows as

[object object]

How can I fix this so that the "name" property [varchar] is displayed and selected from the select menu and the corresponding "id" property [int] is stored into my database.

// @ TSV

html binding

 <div data-bind="with: jobs">
      <div>
        <label>FirstNumber :</label>
        <input data-bind="value: first_no" />
    </div>

         <div>
        <label>operator :</label>
         <select data-bind="options: $root.operators , optionsText: 'name', optionsValue: 'id', value: $root.operator"/>
    </div>
     <div>
        <label>Second Number:</label>
        <input data-bind="value: second_no" />
    </div><div>
        <label>Result :</label>
        <input data-bind="value: result" />
    </div></div>

//These (results, second_no, operator, first_no) are column names in my database and "jobs" is an observableArray.

Note: I'm using breedJS to fetch and store data from a database, that's why I mention it's tag, although the question above is a knockout and not a breeze.

TSV

This code works for me:

var operators = [
{id: 0, name: 'addition'},
{id: 1, name: 'subtraction'},
{id: 2, name: 'division'},
{id: 3, name: 'multiplication'}
   ];

ko.applyBindings({ operators: operators, operator: ko.observable(2) });
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script>

<select data-bind="options: $root.operators, optionsText: 'name', optionsValue: 'id', value: 
$root.operator"/>

I have patched the following in your code: inserted comma after "division", corrected "optionsText" and "optionsValue" parameters, optionsValue should be "id".

Related


Knockout binding using key-value pairs

Ashishraaj I'm doing knockout with BreezeJs to retrieve and store data from a local database. The problem I am facing here is the key value binding in the menu. What I do is display and select the "Name" property from the select menu and store the correspondin

Constrained knockout value binding

Wei Ma I am using a knockout table to implement a shopping cart. One of the requirements is to allow customers to change the quantity of items. So naturally I will <input type="text" data-bind="value: quantity"/> In my view mode I have an item ViewModel defin

How to access key/value pairs using variables?

meditation I'm looking to dynamically access key/value pairs using a variable. I can use the sought value topics.social.color, but the theme changes based on the user's selection. How to use theme variable to get theme color dynamically? let topic = 'social';

Dictionaries using Volt::Model as key/value pairs

Rick I want to use Volt::Model as reactive dictionary in view. Ideally I want something like this: <dl> {{ dictionary.each do |key, val| }} <dt>Term: {{ key }}</dt> <dd>Definition: {{ val }}</dd> {{ end }} </dl> Is it possible to implement in Volt wit

(key, value) pairs using Python Lambdas

Onka I'm trying to solve a simple word count problem and trying to figure out if it's possible to do this by using map, filter and reduce. Here is an example of wordRDD (list for spark): myLst = ['cats', 'elephants', 'rats', 'rats', 'cats', 'cats'] All I need

How to access key/value pairs using variables?

meditation I'm looking to dynamically access key/value pairs using a variable. I can use the sought value topics.social.color, but the theme changes based on the user's selection. How to use theme variable to get theme color dynamically? let topic = 'social';

Storing key, value pairs using Java

Awinash I would like to know the best data structure for use in java for the following senario. There is a key and a value. And the key is not repeated, Each value should store a collection of objects, where the value in each object will change frequently. tha

How to access key/value pairs using variables?

meditation I'm looking to dynamically access key/value pairs using a variable. I can use the sought value topics.social.color, but the theme changes based on the user's selection. How to use theme variable to get theme color dynamically? let topic = 'social';

Dictionaries using Volt::Model as key/value pairs

Rick I want to use Volt::Model as reactive dictionary in view. Ideally I want something like this: <dl> {{ dictionary.each do |key, val| }} <dt>Term: {{ key }}</dt> <dd>Definition: {{ val }}</dd> {{ end }} </dl> Is it possible to implement in Volt wit

Access key, value pairs in a dictionary using integers?

Muzamir Question: How can I use integers as keys of a dictionary? Explanation: I have a dictionary example = {} I populate it with key, value pairs A: something B: something C: something 1: something 2: something 3: something 4: something 5: something 6: some

Convert array to object using key/value pairs

Patrick I'm still learning JavaScript, so my apologies in advance if this is a remedial question. I receive data from an API call in the form of an array like this: arr = ['topic1',497,'topic2',591,'topic3',17,'topic4',980] Although not visible from this arra

How to access key/value pairs using variables?

meditation I'm looking to dynamically access key/value pairs using a variable. I can use the sought value topics.social.color, but the theme changes based on the user's selection. How to use theme variable to get theme color dynamically? let topic = 'social';

How to access key/value pairs using variables?

meditation I'm looking to dynamically access key/value pairs using a variable. I can use the sought value topics.social.color, but the theme changes based on the user's selection. How to use theme variable to get theme color dynamically? let topic = 'social';

Reduce key-value pairs to keylist pairs using Apache Spark

TravisJ: I'm writing a Spark application and want to combine a set of key-value pairs (K, V1), (K, V2), ..., (K, Vn)into a "key-multivalue" pair (K, [V1, V2, ..., Vn]). I feel like I should be able to do this using a function reduceByKeywith some flavor : My_K

Reduce key-value pairs to keylist pairs using Apache Spark

TravisJ: I'm writing a Spark application and want to combine a set of key-value pairs (K, V1), (K, V2), ..., (K, Vn)into a "key-multivalue" pair (K, [V1, V2, ..., Vn]). I feel like I should be able to do this using a function reduceByKeywith some flavor : My_K

Reduce key-value pairs to keylist pairs using Apache Spark

TravisJ: I'm writing a Spark application and want to combine a set of key-value pairs (K, V1), (K, V2), ..., (K, Vn)into a "key-multivalue" pair (K, [V1, V2, ..., Vn]). I feel like I should be able to do this using a function reduceByKeywith some flavor : My_K

Knockout using $index and if binding

User1255162: I'm trying to display some marker based on a value that I $indexcan display, but can't seem to get it to work with binding if, what's the best way to do this here? <!-- ko if: $index===0 --> <div>some mark up here</div> <!-- /ko --> John Earles

Knockout using $index and if binding

User1255162: I'm trying to display some marker based on a value that I $indexcan display, but can't seem to get it to work with binding if, what's the best way to do this here? <!-- ko if: $index===0 --> <div>some mark up here</div> <!-- /ko --> John Earles

Knockout using $index and if binding

User1255162: I'm trying to display some marker based on a value that I $indexcan display, but can't seem to get it to work with binding if, what's the best way to do this here? <!-- ko if: $index===0 --> <div>some mark up here</div> <!-- /ko --> John Earles

How to apply binding and persist input value using Knockout JS?

Kees C. Bakker I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model. Is there a way to automati

How to apply binding and persist input value using Knockout JS?

Kees C. Bakker I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model. Is there a way to automati

How to apply binding and persist input value using Knockout JS?

Kees C. Bakker I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model. Is there a way to automati

Knockout JS binding doesn't output value using simple viewmodel

Carl Weiss I'm trying to load a simple view model that has two models: Profile (a ko.observable()) and Request (a ko.observableArray()). When I apply the binding and then try to bind to the ether profile.field or foreach: request, I get no output, no javascrip

How to apply binding and persist input value using Knockout JS?

Kees C. Bakker I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model. Is there a way to automati

How to apply binding and persist input value using Knockout JS?

Kees C. Bakker I am building an HTML/KnockoutJS application. My web server returns a form with input fields and information. When I update the model and execute ko.applyBindings, the input value is naturally overwritten by the model. Is there a way to automati

Parse a string of key, value pairs using known key delimiters

anon01 How to convert string to dict if key string is a known substring with delimiter? example: s = 'k1:text k2: more text k3:andk4: more yet' key_list = ['k1','k2','k3'] (missing code) # s_dict = {'k1':'text', 'k2':'more text', 'k3':'andk4: more yet'} In