javascript - 如何在 Angular js 智能表中编辑内容

标签 javascript angularjs contenteditable smart-table

我是 java 脚本的新手,所以如果这看起来很基础,我必须道歉。

如何使用 Angularjs 在 Smart-Table 中编辑行表?新的 Smart-Table 似乎没有教程。我想创建一个简单的表单,供用户输入特定地点的营业时间。

我已经创建了可以在表格中添加和删除行的按钮,但是当我添加 contenteditable="true"时,当我更新对象时,所有更改都不会保留。我知道 contenteditable 是独立于智能表的特定 html5 参数,但我不明白我还能如何更新数据或如何检索更新后的数据。

数据是通过 mean.js 路由从 angularjs Controller 中检索的。

<div class="controls">
    <table st-table="place.openHours" class="table table-striped">
        <thead>
        <tr>
            <th>Day</th>
            <th>Opening Time</th>
            <th>Closing Time</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="row in place.openHours" contenteditable="true" >
            <td>{{row.day}}</td>
            <td>{{row.open}}</td>
            <td>{{row.close}}</td>
            <button type="button" ng-click="removeOpenHour(row)" class="btn btn-sm btn-danger">
                <i class="glyphicon glyphicon-remove-circle">
                </i>
            </button>
        </tr>
        </tbody>
    </table>

    <button type="button" ng-click="addOpenHour(row)" class="btn btn-sm btn-success">
        <i class="glyphicon glyphicon-plus">
        </i> Add new Row
    </button>
</div>

在 javascript 中:

    $scope.removeOpenHour = function removeOpenHour(row) {
        var index = $scope.place.openHours.indexOf(row);
        if (index !== -1) {
            $scope.rowCollection.splice(index, 1);
        }
    }

    $scope.addOpenHour = function addOpenHour() {
        $scope.place.openHours.push(
        {
            day: 'Monday',
            open: 540,
            close: 1080
        });
    };

最佳答案

谢谢,我环顾四周并使用了这里的代码 http://jsfiddle.net/bonamico/cAHz7/并将其与我的代码合并。

HTML 文件:

<tr ng-repeat="row in place.openHours">
   <td><div contentEditable="true" ng-model="row.day">{{row.day}}</div></td>
   <td><div contentEditable="true" ng-model="row.open">{{row.open}}</div></td>
   <td><div contentEditable="true" ng-model="row.close">{{row.close}}</div></td>
   <td>
   <button type="button" ng-click="removeOpenHour(row)" class="btn btn-sm btn-danger">
      <i class="glyphicon glyphicon-remove-circle">
      </i>
   </button>
</td>

JS文件:

myApp.directive('contenteditable', function() {
return {
    require: 'ngModel',
    link: function(scope, elm, attrs, ctrl) {
        // view -> model
        elm.bind('blur', function() {
            scope.$apply(function() {
                ctrl.$setViewValue(elm.html());
            });
        });

        // model -> view
        ctrl.render = function(value) {
            elm.html(value);
        };

        elm.bind('keydown', function(event) {
            console.log("keydown " + event.which);
            var esc = event.which == 27,
                el = event.target;

            if (esc) {
                console.log("esc");
                ctrl.$setViewValue(elm.html());
                el.blur();
                event.preventDefault();
            }

        });

    }
};
});

关于javascript - 如何在 Angular js 智能表中编辑内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28446753/

相关文章:

javascript - grunt-notify:成功时不触发

javascript - 具有 0.01 个步骤的数字输入为 angularjs 中的某些值提供未定义

javascript - 获取包含 HTML 内容的 contentEditable 区域中的插入符号(光标)位置

javascript - 在 Aurelia.io 示例中读取 json 时出错

javascript - 倒数计时器的问题

angularjs 选择没有 ng-options 或静态选项

angularjs - 如何在angularjs中链接promise错误函数

javascript - 替代/替代 queryCommandState ('bold' )

javascript - 使内容可编辑部分中的 h2 标签不可删除

javascript - Chart.js - 在气泡图中为特定圆圈提供背景颜色