Knockout.js 找不到 ID 错误的模板

发布于 2024-12-08 14:55:20 字数 2235 浏览 0 评论 0原文

救命,我被困住了! 我正在尝试在 knockout.js 中执行一项简单的任务。基本上,我想让一个项目数组在表中生成一系列行。我正在使用 jquery 和 jquery.tmpl.js。我以前曾多次这样做过,但由于某种原因它不起作用。这是我的代码。

ASPX 页面

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" ClientIDMode="Static" Inherits="EditableGridPrototype._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="Styles/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script src="Styles/jquery.tmpl.js" type="text/javascript"></script>
    <script src="Styles/knockout-1.2.1.debug.js" type="text/javascript"></script>
    <script src="Styles/knockout.mapping.js" type="text/javascript"></script>
    <script src="Scripts/jquery.json-2.2.min.js" type="text/javascript"></script>
    <script src="Grid.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <h3>Transactions</h3>
        <input type="checkbox" data-bind="checked: canSelect" />
        <span data-bind="text: existingTransactions().length" />
        <table width="99%" style="margin-top: 10px" data-bind='template: "existingTransactionsTemplate"'>
            <script type='text/html' id='existingTransactionsTemplate'> 
            {{each(i, tran) existingTransactions()}}        
                        <tr><td>hello</td></tr>          
            {{/each}}                                  
        </script>
        </table>
</asp:Content>

复选框和跨度的绑定起作用。该复选框被选中,并且 2 被写入页面。

这是我的 js Grid.js 文件

$(document).ready(function () {

    var transactionsViewModel = {
        canSelect: ko.observable(true),
        existingTransactions: ko.observableArray([
            { canSelect: true, amount: 100 },
            { canSelect: false, amount: 200}])
    };

    ko.applyBindings(transactionsViewModel);

});

使用的模板很简单,我希望它首先显示行,所以我知道这一切都有效。是的,我不小心把js文件放在Styles文件夹中,这只是一个原型。 :)

感谢您的帮助!!

Help, I'm stuck!
I'm trying to do a simple task in knockout.js. Basically, I'd like to have an array of items generate a series of rows in a table. I'm using jquery, and jquery.tmpl.js. I've done this before many times, but for some reason it is not working. Here's my code.

ASPX Page

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" ClientIDMode="Static" Inherits="EditableGridPrototype._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="Styles/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script src="Styles/jquery.tmpl.js" type="text/javascript"></script>
    <script src="Styles/knockout-1.2.1.debug.js" type="text/javascript"></script>
    <script src="Styles/knockout.mapping.js" type="text/javascript"></script>
    <script src="Scripts/jquery.json-2.2.min.js" type="text/javascript"></script>
    <script src="Grid.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <h3>Transactions</h3>
        <input type="checkbox" data-bind="checked: canSelect" />
        <span data-bind="text: existingTransactions().length" />
        <table width="99%" style="margin-top: 10px" data-bind='template: "existingTransactionsTemplate"'>
            <script type='text/html' id='existingTransactionsTemplate'> 
            {{each(i, tran) existingTransactions()}}        
                        <tr><td>hello</td></tr>          
            {{/each}}                                  
        </script>
        </table>
</asp:Content>

The bindings for the checkbox and span do work. The checkbox is checked, and 2 is written out to the page.

Here's my js Grid.js file

$(document).ready(function () {

    var transactionsViewModel = {
        canSelect: ko.observable(true),
        existingTransactions: ko.observableArray([
            { canSelect: true, amount: 100 },
            { canSelect: false, amount: 200}])
    };

    ko.applyBindings(transactionsViewModel);

});

The template that is used is trivial, I'd like it to just display rows first, so I know it all works. And yes, I put the js files in the Styles folder by accident, this is just a prototype. :)

Thanks for your help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2024-12-15 14:55:20

将其更改

 <span data-bind="text: existingTransactions().length" />

为:

 <span data-bind="text: existingTransactions().length"></span>

就可以了。

Change this:

 <span data-bind="text: existingTransactions().length" />

to:

 <span data-bind="text: existingTransactions().length"></span>

and you should be good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文