javascript - AngularJS update database using X-Editable -


so, i'm using angularjs x-editable make easier way edit data.

i have table information of client such name, phone, address, etc.. , apply x-editablejust fine until moment need save edit on database.

also, page show 1 single client, individual client page, details.

this code i'm using:

page.html

<table fixed-header class="detcli_table" ng-init="get_detcliente()">     <thead>         <tr>             <th>campo</th>             <th>informação</th>         </tr>     </thead>     <tbody>         <tr>             <td>código</td>             <td>{{cliente.id}}</td>         </tr>         <tr>             <td>nome</td>             <td><span editable-text="cliente.nm_cliente" onaftersave="updateperson(cliente.nm_cliente)">{{cliente.nm_cliente || "empty"}}</span></td>         </tr>         <tr>             <td>tel.</td>             <td><span editable-text="cliente.num_tel" onaftersave="updateperson(cliente.num_tel)">{{cliente.num_tel || "empty"}}</span></td>         </tr>         [... more code ...]     </tbody> </table> 

app.js

myapp.controller('detclientesctrl', ['$scope', '$http', '$routeparams', function ($scope, $http, $routeparams) {      var clienteid = $routeparams.id;      $scope.get_detcliente = function() {         var url = 'scripts/php/db.php?action=get_cliente';         return $http.get(url).success(httpsuccess).error(function() {             alert("oops, erro!");         });     }     httpsuccess = function(response) {         $scope.detres = response;     }     function getbyid(arr, id) {         (var d = 0, len = arr.length; d < len; d += 1) {             if (arr[d].id === id) {                 return arr[d];             }         }     }     $scope.get_detcliente().then(function(){         $scope.cliente = getbyid($scope.detres,$routeparams.id);     });      //update client     $scope.updateperson = function() {         $http.post('scripts/php/db.php?action=upd_cliente',         {             'id': $routeparams.id,             'nm_cliente' : $scope.nm_cliente,             'num_tel' : $scope.num_tel         }         ).success(function (data, status, headers, config) {             $scope.get_detcliente();             console.log("efeutou o post!");         }).error(function (data, status, headers, config) {             console.log("algo deu errado!");         });     }; }]); 

control.php
this method i'm using add new data, delete and, in case, update existing data

function upd_cliente() {     $data = json_decode(file_get_contents("php://input"));     $id = $data->id;     $nm_cliente = $data->nm_cliente;     $num_tel = $data->num_tel;      $qry = "update cad_cliente set nm_cliente = '$nm_cliente', num_tel = '$num_tel' cd = '$id'";  } 

when run code, no errors @ all. console.log i'm using showing in console, editing do, working fine on screen when refresh page, there no data saved, goes previous data.

what may wrong?

and don't know if best way it, since have table 10 15 lines of information, if edit 1 or 5 lines, code have run each edit make.

is there better way process it?

well, after research , lot of try/fail cam solution.

in page page.html needed remove remove code inside (), going this:

page.html

<td><span editable-text="cliente.nm_cliente" onaftersave="updateperson()">{{cliente.nm_cliente || "empty"}}</span></td> 

and on app.js needed use $scope.cliente.nm_cliente instead of $scope.nm_cliente. code this:

app.js

$scope.updateperson = function() {     $http.post('scripts/php/db.php?action=upd_cliente',         {             'id': $routeparams.id,               'nm_cliente' : $scope.cliente.nm_cliente,             'num_tel' : $scope.cliente.num_tel         }     ).success(function (data, status, headers, config) {         //success code here     }).error(function (data, status, headers, config) {         //error code here     }); }; 

then, on php file need write other fields need update on database, in case, more 15 fields possible update.

note: far know, code works option onaftersave="" because if use option onbeforesave="", name itself, data not passed since it's being executed before new data passed $scope.

i'm sorry if of information wrong, i'm starting learning angularjs right now. working me.

also, don't know if there better way achieve result, so, if knows, please share us!


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -