2

是否有任何理由为什么以下javascript和html不会出现一行?我在我的网格对象中看到了该对象,但它没有出现在表格中。难道我做错了什么?

查看模型

    app.viewModel.members.eventSchedule = {
            gyms: ko.observableArray(null),
            grid: ko.observableArray([]),

    .
    .
    .

从 Ajax 请求填充初始网格

function populateGrid(grid) {

        var mapping = {
            'Matchup': {
                create: function (options) {
                    if (options.data) {
                        return ko.mapping.fromJS(options.data);
                    }

                    return null;
                }
            }
        };

        viewModel.grid(ko.mapping.fromJS(grid.Dates, mapping)());
        viewModel.gyms(grid.Gyms);
    }

添加新行

                         var timeRow = {
                            Time: ko.observable(data.Newtime),
                            Games: ko.observableArray([])
                        };

                        var gymCourts = viewModel.gymCourts();
                        for (var k = 0; k < gymCourts.length; k++) {
                            timeRow.Games.push(new Game(data.Dates, data.NewTime, gymCourts[k].Id, gymCourts[k].Order));
                        }

                        viewModel.grid()[0].Times().push(timeRow);

表格 HTML

    <tbody data-bind="foreach: grid">
                                <tr>
                                    <td class="empty"></td>
                                    <td class="date-container" data-bind="attr: { colspan: $parent.gymCourts().length }">
                                        <div class="date" data-bind="html: Date"></div>
                                    </td>
                                </tr>
                                <!-- ko foreach: Times -->
                                <tr class="time-games">
                                    <td class="time-container">
                                        <a href="#" class="time" data-bind="html: Time, click: $root.members.eventSchedule.editTime.edit.bind($data, $parent)"></a>
                                    </td>
                                    <!-- ko foreach: Games() -->
                                    <td class="game-container draggable-item-container clearfix">
                                        <div class="game clearfix draggable-active draggable-item" data-bind="if: $data.Matchup, css: { 'empty' : !$data.Matchup, 'ui-droppable-disabled': $data.Disabled }, dynamicClass: { position: ($data.Matchup ? $data.Matchup.DivisionOrder() : 0) }, draggableGameHandler : { disabled: !$data.Matchup, disabledDrop: $data.Disabled() }">
                                            <span data-bind="html: (Matchup.PoolName() + Matchup.AwayTeam.Number())"></span> vs. <span data-bind="html: (Matchup.PoolName() + Matchup.HomeTeam.Number())"></span>
                                        </div>
                                    </td>
                                    <!-- /ko -->
                                </tr>
                                <!-- /ko -->
                            </tbody>
4

1 回答 1

1

检查这一行: viewModel.grid()[0].Times().push(timeRow); 您正在将一个新的 timeRow 推入 observableArray 内的数组中。这不会自动通知侦听器。你可以试试 viewModel.grid()[0].Times.push(timeRow); 反而?

于 2012-05-28T23:08:58.777 回答