knockout.js

Get element an observable is bound to with Knockout?

假装没事ソ 提交于 2019-12-18 03:16:11
问题 This isn't an ideal situation, but due to another knockout binding I am using I am in a situation where I am needing to get the element an observable is bound to, if it is indeed bound to anything. So is there a way to do this? == Update == I didn't want to add any extra context incase it confuses the question, but as it may get an answer more in line with expectations here is the scenario. I am using the knockout validation binding, which exposes all the errors using the ko.validation.group

KnockoutJS - Databind to a dictionary collection

故事扮演 提交于 2019-12-18 03:10:35
问题 How do I use KnockoutJS to bind a dictionary collection to a select list. If my "Destinations" dictionary looks like this in JSON: {"Europe":"Europe incl Egypt, Turkey & Tunisia","ANZO":"Australia & New Zealand","WorldwideUSA":"Worldwide (incl USA & Canada)"} How do I bind this to a select list. Something like this: data_bind="value: Destination, options: Destinations.Value, optionsText: Destinations.Key" 回答1: Typically, when dealing with a dictionary, you will want to map it to an array

How to replace a given index element in knockoutjs

ぐ巨炮叔叔 提交于 2019-12-18 01:52:47
问题 How do you replace a given index in an observableArray with another element. I have: ViewModel.Elements()[index] how can i replace it with another element. 回答1: observableArrays do have a replace method. This takes in the old item and the new item. So, you would call it like: ViewModel.Elements.replace(ViewModel.Elements()[index], yourNewElement); Internally, this just sets that index to your new item and calls valueHasMutated() to notify any potential subscribers. 回答2: It's a little late by

Knockout.js - Dynamic columns but limit to a maximum of 5 for each row

☆樱花仙子☆ 提交于 2019-12-17 23:39:45
问题 I found a similar question/answer here: How to render a table with some fixed and some dynamic columns But it does not completely solve my problem. I am trying to figure out how I can limit the number of dynamic columns to 5 per row and if there are more than 5 items in the view model, make a new row and repeat for all groups of 5 in the array. For example: var vm = { item: { name: 'test1' }, item: { name: 'test2' }, item: { name: 'test3' }, item: { name: 'test4' }, item: { name: 'test5' },

What is the best way of cloning/copying an observablearray in knockoutJS?

馋奶兔 提交于 2019-12-17 23:36:26
问题 Question says it all really. I want to copy an observable array to another in KnockoutJS. 回答1: To clone an observableArray you would want to do: var viewModel = { array1: ko.observableArray(["one", "two"]), array2: ko.observableArray() }; viewModel.clone = function() { viewModel.array1(viewModel.array2.slice(0)); }; If you want to just do a copy, then you would do: viewModel.array1(viewModel.array2()); The problem with the second example is that the underlying array is the same, so pushing to

Load knockout template from external file without complex engine?

旧时模样 提交于 2019-12-17 22:23:01
问题 I've found engines, plugins and functions to load external templates, but I'm wondering if there's a simpler way to do it. Something like this? templates.html <script id="testTemplate" type="text/html"> <div>this is a div</div> </script> index.html <div id="templateContainer"></div> <script> $(document).ready(function() { $("#templateContainer").load("templates.html"); } </script> Will this work? Are there any "gotchas"? 回答1: Here's what I use to load a template file that contains a

Knockout serialization with ko.toJSON - how to ignore properties that are null

折月煮酒 提交于 2019-12-17 19:33:08
问题 When using: var dataToSave = ko.toJSON(myViewModel); .. is it possible to not serialize values that are null? Serializing my current viewModel creates around 500Kb of JSON most of which is ends up like: "SomeObject": { "Property1": 12345, "Property2": "Sometext", "Property3": null, "Property4": null, "Property5": null, "Property6": null, "Property7": null, "Property8": null, "Property9": false } If I could get the serializer to ignore null values then this could be reduced down to:

knockout bind text label to dropdown value selected option text

五迷三道 提交于 2019-12-17 18:57:25
问题 Is there a simple way to bind the textbox of a div to change based on the text value of the selected option in a dropdown on the same page? <div data-bind="text: dropdownValue"></div> <select> <option value="1">Value1</option> <option value="2">Value2</option> </select> Please note, I don't want to put the values into the select element using javascript. I'd like to bind to the value straight from the HTML. I can also include jQuery to make it work. 回答1: I was looking for similar

Binding initial/default value of dropdown (select) list

。_饼干妹妹 提交于 2019-12-17 17:54:22
问题 I'm having a small issue with setting the initial value of a dropdown. The code below is the view model definition and the initialization in $(document).ready . I have an array called sourceMaterialTypes and a selectedSourceMaterialType representing the selected value of that array. I am initializing the view model with values from the (ASP.Net MVC) Model and ViewBag. var viewModel = { sourceMaterialTypes : ko.observableArray(@Html.Raw(Json.Encode(ViewBag.SourceMaterialTypes))),

Knockout.js bound input value not updated when I use jquery .val('xyz')

谁说我不能喝 提交于 2019-12-17 17:33:50
问题 I have an input that has a knockout binding to the value. When I update the value using jquery's .val() method, the changed value is not reflected in the viewModel. I need to use jQuery to set this value. How can I trigger the update to the viewModel? 回答1: .val() does not trigger the change event. So, you can just do . val("blah").change() for KO to pick up the changes. 来源: https://stackoverflow.com/questions/7018885/knockout-js-bound-input-value-not-updated-when-i-use-jquery-valxyz