为什么在子foreach中不能访问ObservableArray对象?

时间:2019-07-06 15:09:14

标签: javascript html knockout.js

您好,我是淘汰赛的新手,我被困在这样的情况下:我正在创建一个嵌套有foreach的表,但内循环在与外循环匹配的条件下起作用,但是当上面的对象是给定的普通数组时,它就起作用了。

viewModel.Subject=ko.observableArray(['Science','Math']);

在给定的observable以下不适用于绑定子元素

viewModel.SubjectClass=ko.observableArray([{'Class':'8','Subject':'Science'},{'Class':'7','Subject':'Math'},{'Class':'8','Subject':'Science'},{'Class':'6','Subject':'Math'}]);

 var viewModel={};
            viewModel.Subject=ko.observableArray(['Science','Math']);
            viewModel.SubjectClass=ko.observableArray([{'Class':'8','Subject':'Science'},{'Class':'7','Subject':'Math'},{'Class':'8','Subject':'Science'},{'Class':'6','Subject':'Math'}]);
            viewModel.Students=ko.observableArray([
                {'StId':1,'StName':'John','Class':'8','Subject':'Science'},
                {'StId':2,'StName':'Martha','Class':'7','Subject':'Math'},
                {'StId':3,'StName':'George','Class':'6','Subject':'Math'},
                {'StId':4,'StName':'Martin','Class':'8','Subject':'Science'},
                {'StId':5,'StName':'Donny','Class':'7','Subject':'Math'},
                {'StId':6,'StName':'Kit','Class':'6','Subject':'Science'},
                {'StId':7,'StName':'Albert','Class':'8','Subject':'Math'}
            ]);
            ko.applyBindings(viewModel);
 body{
                background-color: white;
            }
            .subjectHeader{
                font-weight: bolder;
                font-size: 14px;

            }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table class='table table-condensed table-bordered'>
            <thead>
                <th>St Id</th>
                <th>St Name</th>
                <th>Class</th>
                <th>Subject</th>
            </thead>
            <!--<tbody data-bind='foreach: viewModel.SubjectClass'>-->
            <tbody data-bind='foreach: viewModel.Subject'>
                <tr><td colspan="4">
                    <p class="subjectHeader">
                        <!--<span data-bind="text:Class"></span>
                        <span data-bind='text:Subject' style="margin-left: 40%;"></span>-->
                        <span data-bind='text:$data' style="margin-left: 40%;"></span>
                    </p>
                    <table class='table table-condensed table-bordered'> 
                            <tbody data-bind="foreach:viewModel.Students">
                                <!--<tr data-bind="if: Subject == $parentContext.Subject">-->
                                <tr data-bind="if: Subject == $parentContext.$data">
                                    <td data-bind='text:StId'></td>
                                    <td data-bind='text:StName'></td>
                                    <td data-bind='text:Subject'></td>
                                    <td data-bind='text:Class'></td>
                                </tr>
                            </tbody>
                        </table>
                </td>
                </tr>                
            </tbody>
        </table>

我了解此问题是由于 $ parentContext 所致,因为它没有引用observableArray,这就是为什么'Subject'数组起作用但不能为SubjectClass绑定子循环的原因。我评论过的无效部分。

1 个答案:

答案 0 :(得分:1)

您只需要使用保存实际数据的$parent,而不是引用绑定上下文的$parentContext

 var viewModel={};
            viewModel.Subject=ko.observableArray(['Science','Math']);
            viewModel.SubjectClass=ko.observableArray([{'Class':'8','Subject':'Science'},{'Class':'7','Subject':'Math'},{'Class':'8','Subject':'Science'},{'Class':'6','Subject':'Math'}]);
            viewModel.Students=ko.observableArray([
                {'StId':1,'StName':'John','Class':'8','Subject':'Science'},
                {'StId':2,'StName':'Martha','Class':'7','Subject':'Math'},
                {'StId':3,'StName':'George','Class':'6','Subject':'Math'},
                {'StId':4,'StName':'Martin','Class':'8','Subject':'Science'},
                {'StId':5,'StName':'Donny','Class':'7','Subject':'Math'},
                {'StId':6,'StName':'Kit','Class':'6','Subject':'Science'},
                {'StId':7,'StName':'Albert','Class':'8','Subject':'Math'}
            ]);
            ko.applyBindings(viewModel);
 body{
                background-color: white;
            }
            .subjectHeader{
                font-weight: bolder;
                font-size: 14px;

            }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table class='table table-condensed table-bordered'>
            <thead>
                <th>St Id</th>
                <th>St Name</th>
                <th>Class</th>
                <th>Subject</th>
            </thead>
            <tbody data-bind='foreach: viewModel.SubjectClass'>
            <!--<tbody data-bind='foreach: viewModel.Subject'>-->
                <tr><td colspan="4">
                    <p class="subjectHeader">
                        <span data-bind="text:Class"></span>
                        <span data-bind='text:Subject' style="margin-left: 40%;"></span>
                        <!--<span data-bind='text:$data' style="margin-left: 40%;"></span>-->
                    </p>
                    <table class='table table-condensed table-bordered'> 
                            <tbody data-bind="foreach:viewModel.Students">
                                <tr data-bind="if: Subject == $parent.Subject">
                                <!--<tr data-bind="if: Subject == $parentContext.$data">-->
                                    <td data-bind='text:StId'></td>
                                    <td data-bind='text:StName'></td>
                                    <td data-bind='text:Subject'></td>
                                    <td data-bind='text:Class'></td>
                                </tr>
                            </tbody>
                        </table>
                </td>
                </tr>                
            </tbody>
        </table>