Question

I am working with knockout js - I have a view page in which i am displaying the same information in two different layouts. I am using the knockout-templates and i have my knockout script in a separate file in which i call in the view page.

Problem
Currently i either have the two different layouts being displayed at the same time (grid view and list view) or i get none depending on how i have my data-bind configured. I want to have a toggle button that will change the layout of the displayed item - i wouldn't mind having two buttons one for grid and one for list.

View page

<div class="btn-group">
    <button type="button" class="btn" data-bind="click: toggleView" >Toggle</button>
</div>


<div data-bind="template: {name:'grid', foreach: Users, visible: grid()}"></div>
<div data-bind="template: {name:'list', foreach: Users, visible: !grid()}"></div>

<script style="float:left" type="text/html" id ="grid">
    <section id="Images" style=" float:left">
    <section id="users">
        <div id="nameImage">
            <figure id="content">
                <img width="158" height="158" alt="Gravatar" data-bind="attr:{src: GravatarUrl}"/>
                <figcaption>
                    <a title="Email" id="emailIcon" class="icon-envelope icon-white" data-bind="attr:{'href':'mailto:' + Email()}"></a>
                    <a title="Profile" id="profileIcon" class="icon-user icon-white"></a>
                </figcaption>
            </figure>
            <p data-bind="text:Name"></p>
            </div>
        </section>
    </section>
</script>


<script style="float:left" type="text/html" id="list">
        <div>
            <div class="border_bottom light buffer" style="width:60%; float:left; margin:10px; height:58px">
                <img style="margin-right:5px; vertical-align:middle" width="58" height="58" alt="Gravatar" data-bind="attr:{src: GravatarUrl}"/>
                <span style="height:58px; vertical-align:middle" data-bind="text:Name"></span>
                <a style=" margin: 5px; vertical-align:middle"  title="Email" class="icon-envelope icon-black" data-bind="attr:{'href':'mailto:' + Email()}"></a>
                <a style=" margin: 5px; vertical-align:middle"  title="Profile"  class="icon-user icon-black"></a>
            </div>
        </div>
</script>

@section scripts{
    @Scripts.Render("~/bundles/user" + ViewBag.Layout.AppVersionForUrls)

    <script type="text/javascript">
        (function ($) {
            $.views.User.GetUser('@url');
        })(jQuery);
    </script>
    }

Knockout js i have in a separate file not within the view

$.views.User.UserViewModel = function (data) {
        var self = this;
        self.Name = ko.observable(data.Name);
        self.Email = ko.observable(data.Email);
        self.MD5Email = ko.observable(data.MD5Email);
        self.GravatarUrl = ko.computed(function () {
           return 'http://www.gravatar.com/avatar/' + self.MD5Email() + '?s=300&d=identicon&r=G';
        });
        self.grid = ko.observable(true);
        self.toggleView = function () {
            self.grid(!self.grid());
        }
    };

I would really like any help :)

Was it helpful?

Solution

I think your 'visible' binding should be outside of your template binding like this:

<div data-bind="template: {name:'grid', foreach: Users}, visible: grid()"></div>
<div data-bind="template: {name:'list', foreach: Users}, visible: !grid()"></div>

Here's a fiddle.

OTHER TIPS

You could add a visible: attribute based on an ko.observable such as showGrid or showList to hide each other.

If you make a js.fiddle I can give you a better idea of where to put them.

I had this problem also. Use if and ifnot instead of visible. This solved it for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top