简体   繁体   English

Knockout.js基于索引动态绑定img src

[英]knockoutjs data-bind dynamically img src based on index

I have this code: 我有以下代码:

  <tbody>
                        <tr data-bind="foreach: EffectQuantityTotal">
                            <td>
                                <span>
                                    <img data-bind="attr: { src: $root.ProductEffectImages() ? $root.ProductEffectImages()[0].ImageSrc : '../images/style2/pose-select-placeholder.png' }, click: $root.Paste"
                                        width="120" />
                                </span>
                            </td>
                        </tr>
                    </tbody>

$root.ProductEffectImages() will be null until the actual click event 'Paste' will not be done. $ root.ProductEffectImages()将为null,直到不会完成实际的点击事件“粘贴”。 The placeholder image is displayed fine but I want to update the image once the click function is completed. 占位符图像显示正常,但单击功能完成后,我想更新图像。 The object gets created correctly, but I want to get the nth imageurl for each effectquantitytotal, so for example if I have EffectQuantityTotal of 3, for each value I'll create image placeholder and before selecting the ImageUrl the placeholder will be shown but once the image is selected the proper image url will need to be set. 对象创建正确,但是我想为每个effectquantitytotal获取第n个图像网址,因此,例如,如果我的EffectQuantityTotal为3,则将为每个值创建图像占位符,并且在选择ImageUrl之前将显示占位符,但是一旦选择图片后,需要设置正确的图片网址。

So instead of as I have for testing hardcoded now $root.ProductEffectImages()[0].ImageSrc the 0 will be replaced with nth value, probably by using $index instead of 0. 因此,与现在测试硬编码$root.ProductEffectImages()[0].ImageSrc的方式不同,0将会替换为nth值,可能是使用$ index而不是0。

An observableArray initializes with an empty array, and not null , even if you pass it null . 一个observableArray一个空数组初始化,而不是null的,即使你把它传递null After you create it, you can assign it null and it will really have that value then, and your conditional will allow you to use 创建它之后,可以将其分配为null ,然后它将真正具有该值,并且您的条件将允许您使用

src: $root.ProductEffectImages() ? $root.ProductEffectImages()[$index() - 1].ImageSrc : ...

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

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