Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing JSON search results from within Angular

I am now pulling data from a external JSON URL properly to the master page, but my detail page doesn't seem to pass on the object initallt received from http.get. This master part of the app can be viewed in a Code Pen at CODEPEN

<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" ng-model="query" placeholder="Search Slugname">
<button class="button button-dark" ng-click="getOrders()">Submit</button>
</label>

If my user wanted to change the date(order.date)value manually to say "10/8/16". How can I access/edit any of the JSON values that are returned from the external API?

I eventually wish to edit the returned JSON data within my app and then post that revised data back to a PHP API server.

like image 677
me9867 Avatar asked Aug 10 '16 08:08

me9867


1 Answers

Your main issue is that you want to modify the incoming data from the $http call.

You can implement an http interceptor, the method response will take the response modify it and then return it to the caller. Since the http interceptor will take all the incoming requests just check the url start to not modify other requests.

angular.module('ionicApp', ['ionic'])
.factory('httpInterceptor', function($q, $rootScope) {
    var httpInterceptor = {
        response: function(response) {
            var deferred = $q.defer();
            var results = response.data;
            var urlStart = 'http://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=';
            if (response.config.url.startsWith(urlStart)) {
                angular.forEach(results, function(result, key) { 
                    result.date = '10/8/16'; 
                });
            }
            deferred.resolve(response);
            return deferred.promise;
        }
    };
    return httpInterceptor;
})
.config(function($httpProvider) { 
    $httpProvider.interceptors.push('httpInterceptor'); 
})
.controller('ListController', 
    ['$scope', '$http', '$state', '$stateParams', '$window', '$location', 
    function($scope, $http, $state, $stateParams, $window, $location) {
        $scope.getOrders = function(query) {
            $http.get('http://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=' + query)
            .success(function(data) {
                $scope.orders = data;
            })
        }
        $scope.orders = [];
}]);

The only modification that I've made on your html is to send query param directly to the function on the ng-click

<html ng-app="ionicApp">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title>Tabs Example</title>
    <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
</head>
<body ng-controller="ListController">
    <ion-header-bar class="bar-dark" align-title="center">
        <h2 class="title">Order Search</h2>
    </ion-header-bar>
    <ion-content padding="true" class="has-header">
        <div class="item item-input">
            <i class="icon ion-search placeholder-icon"></i>
            <input type="text" ng-model="query" placeholder="Search Slugname">
            <button type="submit" class="button button-dark" ng-click="getOrders(query)">Submit</button>
        </div>
        <p>An example and working slug search term is test-3. The JSON can be viewable in a browser using https://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=test-3</p>
        <ion-list>
            <ion-item ng-repeat="order in orders | filter:query" class="item-thumbnail-left item-text-wrap" href="#/tab/list/{{order.id}}">
                <h2>Page ID: {{order.id}}</h2>
                <h3>Date created: {{order.date}}</h3>
                <h2>Page URL: £{{order.link}}</h2>
                <h2>Page Title: £{{order.title/rendered}}</h2>
            </ion-item>
        </ion-list>
    </ion-content>
</body>
</html>

I almost forgot the codepen is here: http://codepen.io/pachon1992/pen/QEodJR

EDIT ---------------------------

As per comments you want your user to update manually the data right? as an example you would like to update the date, you can do enabling a input for the user to edit the data, since angular is two-way data-binding will modify your data.

<html ng-app="ionicApp">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title>Tabs Example</title>
    <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
</head>
<body ng-controller="ListController">
    <ion-header-bar class="bar-dark" align-title="center">
        <h2 class="title">Order Search</h2>
    </ion-header-bar>
    <ion-content padding="true" class="has-header">
        <div class="item item-input">
            <i class="icon ion-search placeholder-icon"></i>
            <input type="text" ng-model="query" placeholder="Search Slugname">
            <button type="submit" class="button button-dark" ng-click="getOrders(query)">Submit</button>
        </div>
        <p>An example and working slug search term is test-3. The JSON can be viewable in a browser using https://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=test-3</p>
        <ion-list>
            <ion-item ng-repeat="order in orders | filter:query" class="item-thumbnail-left item-text-wrap" href="#/tab/list/{{order.id}}">
                <h2>Page ID: {{order.id}}</h2>
                <div><input type="text" ng-model="order.date"></div>
                <h2>Page URL: £{{order.link}}</h2>
                <h2>Page Title: £{{order.title/rendered}}</h2>
            </ion-item>
            <button type="button" class="button button-dark" ng-click="update()">Update</button>
        </ion-list>
    </ion-content>
</body>
</html>

In your controller you can call http service for every order calling via typically to a PUT endpoint

angular.module('ionicApp', ['ionic'])

.controller('ListController', ['$scope', '$http', '$state', '$stateParams', '$window', '$location',
    function($scope, $http, $state, $stateParams, $window, $location) {
        $scope.query = "";
        $scope.getOrders = function(query) {
            $http.get('http://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=' + query)
                .success(function(data) {
                    $scope.orders = data;
                });
        }
        $scope.orders = [];
        $scope.update = function() {
            angular.forEach($scope.orders, function(order) {
                //whetever url you are using
                $http.put('http://mvcframe.com/wpapi/wp-json/wp/v2/pages/update/' + order.id, order, {})
                    .success(function(data) {
                        console.log('updated');
                    });
            });
        };
    }
]);

I've edited the codepen

like image 193
pachonjcl Avatar answered Oct 20 '22 10:10

pachonjcl