0

I have this URL working with my site using wp-api -

http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=midlands

which outputs this code (using a chrome plugin to display it neatly) -

enter image description here

I'm really new to this and would appreciate if someone could point me in the right direction of how to use AngularJS (This is what I've read is best to use) to display this data on a useable page.

This is the code I have so far pulling in the data -

<script type="text/javascript">
        function goToNewPage()
        {
            var url = document.getElementById('list').value;
            if(url != 'none') {
                window.location = url;
            }
        }
    </script>

    <select name="list" id="list" accesskey="target">
        <option value='none' selected>Choose a region</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=northwest">North West</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=northeast">North East</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=midlands">Midlands</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=eastanglia">East Anglia</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=southeast">South East</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=southwest">South West</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=scotland">Scotland</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=wales">Wales</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=northernireland">Northern Ireland</option>
        <option value="http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=channel">Channel Islands</option>






    <select>

    <input type=button value="Go" onclick="goToNewPage()" /> 

The link - http://scd.blaze.wpengine.com/test/

Thanks for any help in advance!

Lucy Brown
  • 264
  • 4
  • 17
  • 1
    Can you show us what code have you written already? if not here is a good starting point: https://docs.angularjs.org/tutorial – Jax Dec 11 '15 at 12:06
  • All I have so far is just a dropdown linking to the different term data if that makes sense, I have nothing else working at the moment, I'm just looking for a way to style/format these results pulled from the URL – Lucy Brown Dec 11 '15 at 12:21
  • I would share the code here for people to take a look. – Jax Dec 11 '15 at 12:22
  • I will do, thanks for your help – Lucy Brown Dec 11 '15 at 12:24

1 Answers1

1

Ok there is not much Angular in your code but here is a starting point:

View:

<div ng-app="myApp" ng-controller="regionsCtrl">
  <label>Select a region:</label>
  <select ng-options="region for region in regions" ng-model="region" ng-change="getRegionData()">
  </select>
  <table>
    <tr>
      <th>id</th>
      <th>Title</th>
      <th>Status</th>
      <th>Type</th>
    </tr>
    <tr ng-repeat="d in data">
      <td>{{d.ID}}</td>
      <td>{{d.title}}</td>
      <td>{{d.status}}</td>
      <td>{{d.type}}</td>
    </tr>
  </table>
</div>

Controller:

var app = angular.module('myApp', []);
app.controller('regionsCtrl', function($scope, $http) {
  $scope.regions = [
    'North West', 'North East', 'Midlands', 'East Anglia', 'South East', 'South West', 'Scotalnd', 'Wales'
  ]

  $scope.getRegionData = function() {
    var region = $scope.region
    $http.get("http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=#{region}").then(function(data) {
      $scope.data = data; //this is the data that server returns
    })
  }
});

take a look at this fiddle

What this does is makes an call to the server using the region from the select menu, then stores the array returned from server in a variable on the scope (data in this case), then you can iterate over it in your view using ng-repeat. Bear in mind that I can't access the server due to cors but if you have the right credentials it should work.

Jax
  • 1,839
  • 3
  • 18
  • 30
  • I think I've confused you with what i needed help with, I'm not very good at explaining as I don't understand it too well! The links that are in the dropdown - where the urls lead to and I get all the code showing the data of my post type term, how do I display that as actual content using Angular? Do I reference that link somewhere and then create the content there? I hope I'm making sense! – Lucy Brown Dec 11 '15 at 13:09
  • 1
    ok, take a look at the fiddle now, is this what you're looking for? – Jax Dec 11 '15 at 13:43
  • Yes, definitely closer, I just need a way to display the data that URL displays, so yes this does that. Is there a way i can just reference the URL in the javascript so it can pull the data from there and not the whole code you've copy and pasted in there? Also I had a play with it and I got it to output the slug with the method you used, but I couldn't get it to output the excerpt of the post, how can I do this? Thanks so much for your help so far – Lucy Brown Dec 11 '15 at 13:52
  • I'm being stupid! There is no excerpt, ignore that last bit – Lucy Brown Dec 11 '15 at 13:55
  • Is it definitely updated? It's not doing anything for me, sorry! – Lucy Brown Dec 11 '15 at 14:56
  • Read the comment: "Bear in mind that I can't access the server due to cors but if you have the right credentials it should work." – Jax Dec 11 '15 at 14:57
  • 1
    Ah, sorry it didn't show me a comment. Thats definitely doing something, it reveals a table when I click on a region but no results, I imagine as you say something my end isn't correct, I will try and see if I can work out where I'm going wrong. Thanks so much for all your help so far, I really appreciate you taking the time! – Lucy Brown Dec 11 '15 at 15:03
  • I've changed the values to the slugs of my terms but it's still not working which is the only thing I could see that would be wrong, sorry to bug you, could you have a little look at this link and see if you can tell if I've dne something ridiculous? http://scd.blaze.wpengine.com/test/ – Lucy Brown Dec 11 '15 at 15:25