2015-02-09 60 views
3

我对这个大多数人来说都是非常新的,所以我的歉意是,我的代码大部分都是从这个论坛借用和欺骗的,我正在学习。AngularJS中的可编辑表格单元格

目前我正试图复制一个加热器计时系统,每天有6个开关可以改变温度。

我已经通过我的控制器加载了一些示例数据,并使用ng-repeat在表格中显示,但它并不理想,但暂时工作并将按我的要求做。

我的数据显示是静态的,没有格式或排序,只能编辑。

我现在卡在下一个阶段,表格中的每个单元格都是可编辑的。我已经阅读了大量的帖子和信息(主要在行和使用ng-grid和其他插件),我希望有人可以指出我在正确的方向请如何继续我的当前代码如何id每个单击并点击弹出一个模式,这将允许输入组成每个单元格,小时,分钟和温度的三个元素。

请不要期望自定义代码或高级课程,我知道我的工作是非常基础的,但是如果有人能指向我正确的方向或一些有用的链接,将不胜感激。

我第一次在这里发帖,希望我提供的信息足够清楚。

p.s.我现在要睡觉(英国时间晚上11点30分),今天上午7点以后(大部分周末)我都要睡觉,因此我需要让自己远离屏幕。

function rowController($scope) { 
 
    $scope.sw1 = [{ 
 
     hours: '01', 
 
     minutes: '05', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '05', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '05', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '05', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '05', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '05', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '05', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.sw2 = [{ 
 
     hours: '01', 
 
     minutes: '10', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '10', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '10', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '10', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '10', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '10', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '10', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.sw3 = [{ 
 
     hours: '01', 
 
     minutes: '15', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '15', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '15', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '15', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '15', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '15', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '15', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.sw4 = [{ 
 
     hours: '01', 
 
     minutes: '20', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '20', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '20', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '20', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '20', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '20', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '20', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.sw5 = [{ 
 
     hours: '01', 
 
     minutes: '25', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '25', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '25', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '25', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '25', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '25', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '25', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.sw6 = [{ 
 
     hours: '01', 
 
     minutes: '30', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '30', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '30', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '30', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '30', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '30', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '30', 
 
     temp: '02' 
 
    }, ]; 
 
}
 th, td { 
 
      text-align: center; 
 
     } 
 
     col { 
 
      min-width:95px; 
 
      max-width:95px; 
 
     } 
 
     .table { 
 
      width:auto; 
 
     }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="container-fluid"> 
 
    <div ng-app="" ng-controller="rowController"> 
 
     <div class="table-responsive col-xs-12"> 
 
      <table class="table table-bordered table-striped"> 
 
       <colgroup> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
       </colgroup> 
 
       <thead> 
 
        <tr class="info"> 
 
         <th>Monday</th> 
 
         <th>Tuesday</th> 
 
         <th>Wednesday</th> 
 
         <th>Thursday</th> 
 
         <th>Friday</th> 
 
         <th>Saturday</th> 
 
         <th>Sunday</th> 
 
        </tr> 
 
       </thead> 
 
       <tbody> 
 
        <tr> 
 
         <td ng-repeat="x in sw1">{{ x.hours + ':' + x.minutes + ' ' + x.temp + '&deg; c' }}</td> 
 
        </tr> 
 
        <tr> 
 
         <td ng-repeat="x in sw2">{{ x.hours + ':' + x.minutes + ' ' + x.temp + '&deg; c' }}</td> 
 
        </tr> 
 
        <tr> 
 
         <td ng-repeat="x in sw3">{{ x.hours + ':' + x.minutes + ' ' + x.temp + '&deg; c' }}</td> 
 
        </tr> 
 
        <tr> 
 
         <td ng-repeat="x in sw4">{{ x.hours + ':' + x.minutes + ' ' + x.temp + '&deg; c' }}</td> 
 
        </tr> 
 
        <tr> 
 
         <td ng-repeat="x in sw5">{{ x.hours + ':' + x.minutes + ' ' + x.temp + '&deg; c' }}</td> 
 
        </tr> 
 
        <tr> 
 
         <td ng-repeat="x in sw6">{{ x.hours + ':' + x.minutes + ' ' + x.temp + '&deg; c' }}</td> 
 
        </tr> 
 
       </tbody> 
 
      </table> 
 
     </div> 
 
    </div> 
 
</div> 
 
<script src="rowController.js"></script>

下面就来​​

+0

而不是仅仅渲染数据,使用''标签的内' NG-repeat's。然后,如果你想要适当的时间输入,你可以使用一些timepicker库:http://mgcrea.github。io/angular-strap/##时间选择器 – floribon 2015-02-09 23:34:20

+0

谢谢floribon对于时间选择器的快速反应和建议,我为我的代码玩过时间选取器,并遇到一些问题,所以想出了一个非常基本的列表选择器,我希望呼叫。如果我在线上使用输入标签,我是否需要获取单元ID来调用我的数据(道歉我的措辞是如此基本)。我已经看到了很多修改行的例子,但是没有一个在单元级别工作,也没有我通过数据获取的方式,让自己迷失了方向。 – 2015-02-09 23:44:51

+0

对不起,我不明白你的意思是“我需要获取手机号码才能拨打我的数据”。但是你可能不需要,所有的数据都来自ng-repeat,如果你想处理用户的改变,你可以添加'ng-change =“onDataChange($ index,model)”'来知道你编辑了哪个单元例如。 – floribon 2015-02-09 23:56:43

回答

3

我建议把它处理每一个小区的指令。如果您将此指令作为隔离范围,则不必担心管理中央控制器中的所有数据。

喜欢的东西:

var tempEx = angular.module('TempEx', []); 
 

 
tempEx.directive('tempCell', function() { 
 
    return { 
 
     restrict: 'A', 
 
     scope: { 
 
      temp: '=tempCell' 
 
     }, 
 
     template: '\ 
 
<input type="text" ng-model="temp.temp" ng-show="editMode()" /> \ 
 
<input type="text" ng-model="temp.hours" ng-show="editMode()" /> \ 
 
<input type="text" ng-model="temp.minutes" ng-show="editMode()" /> \ 
 
<div ng-show="editMode()"> \ 
 
    <button ng-click="save()">Save</button> \ 
 
    <button ng-click="cancel()">Cancel</button> \ 
 
</div> \ 
 
<span ng-show="!editMode()"> \ 
 
    {{ temp.hours }} : {{ temp.minutes }} - {{ temp.temp }} \ 
 
</span>', 
 
     link: function ($scope, $element) { 
 
      $element.on('click', function ($event) { 
 
       if ($scope.editMode()) return; 
 
       $scope.enableEdit(); 
 
      }); 
 
     }, 
 
     controller: function ($scope, $timeout) { 
 

 
      var toggling = false; 
 

 
      $scope.meta = { 
 
       mode: 'view' 
 
      }; 
 

 
      $scope.enableEdit = function() { 
 
       if (toggling) return; 
 
       console.log('aaaaaand edit'); 
 
       $scope.meta.mode = 'edit'; 
 
       $timeout(function() { 
 
        $scope.$apply() 
 
       }); 
 
      } 
 

 
      $scope.editMode = function() { 
 
       return $scope.meta.mode === 'edit'; 
 
      }; 
 

 
      $scope.save = function() { 
 
       // do stuff here 
 
      }; 
 

 
      $scope.cancel = function() { 
 
       toggling = true; 
 
       $timeout(function() { 
 
        $scope.meta.mode = 'view'; 
 
        toggling = false; 
 
       }, 250); 
 
      }; 
 
     } 
 

 
    } 
 
}) 
 

 
    .controller('RowCtrl', function ($scope) { 
 

 
    $scope.temps = {}; 
 

 
    $scope.temps.sw1 = [{ 
 
     hours: '01', 
 
     minutes: '05', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '05', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '05', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '05', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '05', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '05', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '05', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.temps.sw2 = [{ 
 
     hours: '01', 
 
     minutes: '10', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '10', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '10', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '10', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '10', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '10', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '10', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.temps.sw3 = [{ 
 
     hours: '01', 
 
     minutes: '15', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '15', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '15', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '15', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '15', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '15', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '15', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.temps.sw4 = [{ 
 
     hours: '01', 
 
     minutes: '20', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '20', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '20', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '20', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '20', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '20', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '20', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.temps.sw5 = [{ 
 
     hours: '01', 
 
     minutes: '25', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '25', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '25', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '25', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '25', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '25', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '25', 
 
     temp: '02' 
 
    }, ]; 
 
    $scope.temps.sw6 = [{ 
 
     hours: '01', 
 
     minutes: '30', 
 
     temp: '32' 
 
    }, { 
 
     hours: '02', 
 
     minutes: '30', 
 
     temp: '20' 
 
    }, { 
 
     hours: '03', 
 
     minutes: '30', 
 
     temp: '13' 
 
    }, { 
 
     hours: '04', 
 
     minutes: '30', 
 
     temp: '23' 
 
    }, { 
 
     hours: '05', 
 
     minutes: '30', 
 
     temp: '12' 
 
    }, { 
 
     hours: '06', 
 
     minutes: '30', 
 
     temp: '02' 
 
    }, { 
 
     hours: '07', 
 
     minutes: '30', 
 
     temp: '02' 
 
    }, ]; 
 

 
    $scope.rows = ['sw1', 'sw2', 'sw3', 'sw4', 'sw5', 'sw6']; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script> 
 
<div class="container-fluid" ng-app="TempEx"> 
 
    <div> 
 
     <div class="table-responsive col-xs-12" ng-controller="RowCtrl"> 
 
      <table class="table table-bordered table-striped"> 
 
       <colgroup> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
        <col span="1" /> 
 
       </colgroup> 
 
       <thead> 
 
        <tr class="info"> 
 
         <th>Monday</th> 
 
         <th>Tuesday</th> 
 
         <th>Wednesday</th> 
 
         <th>Thursday</th> 
 
         <th>Friday</th> 
 
         <th>Saturday</th> 
 
         <th>Sunday</th> 
 
        </tr> 
 
       </thead> 
 
       <tbody> 
 
        <tr ng-repeat="key in rows" ng-init="curTemps = temps[key]"> 
 
         <td ng-repeat="temp in curTemps" temp-cell="temp"></td> 
 
        </tr> 
 
       </tbody> 
 
      </table> 
 
     </div> 
 
    </div> 
 
</div>

+0

非常感谢您的解决方案colefner,我对延迟回复表示歉意。该代码片段工作出色,我现在试图实现上面restassured概述的模式,因为我弹出数据输入以保持视图更清晰的移动设备。非常感谢你的时间和理解,非常感谢。 – 2015-02-21 11:25:57

1

一个链路上的每个要编辑,添加NG点击= “onTdClick(X)”。

<td ng-click="onTdClick(x)"> 

然后,在你的控制器:

$scope.onTdClick = function(x) { 
    var modalInstance = $modal.open({ 
    templateUrl: 'your-modal-content.html', 
    controller: 'YourModalCtrl', 
    resolve: { 
    editingItem: function() { 
     return x; 
    } 
    } 
}); 

modalInstance.result.then(function (editingItem) { 
    $log.info("Modal closed on success: ", editingItem); 
}, function() { 
    $log.info('Modal dismissed at: ' + new Date()); 
}); 

退房$模式:http://angular-ui.github.io/bootstrap/#/modal

+0

嗨restassured非常感谢你的模式是我想要实现的理想,我目前正在使用您的建议以及Colefner的答案如下。对于延迟回复感到抱歉。 – 2015-02-21 11:27:43

1

有人写这一点,最好的例子小提琴:http://jsfiddle.net/davekr/F7K63/43/

<!DOCTYPE html> 
<div ng-app ng-controller="myCtrl" class="container">Double-click on the items below to edit: 
    <button type="button" ng-click="newItem()">Add item</button> 
     <table> 
     <tr ng-repeat="item in items"> 
      <td> 
       <span ng-hide="item.editing" ng-dblclick="editItem(item)">{{item.name}}</span> 
       <input ng-show="item.editing" ng-model="item.name" ng-blur="doneEditing(item)" autofocus /> 
      </td> 
     </tr> 
     </table> 
</div>