在knockoutJS中嵌套的foreach绑定(Nested foreach binding in knockoutJS)

我想通过敲击JS foreach绑定在AJAX数据的帮助下创建手风琴。 每个Accordion组都包含一个表,当在敲击JS点击绑定的帮助下点击特定的手风琴时,该表的数据应该通过另一个AJAX调用获取。

我面临的问题:

第一个foreach绑定工作正常,没有任何问题,它根据返回的数据创建确切数量的手风琴组。 手风琴上的Click绑定也很有效,并通过AJAX调用获得正确的数据。

然而,当我为每个绑定添加第二个时刻时,一切都崩溃了。 即使是在第一个手风琴组之后,foreach绑定也会停止渲染。

HTML:

<div class="accordion" id="accordion" data-bind="foreach: years">
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-parent="#accordion"
               data-toggle="collapse" href="" data-bind="click: $parent.get_statement, attr: { href: '#' + year0,}, text: year2 + ' ' + year0 + ' - ' + year1">Financial Year
                2014-15</a>
        </div>
        <div class="accordion-body collapse" id="" data-bind="attr: {id: year0}">
            <div class="accordion-inner">
                <table class="table">
                    <thead>
                    <tr>
                        <th>Column A</th>
                        <th>Column B</th>
                        <th>Column C</th>
                        <th>Column D</th>
                        <th>Column E</th>
                        <th>Column F</th>
                    </tr>
                    </thead>
                    <tbody data-bind="foreach: statements">
                    <tr>
                        <td data-bind="text: A">cell is row 0, column 0</td>
                        <td data-bind="text: B">cell is row 0, column 1</td>
                        <td data-bind="text: C">cell is row 0, column 2</td>
                        <td data-bind="text: D">cell is row 0, column 3</td>
                        <td data-bind="text: E">cell is row 0, column 4</td>
                        <td data-bind="text: F">cell is row 0, column 5</td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

JS代码:

this.years = ko.observableArray();
this.statements = ko.observableArray();

$.ajax({
            url: "site_api/user_data.php",
            type: "get",
            data: {data_type: getParam("id"), request_type: "account_statement_years", data_value: 0},
            cache: false,
            success: function(result) {
                var data = $.parseJSON(result);
                self.years(data);
            }
        });

        this.get_statement = function() {
            var c = self.years.indexOf(this);
            $.ajax({
                url: "site_api/user_data.php",
                type: "get",
                data: {data_type: getParam("id"), request_type: "account_statement_data", data_value: self.years()[c].year0},
                cache: false,
                success: function(result) {
                    var data = $.parseJSON(result);
                    self.statements(data);
                }
            });
        };

样本数据:

Years: [{year0: 2015, year2:16, year3: ABC},{year0: 2014, year2:15, year3: DEF},{year0: 2013, year2:14, year3: GHI}]

声明:

[{"A":"AQA", "B":"BQB", "C":"CQC", "D":"DQD", "E":"EQE", "F":"FQF"},
{"A":"AQA", "B":"BQB", "C":"CQC", "D":"DQD", "E":"EQE", "F":"FQF"},
{"A":"AQA", "B":"BQB", "C":"CQC", "D":"DQD", "E":"EQE", "F":"FQF"}]

I want to create accordions with the help of AJAX data through knockout JS foreach binding. Each Accordion group includes a table whose data is supposed to be fetched through another AJAX call when the particular accordion is clicked with the help of knockout JS click binding.

Issue that i am facing:

First foreach binding works well without any issues and it creates the exact number of accordion groups as per the data returned. Click binding on accordion also works well and gets correct data through AJAX call.

However, the moment i add the second for each binding everything falls apart. Even the years foreach binding stops rendering after the first accordion group.

HTML:

<div class="accordion" id="accordion" data-bind="foreach: years">
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-parent="#accordion"
               data-toggle="collapse" href="" data-bind="click: $parent.get_statement, attr: { href: '#' + year0,}, text: year2 + ' ' + year0 + ' - ' + year1">Financial Year
                2014-15</a>
        </div>
        <div class="accordion-body collapse" id="" data-bind="attr: {id: year0}">
            <div class="accordion-inner">
                <table class="table">
                    <thead>
                    <tr>
                        <th>Column A</th>
                        <th>Column B</th>
                        <th>Column C</th>
                        <th>Column D</th>
                        <th>Column E</th>
                        <th>Column F</th>
                    </tr>
                    </thead>
                    <tbody data-bind="foreach: statements">
                    <tr>
                        <td data-bind="text: A">cell is row 0, column 0</td>
                        <td data-bind="text: B">cell is row 0, column 1</td>
                        <td data-bind="text: C">cell is row 0, column 2</td>
                        <td data-bind="text: D">cell is row 0, column 3</td>
                        <td data-bind="text: E">cell is row 0, column 4</td>
                        <td data-bind="text: F">cell is row 0, column 5</td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

JS Code:

this.years = ko.observableArray();
this.statements = ko.observableArray();

$.ajax({
            url: "site_api/user_data.php",
            type: "get",
            data: {data_type: getParam("id"), request_type: "account_statement_years", data_value: 0},
            cache: false,
            success: function(result) {
                var data = $.parseJSON(result);
                self.years(data);
            }
        });

        this.get_statement = function() {
            var c = self.years.indexOf(this);
            $.ajax({
                url: "site_api/user_data.php",
                type: "get",
                data: {data_type: getParam("id"), request_type: "account_statement_data", data_value: self.years()[c].year0},
                cache: false,
                success: function(result) {
                    var data = $.parseJSON(result);
                    self.statements(data);
                }
            });
        };

Sample Data:

Years: [{year0: 2015, year2:16, year3: ABC},{year0: 2014, year2:15, year3: DEF},{year0: 2013, year2:14, year3: GHI}]

Statements:

[{"A":"AQA", "B":"BQB", "C":"CQC", "D":"DQD", "E":"EQE", "F":"FQF"},
{"A":"AQA", "B":"BQB", "C":"CQC", "D":"DQD", "E":"EQE", "F":"FQF"},
{"A":"AQA", "B":"BQB", "C":"CQC", "D":"DQD", "E":"EQE", "F":"FQF"}]

原文:https://stackoverflow.com/questions/31254763
2024-03-01 21:03

相关文章

更多

JSTL详解之c:forEach

JSTL详解之c:forEach,status.current 当前这次迭代的(集合中的)项 stat ...

There is already an open DataReader associated with this Connection which must be closed first

使用MVC4 EF Linq获取foreach列表循环的时候遇到了如下的问题:报错提示 Ther ...

ExtJS3.2,在FormPanel中嵌套TabPanel和GridPanel后无法正常显示

原本在没有添加FormPanel的情况下可以正常显示,但添加FormPanel后就无法正常显示页面布局 ...

JavaFx Main中怎么绑定Java类中的一个static变量呢?

最近遇到一个很烦的问题,搞了很久搞不定,想请各位帮忙,看看有没有解决的办法,我建了个JavaFx应用程 ...

怎么递归json嵌套json?

比如JSONObject: {&quot;temp1&quot;:{&quot;b&quot;:&q ...

spring 嵌套事务 的 加锁 问题

有两个service:ServiceA 和 ServiceB ServiceA { metho ...

spring mvc 页面传值到controller

一个页面上有很多输入框,我想把这些值通过json格式传到controller中,通过注解应该如何实现? ...

Hadoop pig进阶语法

。Pig Latin是一种数据流语言,变量的命名规则同java中变量的命名规则,变量名可以复用(不建议 ...

关于一个页面多个form问题????????

当前我的页面是用一个&lt;c:forEach &gt;遍历出来的。有多个form每个都不一样,我可以 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本