How to search for words in a JSON using Ionic?

Asked

Viewed 121 times

0

I have a JSON and it’s paginated. When it was not paged, when typing anything in the search, it looked for what was printed on the screen, as in the following example:

<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>

  <div class="card" ng-repeat="item in ofertass | filter:q | orderBy:someModel | unique: 'cadastra_oferta_cod_oferta'" ng-init="$last ? fireEvent() : null" href="#/nhaac/ofertas_singles/{{item.cadastra_oferta_cod_oferta}}">

Now that I have pagined, it continues searching only what is printed, already loaded in the pagination.

How can I perform a search for my JSON without the need to load all records on the screen?

Follow the section of my controller with the pagination:

$scope.pagination = {
        limit: 8,
        offset: 0
    };
    $scope.pagination.limit = 8;
    $scope.pagination.offset = 0;
    $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).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.noMoreItemsAvailable = false;
        $scope.getRecord();
    };



    if (data_ofertass === null) {
        data_ofertass = [];



        window.localStorage.getItem("endereco_temp");
        window.localStorage.getItem("endereco_atual");
    };
  • 1

    Dude, I don’t know if you’ve solved it yet, I got to comment there on the face, but I blacked out because I don’t know angular/Ionic so deeply to know if there’s any magic... if there’s no magic, the default solution is to pass the data from the search to your backend so that it already generates the filtered json for vc.

1 answer

0


I solved using POG!

Follows the resolution:

$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.