简体   繁体   English

使用Knockout动态添加dom元素

[英]Dynamically adding dom elements using Knockout

I have reached a dead end in searching for a solution to my problem. 在寻找解决方案方面我走到了尽头。 The problem is, i have an .xml File which contains data which that help in generating html page on the fly. 问题是,我有一个.xml文件,其中包含有助于动态生成html页面的数据。 The data looks like this. 数据看起来像这样。 <?xml version="1.0"?><Controls><TextInput XmlNodeBoundTo="TEXTINPUT1" Name="TEXTINPUT1" Id="T1></TextInput><TextInput XmlNodeBoundTo="TEXTINPUT2" Name="TEXTINPUT2" Id="T2"></TextInput></Controls>

Now My problem is for each control if tagName is "TextInput" then i had to add a input <input/> control in the html page. 现在我的问题是每个控件,如果tagName为“ TextInput”,那么我必须在html页面中添加输入<input/>控件。 This i have implemented in basic javascript, but I want to implement using KO, since i am a newbie to KO I am not able understand that much. 我已经用基本的javascript实现了此功能,但是我想使用KO实现,因为我是KO的新手,所以我不太了解。 So guys any tutorials which are similar to my problem would be of great help. 因此,任何与我的问题相似的教程都将对您有很大的帮助。

First you need to make a template and then bind view model to data Pls refer to below psedo code 首先,您需要制作一个模板,然后将视图模型绑定到数据。请参考下面的psedo代码

<tbody data-bind="template: { name: 'comissionTemplate', foreach: Comissions }"></tbody> <script type="text/html" id="comissionTemplate">
    <tr >
        <td><input type='text'  data-bind="value: "based on object passed to obseravable array set the value here"/></td>
</tr>

viewModel = {
Comissions: ko.observableArray([])}
ko.applyBindings(viewModel);

So you need to parse the xml and push the data inside commisions and then bind to script tag 因此,您需要解析xml并将数据推送到委员会内部,然后绑定到脚本标签

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

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