我在 ng-grid 中编辑数据后,图像没有更新



我有一个带有按钮的网格,通过单击该按钮它会打开一个模态窗口,允许用户修改该行中存在的数据,所需的代码是

//Html file
<body ng-controller="MyCtrl">
   <script type="text/ng-template" id="myModalContent.html">
          <div class="modal-header">
            <h3 class="modal-title">I'm a modal!</h3>
          </div>
       <div class="modal-body" >
           <button>Hello</button>
             <form>
             <input data-ng-model="items.name"/>
             <input data-ng-model="items.age"/>
                <input data-ng-model="items.SA"/>
             <button class="primary" data-ng-click="ok()">save</button>
             <button class="primary" data-ng-click="cancel()">cancel</button>
             </form>
        </div>
    </script>
        <div class="gridStyle" ng-grid="gridOptions"></div>
    </body>
//Js file
// main.js
var app = angular.module('myApp', ['ngGrid','ui.bootstrap']);
var cellTemplate='<div class="ngCellText"  data-ng-model="row"><button data-ng-click="updateSelectedRow(row,$event)">Edit</button></div>'
var ctrl=app.controller('MyCtrl', function($scope,$modal) {
    $scope.myData = [{name: "Moroni", age: 50,SA: 2,},
                     {name: "Tiancum", age: 43,SA:1,},
                     {name: "Jacob", age: 27,SA: 2,},
                     {name: "Nephi", age: 29,SA: 4,},
                     {name: "Enos", age: 34,SA: 1,}];
    $scope.gridOptions = { 
      data: 'myData', 
      enableCellSelection: true,
      enableCellEdit: true,
      enableRowSelection: false,
      columnDefs: [{field: 'name', displayName: 'Name', enableCellEdit: true}, 
      {field:'age', displayName:'Age'},
      {field:'SA', displayName:'ServiceAffecting', 
//cellTemplate:'<div><img ng-src="{{row.getProperty('"SA"') | imagefilter}}"></img></div>'
cellTemplate:'<div><img ng-src="{{row.getProperty('SA') | imagefilter}}"></img></div>'},
      {field:'',cellTemplate:cellTemplate}   
      ]
    };
    var dialog;
    $scope.updateSelectedRow=function(row){
       $scope.myrow=row.entity;
     var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      resolve: {
        items: function () {
          return row.entity;
        }
      }
    });
    }
    $scope.save=function(){
      console.log($modal);
     $modal.dismiss('cancel');
    }
});
var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
  $scope.items = items;
  var name=items.name;
  var age=items.age;
  var SA=items.SA;

  $scope.ok = function () {
   $modalInstance.close();

  };
  $scope.cancel = function () {
    items.name=name;
    items.age=age;
    $modalInstance.dismiss('cancel');
  };
  };
app.filter('imagefilter', function() {
        return function(SA) {
            if(SA===0) { return 'http://goo.gl/aFomAA'; }
            if(SA===1) { return 'http://goo.gl/vxCnLC'; }
            if(SA===2) { return 'http://goo.gl/aFomAA'; }
              if(SA===4) { return 'http://goo.gl/aFomAA'; }
                if(SA===5) { return 'http://goo.gl/aFomAA'; }
            return 'unknown';
        };
});

当我编辑图像的值时,它没有得到更新,不明白为什么?要么发生单击该按钮后图像应自动变为红色的情况,任何解决方案都将不胜感激。谢谢。

模态将items.SA转换为字符串,因此imagefilter中的任何分支都不再匹配,因为SA仅与数值进行比较。

两个解决方案浮现在脑海中:

使用数字输入框:<input type="number" data-ng-model="items.SA"/>

胁迫SA imagefilter中的数字:

app.filter('imagefilter', function () {
    return function (SA) {
        SA = +SA; //coerce SA into a number
        if (SA === 0) {
        ....

最新更新