简体   繁体   中英

Update data in ng-repeat using Angular js

I have a problem in updating data to database. For example: in my table contain three rows for a particular id. So while edit am displaying contents through ng-repeat.

view

  <tr class="odd gradeX" ng-repeat="d in data">
       <td> <input type="text" name="tools" class="form-control" ng-model="d.po_tools" placeholder="Tools"> </td>

       <td> <input type="text" name="qnty" class="form-control" ng-model="d.po_quantity" placeholder="Quantity"> </td>
   </tr>

CI controller

 public function updatePurchaseDetails()
{
     $po_id = $this->uri->segment(4); 
     $data = file_get_contents('php://input');  
      $this->model->update_purchase_data($data,$data['count']);

}

model

    public function update_purchase_data($data,$count)
    {
       $count=$count+1; 
         for($i=0;$i<$count;$i++) 
        {
            $data_array = array(

            'po_id' => $id,
            'po_tools' => $data['data']['po_tools'] ,
            'po_quantity' =>$data['data']['po_quantity']   
            );
            $this->db->update('purchase_order_tool', $data_array);     
            $this->db->where('po_id',$purchase_id);
        } 
  }       

How to update the edited data to db on submit.

HTML:

   <td> <input type="text" name="tools" class="form-control" ng-model="d.po_tools"  ng-keyup="submit('tools');" value="{{d.po_tools}}" placeholder="Tools"></td>

   <td> <input type="text" name="qnty" class="form-control" ng-model="d.po_quantity" ng-keyup="submit('quantity');" value="{{d.po_quantity}}" placeholder="Quantity"> </td>

JS Controller file :

angular.module('appname').controller('myCtrl',function($scope,Service){

  if(!$scope.d) $scope.d = {};

  $scope.submit = function (type){
    var obj = {
      data : $scope.d,
      type : type
    }
    service.send(obj).then(function(success){
      console.log(success);
    },function(err){
      console.log(err);
    }).finally(function(){
      console.log('finish');
    });
  }

});

JS Service file :

angular.module('appname').factory('Service',$http,function(){
  return {
    send : function(obj){
      var params = $.param(obj);
      var options = {
            url : 'http://example.com/upload.php',
            method: 'POST',
            data : params,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          };
      return $http(options);
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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