简体   繁体   English

如果基于Json值进行数据绑定,则敲除Js呈现的方式会有所不同

[英]Knockout Js render differently with if data-bind based on a Json value

I've got a similar to the below code and i want to render differently the capital with cityName Barnsley and differently a city with some other name. 我有一个与以下代码相似的代码,并且我想使用cityName Barnsley来渲染首都,并使用其他名称来渲染城市。 What is the right way to do the if binding ? 进行if绑定的正确方法是什么? Do i have to put cityName as observable to work? 我是否必须将cityName设置为可观察到工作?

<ul data-bind="foreach: planets">
    <li>
        Planet: <b data-bind="text: name"> </b>
        <div data-bind="if: capital">
            Capital: <b data-bind="text: capital.cityName"> </b>
        </div>
    </li>
</ul>


<script>
    ko.applyBindings({
        planets: [
            { name: 'Mercury', capital: null }, 
            { name: 'Earth', capital: { cityName: 'Barnsley' } }        
        ]
    });
</script>

Ok Still no success My json is this: 确定仍然没有成功我的json是这样的:

var Images = {
    "ImageInfo": [
        {
            "thumbs": [
                {
                    "IMAGE": "url(http://...d38508d4f183.jpg)",
                    "type": "img"
                },
                {
                    "IMAGE": "url(http://...d38508d4f183.jpg)",
                    "type": "video"
                }
            ]
        },
        {
            "thumbs": [
                {
                    "IMAGE": "url(http://...d38508d4f183.jpg)",
                    "type": "img"
                },
                {
                    "IMAGE": "url(http://...d38508d4f183.jpg)",
                    "type": "video"
                }
            ]
        }
    ]
};

The HTML i want to achieve is the below: 我要实现的HTML如下:

    <div id="Images">
        <div data-bind="foreach: ViewModelB">

            <div data-bind="foreach: thumbs">
                <div data-bind="if: type == 'img'">
                    <a style="width: 50px; height: 50px; float: left; display: block; background-size: cover; margin-left: 15px; cursor: pointer;" data-bind="style: { backgroundImage: IMAGE_PATH }"></a>
                </div>
                <div data-bind="if: type == 'video'">
                    <p style="width: 50px; height: 50px; float: left; display: block; background-size: cover; margin-left: 15px; cursor: pointer;" data-bind="style: { backgroundImage: IMAGE_PATH }"></p>
                </div>
            </div>
        </div>
    </div>
<script>
var ViewModelB = ko.mapping.fromJS(Images);
        ko.applyBindings(ViewModelB, document.getElementById("Images"));
</script>

Any help will be much appreciated! 任何帮助都感激不尽!

You are almost there, it doesnt have to be observable if its not going change later on, you can do if statements and render differently , something like below 您快到了,如果以后不做更改,它不必是可观察的,如果语句和呈现方式不同,则可以执行此操作,如下所示

<ul data-bind="foreach: planets">
    <li>
        Planet: <b data-bind="text: name"> </b>
        <div data-bind="if: capital">
              <!-- ko if:capital.cityName=='Barnsley' -->
            Capital: <b data-bind="text: capital.cityName"> </b>
              <!-- /ko -->
             <!-- ko if:capital.cityName=='asdad' -->
            Capital: <i data-bind="text: capital.cityName"> </b>
              <!-- /ko -->
        </div>
    </li>
</ul>

Or you can also do switch case https://github.com/mbest/knockout-switch-case 或者您也可以切换大小写https://github.com/mbest/knockout-switch-case

不,它不必是可观察的(只要它以后不会更改;否则,更改将不会自动反映在视图中),并且您建议的绑定几乎正确:它应该是data-bind="if: cityName=='Barnsley'" (注意冒号和引号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM