简体   繁体   English

如何使用angular JS制作动态内容可编辑表?

[英]How to make dynamic content-editable table using angular JS?

Caveat : I've just started with client side scripting and Angular JS is the first thing I'm learning and now I feel I should've started with javascript. 警告 :我刚开始使用客户端脚本,Angular JS是我正在学习的第一件事,现在我觉得我应该开始使用javascript。 PS : I don't wanna use any third party libraries. PS :我不想使用任何第三方库。 I wanna learn to code. 我想学习编码。

Anyway,I have dynamic table which I want to make editable using content-editable=true attribute of HTML. 无论如何,我有要使用HTML的content-editable = true属性使其可编辑的动态表。

Problem : How to I get the edited data? 问题 :如何获取编辑后的数据? whenever I click on submit and pass the this object to the check() function. 每当我单击提交并将此对象传递给check()函数时。 I doesn't contain edited values. 我不包含编辑后的值。 is there a possible way to pass only edited value if it's dirty. 如果脏的话,有没有办法传递仅已编辑的值。 It has pagination so If g to the next page the edited values are gone. 它具有分页功能,因此如果g转到下一页,则编辑的值将消失。 I know I've give unique Id to every td element with $Index concatenated to it. 我知道我为每个td元素赋予了唯一的ID,并将其与$ Index相连。 But I don't know how should I proceed. 但是我不知道该怎么办。

Any help or guidance will be appreciated. 任何帮助或指导将不胜感激。 Controllers and others are defined in my route. 我的路线中定义了控制器和其他控制器。

<div>
<form ng-submit="check(this)">
    <table class="table table-striped table-hover">
        <tbody>
            <tr ng-repeat="data in currentItems">
                <td contenteditable="true >{{data.EmpNo}}</td>
                <td contenteditable="true">{{data.isActive}}</td>
                <td contenteditable="true">{{data.balance}}</td>
                <td contenteditable="true">{{data.age}}</td>
                <td contenteditable="true">{{data.eyeColor}}</td>
                <td contenteditable="true">{{data.fname}}</td>
            </tr>
        </tbody>
        <tfoot>
            <td>
                <div class="pagination pull-right">
                    <li ng-class="{'disabled': previousPage}">
                        <a ng-click="previousPage()" >Previous</a>
                    </li>   
                    <li ng-repeat="page in pageLengthArray track by $index">
                        <a ng-click="pagination($index)">{{$index+1}} </a>
                    </li>
                    <li  disabled="disabled">
                        <a ng-click="nextPage()" ng-class="{'disabled':nextPage}>Next </a>
                    </li>
                </div>
            </td>
        </tfoot>
    </table>
    <input type="submit" value="Submit">
</form>

     $scope.currentPage=0;
     $scope.pageSize=10;
     $scope.currentItems;
     $scope.tableData;
   $http.get('../json/generated.json').then(function(response){
      $scope.tableData=response.data;
      $scope.pageLength=Math.ceil($scope.tableData.length/$scope.pageSize);
           $scope.currentItems=$scope.tableData.slice($scope.currentPage,$scope.pageSize);
      $scope.pageLengthArray= new Array($scope.pageLength);
    });

        $scope.pagination=function(currentPage){   $scope.currentItems=$scope.tableData.slice($scope.pageSize*currentPage,$scope.pageSize*currentPage+$scope.pageSize);
          $scope.currentPage=currentPage;
        }   
        $scope.nextPage=function nextPage(argument) {
              $scope.currentPage++;  $scope.currentItems=$scope.tableData.slice(($scope.pageSize*$scope.currentPage),($scope.pageSize*($scope.currentPage)+$scope.pageSize));
            }
        $scope.previousPage=function previousPage(argument) {
          $scope.currentPage--;
          $scope.currentItems=$scope.tableData.slice(($scope.pageSize*$scope.currentPage),($scope.pageSize*($scope.currentPage)+$scope.pageSize));
        } 

In the usual case, you can not get a change model for contenteditabe because to change the model used ngModel . 在通常情况下,您无法获得contenteditabe的更改模型,因为要更改使用ngModel的模型。

But we can create a directive that we have updated the value of the model. 但是我们可以创建一条指令,以更新模型的值。

Live example on jsfiddle . jsfiddle上的实时示例。

 angular.module('ExampleApp', []) .controller('ExampleController', function($scope, $timeout) { $scope.data = { EmpNo: "123" }; }) .directive('contenteditable', function($timeout) { return { restrict: "A", priority: 1000, scope: { ngModel: "=" }, link: function(scope, element) { element.html(scope.ngModel); element.on('focus blur keyup paste input', function() { scope.ngModel = element.text(); scope.$apply(); return element; }); } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="ExampleApp"> <div ng-controller="ExampleController"> <table> <tr> <td ng-model="data.EmpNo" contenteditable="true"></td> </tr> </table> <pre>{{data|json}}</pre> </div> </div> 

I would store any object that gets modified in a seperate array using the ng-keyup directive. 我会使用ng-keyup指令将要修改的所有对象存储在单独的数组中。 When the form is submitted, you will have an array of only elements which have been modified. 提交表单后,将只有一个已修改元素的数组。 You may have some UX issues if your pagination is done by server as when you change page and come back, it will show your old data, but hopefully this helps. 如果您的分页是由服务器完成的,则可能会遇到一些UX问题,因为当您更改页面并返回时,它将显示您的旧数据,但是希望这会有所帮助。

$scope.check = function () {
    // check modifiedItems
    console.log(modifiedItems);
};

// store modified objects in a seperate array
var modifiedItems = [];

$scope.modifyItem = function (data) {

    // check if data has already been modified and splice it first
    for(var i = 0, j = modifiedItems.length; i < j; i++) {
        var currentItem = modifiedItems[i];

        if (currentItem.id === data.id) {
            modifiedItems.splice(i, 1);
            break;
        }
    }

    // add to modified
    modifiedItems.push(data);
    console.log('modifiedItems: ', modifiedItems);
};

HTML 的HTML

<form ng-submit="check()">
    <table class="table table-striped table-hover">
        <tbody>
            <tr ng-repeat="data in currentItems">
                <td ng-repeat="(key, value) in data" contenteditable="true"
                    ng-keyup="modifyItem(data)">
                    {{data[key]}}
                </td>
            </tr>
        </tbody>
        <tfoot>
    </table>
    <input type="submit" value="Submit">
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM