【问题标题】:Update data in ng-repeat using Angular js使用 Angular js 更新 ng-repeat 中的数据
【发布时间】:2016-12-17 17:06:36
【问题描述】:

我在将数据更新到数据库时遇到问题。例如:在我的表中包含特定 id 的三行。所以在编辑时通过 ng-repeat 显示内容。

查看

  <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 控制器

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

}

型号

    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);
        } 
  }       

如何在提交时将编辑后的数据更新到数据库。

【问题讨论】:

  • 您可能需要添加更多代码。你在用表格吗?您如何将数据提交到服务器?
  • 这似乎应该在一个表单中,并且需要附加一个提交操作,以便如果您确实想将信息保存在那里,您可以发布到您的数据库
  • 使用 ng-click..then $http.post(base_url+"purchase/tools/updatePurchaseDetails/"+purchase_id,{data:$scope.data,count:$scope.counter})。
  • 你能从chrome开发者工具中看出信息是否正在发送到服务器吗?

标签: javascript angularjs


【解决方案1】:

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 控制器文件:

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 服务文件:

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);
    }
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多