How to identify exactly which record has changed from one list to another? By ID

3

Hello.

I have 2 lists being assembled in javascript, based on the rule:

  • Do you have flag Automatically DispatchedNotes as true in the database? Then it stays in the Array: clientsDownloadTrue
  • Do you have flag queryNotesAddressed Automatically as false in the database? Then it stays in the Array: clientsDownloadFalse

The screen is this:

Whensending,doIwanttoknowexactlywhichrecordwenttoonesideortheother?

Followmyjavascript:

app.controller("ConfiguraDownloadAutomatico",

    function($scope, $http,$filter,dialogMessage) {

        $scope.clientes = [];
        $scope.clientesDownloadFalse= [];
        $scope.clientesDownloadTrue =[];

        // picklist
        $scope.selectedA = [];
        $scope.selectedB = [];
        $scope.items = [];
        $scope.checkedA = false;
        $scope.checkedB = false;

        /**
         * Obter sessão
         */
        $scope.loadSession = function() {

            $http({
                method : 'GET',
                url : 'session'
            }).then(function successCallback(response) {

                $scope.idUsuario = response.data.result.id_usuario;
                $scope.iniciar();   

            }, function errorCallback(response) {
                console.log(response.data);

            });

        };
        $scope.loadSession();

        $scope.iniciar = function() {
            $http.post('obterEmpresasDownload', $scope.idUsuario).success(function(response) {

                $scope.clientes = response;
                $scope.items = response;
                angular.forEach($scope.clientes, function(value, key){
                    if(value.consultaNotasDestinadasAutomaticamente == true){
                        $scope.clientesDownloadTrue.push(value);
                    }else{
                        $scope.clientesDownloadFalse.push(value);
                    }
                  });                 
            }); 
        };



        $scope.preparaInsercao = function(clientesDownloadFalse, clientesDownloadTrue) {

            var dados = {
                    clientesDownloadFalse : $scope.clientesDownloadFalse,
                    clientesDownloadTrue : $scope.clientesDownloadTrue
            };

            $http.post(
                    'configuradownload/salvarEmpresasDownloadAutomatico',
                    dados).then(
                    function(response) {
                        if (response.data.codReturn == 0) {
                            dialogMessage("Configura Download Automático",
                                    response.data.descReturn, "success");
                        } else {
                            dialogMessage("Configura Download Automático",
                                    response.data.descReturn, "erro");
                        }

                    }, function(response) {

                    });

        };  

            function arrayObjectIndexOf(myArray, searchTerm, property) {
                for(var i = 0, len = myArray.length; i < len; i++) {
                    if (myArray[i][property] === searchTerm) return i;
                }
                return -1;
            }

            $scope.aToB = function() {
              for (i in $scope.selectedA) {
                var moveId = arrayObjectIndexOf($scope.items, $scope.selectedA[i], "idCliente"); 
                $scope.clientesDownloadTrue.push($scope.items[moveId]);
                var delId = arrayObjectIndexOf($scope.clientesDownloadFalse, $scope.selectedA[i], "idCliente"); 
                $scope.clientesDownloadFalse.splice(delId,1);
              }
              reset();
            };

            $scope.bToA = function() {
              for (i in $scope.selectedB) {
                var moveId = arrayObjectIndexOf($scope.items, $scope.selectedB[i], "idCliente"); 
                $scope.clientesDownloadFalse.push($scope.items[moveId]);
                var delId = arrayObjectIndexOf($scope.clientesDownloadTrue, $scope.selectedB[i], "idCliente"); 
                $scope.clientesDownloadTrue.splice(delId,1);
              }
              reset();
            };

            function reset(){
              $scope.selectedA=[];
              $scope.selectedB=[];
              $scope.toggle=0;
            }

            $scope.toggleA = function() {

              if ($scope.selectedA.length>0) {
                $scope.selectedA=[];
              }
              else {
                for (i in $scope.clientesDownloadFalse) {
                  $scope.selectedA.push($scope.clientesDownloadFalse[i].idCliente);
                }
              }
            }

            $scope.toggleB = function() {

              if ($scope.selectedB.length>0) {
                $scope.selectedB=[];
              }
              else {
                for (i in $scope.clientesDownloadTrue) {
                  $scope.selectedB.push($scope.clientesDownloadTrue[i].idCliente);
                }
              }
            }

            $scope.selectA = function(i) {
              $scope.selectedA.push(i);
            };

            $scope.selectB = function(i) {
                $scope.selectedB.push(i);
            };


    });

When saving, call the prepareInserca function that picks up the lists, send it to my java controller and there save the object.

If you need more information, please put it on time. Thank you.

    
asked by anonymous 25.02.2016 / 14:10

1 answer

2

Your model can be simplified. The functional example below has only one collection, $scope.dados , whose items have the boolean property consultaNotasDestinadasAutomaticamente .

In the view, tables that use ng-repeat with filter separate the two types. Click one line to have the property reversed, and the item passed to the other list:

var app = angular.module('sampleApp', []);

app.controller('SampleController', function ($scope) {

  $scope.dados = [
    {
      "nome": "item1",
      "consultaNotasDestinadasAutomaticamente": false
    },
    {
      "nome": "item2",
      "consultaNotasDestinadasAutomaticamente": false
    },
    {
      "nome": "item3",
      "consultaNotasDestinadasAutomaticamente": false
    },
    {
      "nome": "item4",
      "consultaNotasDestinadasAutomaticamente": true
    },
    {
      "nome": "item5",
      "consultaNotasDestinadasAutomaticamente": true
    },
    {
      "nome": "item6",
      "consultaNotasDestinadasAutomaticamente": true
    },
  ]
});
<html ng-app='sampleApp'>


  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script></head><body><divng-controller="SampleController">
      <table>
        <tr>
          <td>
            <table>
              <tr><th>False</th></tr>
              <tr ng-repeat='i in dados | filter: { consultaNotasDestinadasAutomaticamente : false }'>
                <td ng-click='i.consultaNotasDestinadasAutomaticamente = !i.consultaNotasDestinadasAutomaticamente'>{{i.nome}}</td>
              </tr>
            </table>
          </td>
          <td>
            <table>
              <tr><th>True</th></tr>
              <tr ng-repeat='i in dados | filter: { consultaNotasDestinadasAutomaticamente : true }'>
                <td ng-click='i.consultaNotasDestinadasAutomaticamente = !i.consultaNotasDestinadasAutomaticamente'>{{i.nome}}</td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>
    
25.02.2016 / 14:37