How do I get splice() into http.get using Ionic / Angularjs for searching?

Asked

Viewed 115 times

0

I want to filter according to what the user type on the screen. I have a form with the ng-model="q" and the | filter:q

Where my http.get pass the parameters I am filtering, where ng-model is the "fetch parameter:

$scope.pagination = {
        limit: 10,
        offset: 0
    };
    $scope.pagination.limit = 8;
    $scope.pagination.offset = 0;
    $scope.q = '';
    $scope.noMoreItemsAvailable = false;
    $scope.ofertass = [];
    $scope.getRecord = function () {
        $http.get("http://meusite.com.br/admin/apis/api_listagem/lista_oferta_api.php?json=promocao" + "&latitude=" + $scope.lat_cliente + "&longitude=" + $scope.long_cliente + "&raio=" + $scope.raio + "&limit=" + $scope.pagination.limit + "&offset=" + $scope.pagination.offset + "&busca=" + $scope.q).then(function (response) {
                // console.log(response.data);
                if (response.data.length) {
                    $scope.ofertass.push.apply($scope.ofertass, response.data);
                    $scope.pagination.offset = $scope.pagination.offset + $scope.pagination.limit;
                    window.localStorage.setItem("data_ofertass", JSON.stringify(response.data));

                } else {
                    $scope.noMoreItemsAvailable = true;
                    if (window.localStorage.getItem("data_ofertass") === null) {
                        $state.go("nhaac.nachegamos");
                    }

                }
            },
            function (response) {
                // error message
                var alertPopup = $ionicPopup.alert({
                    title: "error " + response.status,
                    template: response.statusText + "<br/>Problema: Conexão com sua Internet.",
                });
            }).finally(function () {
            console.log("finally....");
            $scope.$broadcast('scroll.infiniteScrollComplete');

        });
    }

    $scope.doRefresh = function () {
        limit = 20;
        offset = 0;
        $scope.q = '';
        $scope.noMoreItemsAvailable = false;
        $scope.getRecord();
    };

Works well, but when I empty the search field returns blank results with the last filter below.

Follow my search on View:

<center>
        <form ng-submit="fechaTeclado()">
            <div class="bar bar-subheader bar-light">
                <label class="item item-input item-floating-label">
                    <i class="icon ion-search placeholder-icon"></i>
                    <input type="text" size="100" ng-model="q" placeholder="Procurar" type="submit" ng-submit="fechaTeclado()" ng-click="fechaTeclado()" />
                    <input type="submit" id="submit" value="OK" ng-click="fechaTeclado()" />

                </label>
            </div>
        </form>
    </center>

What am I doing wrong? I test the URL with the parameters and it works well...

If I leave "&search=" empty lists everything. That’s correct. But in my application, you’re not clearing the search.

EDITED:

Has the function Closekeyboard() which is only to close the device keyboard:

$scope.fechaTeclado = function (q) {
    $cordovaKeyboard.close();
    $scope.q = q;        
    $scope.getRecord();
};

I’m thinking of using a Slice() or splice() in getRecord to eliminate what shouldn’t seem:

inserir a descrição da imagem aqui

Note in the image above, that there are blank spaces that were filled before the filter, and if dragging shows what was searched.

But I’m on a good logic of how to do this.

  • 1

    Dude, I couldn’t quite figure out what was wrong. What was your question? This line did not understand -> It works well, but when I empty the search field returns me blank results with the last filter below.

  • I’ll capture the screens and edit the question.

  • How many points do I have to have to reward the question?

1 answer

0


I settled with POG!

Follows:

$scope.fechaTeclado = function () {
            $cordovaKeyboard.close();

            if ($scope.q === '') {
                $scope.q = '';
                window.localStorage.setItem("q", $scope.q);
                $scope.pagination = {
                    limit: 10,
                    offset: 0
                };
                $scope.pagination.limit = 8;
                $scope.pagination.offset = 0;
                $scope.noMoreItemsAvailable = false;
                $scope.ofertass = [];

                $scope.getRecord();

            } else {



                window.localStorage.setItem("q", $scope.q);

             //   $scope.q = window.localStorage.getItem("q");

                $scope.pagination = {
                    limit: 10,
                    offset: 0
                };
                $scope.pagination.limit = 8;
                $scope.pagination.offset = 0;
                $scope.noMoreItemsAvailable = false;
                $scope.ofertass = [];

                $scope.getRecord();
            }
        };

Browser other questions tagged

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