7

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.

me9867
  • 1,519
  • 4
  • 25
  • 53

4 Answers4

1

https://codepen.io/anon/pen/qNLOAP

So apparently it seems you cannot have a button in a <label> with ng-click. I changed the label to a <div> and it started working.

The search doesn't work at the moment but that should get you going.

deadwards
  • 2,109
  • 1
  • 16
  • 27
  • Thanks Daniel, test-3 now returning data-I have CORS Chrome plugin that may be allowing this. Just need to know how to access/edit the JSON value of "date". Perhaps need to add JSON stringify or parse maybe, not sure... – me9867 Aug 13 '16 at 15:39
1

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

pachonjcl
  • 723
  • 4
  • 11
  • Never seen $q.defer before, does this comply with what this guy says? http://www.codelord.net/2015/09/24/$q-dot-defer-youre-doing-it-wrong/ Would data.order.date = "new value" be another viable alternative? – me9867 Aug 17 '16 at 08:08
  • This is looking really good thanks. Ideally, I want to load the data in first using test-3. Then click on a button below the data (for example) and then change date to current date such as ```var date = new Date(); date.getDate() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getFullYear()).slice(-2);``` – me9867 Aug 19 '16 at 10:08
  • This method is not what the original post is asking for. $http intercepter will automatically update the date field as soon as the data is recieved. The original post is asking to access the data to allow a user to manually update the data (the date field is an example) at a later interaction. – fourfightingfoxes Aug 19 '16 at 13:40
  • The method of passing in the 'query' param explicitly to the getOrders() function is probably what is fixing this for you. In this case it is never setting the query field on your controller $scope, but instead on a child scope and just passing that in to the function. I prefer $parent.query because then you know you can access $scope.query anywhere in your controller. – fourfightingfoxes Aug 19 '16 at 13:47
  • You are right @fourfightingfoxes, I didn't have any comments on my response until now – pachonjcl Aug 19 '16 at 14:53
0

Please resolve listed points.

1> tabs.home state you don't have injected controller and the other thing is also define home.html and

2> all other template separably in templates folder parallel to your controllers.

3> http call to http://mvcframe.com/wpapi/wp-json/wp/v2/pages requires cross origin domain support for that you can use chrome (CORS) plugin.

it will work for me. Please check and let me know if it works.

  • Ignore the tabs, that was just a setting from a Codepen that had Ionic running in it that i forked from. I have CORS plugin running in the browser, please provide a working forked CodePen of the version you have working. – me9867 Aug 13 '16 at 12:19
0

Working Pen: https://codepen.io/jasondecamp/pen/gryxGQ

The two changes I made were: 1) Added $parent to the reference to 'query' in the input field ng-model. I am not that familiar with ionic, but my guess is that ion-content directive adds a child scope so to reference query you need to look to the parent.

<input type="search" ng-model="$parent.query" placeholder="Search Slugname">

2) I changed the http get url to be https because I was getting insecure access warnings.

As a note, you should not need to use any JSON parsing function when the response is pure JSON data, because angular $http service will automatically recognize the data and convert it into a js object.