Unable to bind view model from collection using Knockout

Sachin Gaur

I would like to bind the view model item to a part of HTML from collection which is bound to another HTML in the page.
Here is my sample HTML and JS code:

<div>
<div style="width: 200px; float: left;" class="roundedBorder">
    <fieldset id="fieldsetCategory">
        <legend>Category</legend>
        <table>
            <thead>
                <tr>
                    <th>Category Name</th>    
                </tr>    
            </thead>
            <tbody data-bind="foreach: categories">
                <tr data-bind="click: onCategoryClick.bind($data, $parent)">
                    <td data-bind="text: name"></td>
                </tr>
            </tbody>
        </table>
    </fieldset>
</div>
<div id="divCategoryData" style="width: 200px; float: right;" class="roundedBorder">
    <fieldset id="fieldsetCategoryInfo">
        <legend>Category Information</legend>
        <table>
            <tr>
                <td>Catgory Name:</td>
                <td>
                    <strong data-bind="text: name"></strong>
                </td>
            </tr>
            <tr>
                <td>Catgory Address:</td>
                <td>
                    <strong data-bind="text: address"></strong>
                </td>
            </tr>
        </table>
    </fieldset>
</div>

<script type="text/javascript">
    function CategoryModel(name, address) {
        var self = this;
        this.name = name;
        this.address = address;
    }

    function CategoryViewModel() {
        var self = this;
        self.categories = [
            new CategoryModel("Category 1", "Delhi"),
            new CategoryModel("Category 2", "Gurgaon"),
            new CategoryModel("Category 3", "Noida"),
            new CategoryModel("Category 4", "Ghaziabad")
        ];
    }

    self.onCategoryClick = function () {
        var model = this;
        alert(model.name + " " + model.address);
        ko.applyBindings(model, "divCategoryData");

    };

    $(document).ready(function() {
        ko.applyBindings(new CategoryViewModel());
        ko.applyBindings(new CategoryModel(), "divCategoryData");
    });

</script>

I want to bind the CategoryModel object to "divCategoryData" html section. As you can see, model is being passed to the click of row event. However, I am unable to display the category name & address on selection from the list.
Can someone guide me the code snippet of "self.onCategoryClick" event?

Damien

In your case I wouldn't try to rebind a div on each selection change.

I suggest to create an selectedCategory observable that contains the selected Category.

function CategoryModel(name, address) {
    var self = this;
    this.name = name;
    this.address = address;
}

function CategoryViewModel() {
    var self = this;
    self.categories = [
    new CategoryModel("Category 1", "Delhi"),
    new CategoryModel("Category 2", "Gurgaon"),
    new CategoryModel("Category 3", "Noida"),
    new CategoryModel("Category 4", "Ghaziabad")];

    self.selectedCategory = ko.observable();

    self.onCategoryClick = function (model) {
        //alert(model.name + " " + model.address);
        self.selectedCategory(model);

    };
}

ko.applyBindings(new CategoryViewModel());

See this fiddle for demo

I hope it helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to model bind a collection within a view model

From Dev

Benefit of declaring knockout view model using IIFE

From Dev

Unable to bind a view with complex object in Knockout.js

From Dev

Knockout one way binding from model to view

From Dev

How to update view model data from another view model in Knockout

From Dev

how to bind several view models in a single page using knockout?

From Dev

Bind modal data to knockout model

From Dev

unable to bind observable array - knockout

From Dev

MVC - Sending collection View Model using AutoMapper

From Dev

Issue loading knockout components view model using requireJS

From Dev

Calling method from view in model collection?

From Dev

Call function from outside master view model in knockout.js

From Dev

Knockout.js Calling API from View Model

From Dev

WPF CustomControl unable to bind dependency property two way to view model

From Dev

Cannot bind model to collection

From Dev

Knockout MVC, bind model & add object to model

From Dev

How to data bind from view to view model in Xamarin?

From Dev

Passing a Backbone model from a collection to a new view keeps the collection in memory

From Dev

Knockout view model with mapping plugin

From Dev

Using function from model in view

From Dev

Backbone bind Model to View

From Dev

How to Bind OData $count from expanded collection in an XML view

From Dev

Unable to bind ViewModel to View

From Dev

Pass one model from a partial view to a Parent View and bind another model to the same Parent View

From Dev

Need to call ViewModal Method from Model Class using knockout binding

From Dev

How to bind Knockout model data to Multiple tables

From Dev

to bind multiple knockout viewmodel scripts to one view

From Dev

Using Knockout to update the view from JSON via click event

From Dev

How to bind dropdownlist in partial view in mvc 4 using model?

Related Related

  1. 1

    Unable to model bind a collection within a view model

  2. 2

    Benefit of declaring knockout view model using IIFE

  3. 3

    Unable to bind a view with complex object in Knockout.js

  4. 4

    Knockout one way binding from model to view

  5. 5

    How to update view model data from another view model in Knockout

  6. 6

    how to bind several view models in a single page using knockout?

  7. 7

    Bind modal data to knockout model

  8. 8

    unable to bind observable array - knockout

  9. 9

    MVC - Sending collection View Model using AutoMapper

  10. 10

    Issue loading knockout components view model using requireJS

  11. 11

    Calling method from view in model collection?

  12. 12

    Call function from outside master view model in knockout.js

  13. 13

    Knockout.js Calling API from View Model

  14. 14

    WPF CustomControl unable to bind dependency property two way to view model

  15. 15

    Cannot bind model to collection

  16. 16

    Knockout MVC, bind model & add object to model

  17. 17

    How to data bind from view to view model in Xamarin?

  18. 18

    Passing a Backbone model from a collection to a new view keeps the collection in memory

  19. 19

    Knockout view model with mapping plugin

  20. 20

    Using function from model in view

  21. 21

    Backbone bind Model to View

  22. 22

    How to Bind OData $count from expanded collection in an XML view

  23. 23

    Unable to bind ViewModel to View

  24. 24

    Pass one model from a partial view to a Parent View and bind another model to the same Parent View

  25. 25

    Need to call ViewModal Method from Model Class using knockout binding

  26. 26

    How to bind Knockout model data to Multiple tables

  27. 27

    to bind multiple knockout viewmodel scripts to one view

  28. 28

    Using Knockout to update the view from JSON via click event

  29. 29

    How to bind dropdownlist in partial view in mvc 4 using model?

HotTag

Archive