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

Asked

Viewed 64 times

3

Hello.

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

  • Do you have flag queries Then stay in the Array: clientesDownloadTrue
  • Do you have flag queries Then it stays in the Array: clientesDownloadFalse

The screen is this:

inserir a descrição da imagem aqui

When sending, I want to be able to know exactly which record went to one side or the other?

Follow my javascript:

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 function prepareInsercao that picks up the lists, sends it to my java controller and saves the object there.

In case you need more information, post on time. Thank you.

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 on a row 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>
    <div ng-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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.