knockout.js和表没有添加新行

时间:2012-05-27 23:57:38

标签: knockout.js html-table

有没有理由为什么行不会出现以下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>

1 个答案:

答案 0 :(得分:1)

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