【问题标题】:knockout js template passing json data source to template淘汰js模板将json数据源传递给模板
【发布时间】:2014-07-10 16:10:45
【问题描述】:

我是 Knockout 及其模板功能的新手,我有以下 html,有人可以帮我构建一个合适的模板来替换它的字符串文字和数据源,我正在使用 x-editable,#

<tr data-bind="foreach: fundClasses">
    <td class="span1">
        <span data-bind="visible: $index() == 0">  Rate Index</span>
    </td>
    <td class="span1 protected">
        <span data-bind=" editable:INDEX_ID,  editableOptions: {name:'INDEX_ID',mode: 'popup',type: 'select', source: rateIndex,pk: ID, url: '/create/EditInPlace'}"></span>
    </td>
</tr>

我想替换上面sn-ps中的rateIndex,INDEX_ID,非常感谢任何帮助。

谢谢 乔治

【问题讨论】:

    标签: javascript jquery knockout.js knockout-mapping-plugin x-editable


    【解决方案1】:

    我自己想通了,所以想回答一下,这样可以帮助其他寻找类似问题的人,

    模板:

    <script id="textBoxTemplate" type=“text/html”>
       <!-- ko foreach: $data -->
        <td class="span1">
            <span data-bind="visible: $index() == 0,text:title">  </span>
        </td>
        <td class="span1 protected">
             <span data-bind="editable:$data[field],editableOptions: {  mode: 'popup', pk: ID, url: '/create/EditInPlace'}"></span>
       </td>
       <!-- /ko -->
    

    用法:

    <tr data-bind="templateWithContext: { name: 'textBoxTemplate', data: fundClasses, context: { title: 'Index', field:'INDEX_ID' }}"></tr>
    

    绑定处理程序:

    ko.bindingHandlers.templateWithContext = {
        init: ko.bindingHandlers.template.init,
        update: function (element, valueAccessor, allBindings, data, context) {
            var options = ko.utils.unwrapObservable(valueAccessor());
    
            ko.utils.extend(context, options.context);
    
            return ko.bindingHandlers.template.update.apply(this, arguments);
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2021-03-09
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 2013-05-08
      • 2016-02-27
      • 2021-02-11
      • 2014-01-02
      相关资源
      最近更新 更多