2

I am trying to display a menu on my page using the values from the WordPress database of another site. This is what I have done so far:

app.js

angular.module('plunker', []).controller('MainCtrl', function($scope) {
 $scope.menus = [
    {

        title: "Home",
        type: "GET",
        url:'#'
    },
    {

        title: "News",
        action: "#",
        menus: [
          {
              title: "Local", 
              type:"GET",
              action: "#"
          },
          {
              title: "Kerala",
              action: "#"
          },
          {
              title: "National",
              action: "#"
          },
          {
              title: "World",
              action: "#"
          }
        ]
    },
    {
        title: "Entertainment",
        action: "#",
        menus: [
        {
            title: "Malayalam",
            action: "#"
        },
        {
            title:"Tamil",
            action:"#"
        },
        {
            title:"Hindi",
            action:"#"
        },
        {
            title:"English",
            action:"#"
        }

      ]
    }
];
});

index.html

<!doctype html>

<html>
  <head>

    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-app="plunker" ng-cloak>
    <div ng-controller="MainCtrl">
     <div id="wrapper1" class="container">
       <div id="nav1">
       <div><span></span> </div>
             <ul>
                <li ng-repeat="menu in menus" class="has-children" ng-click="changeClass(menu)" lastOrFirst="{{menu.lastOrFirst}}" >
                  <a href="{{menu.url}}">{{menu.title}} <span ></span></a>
                  <ul ng-if="menu.menus">
                   <li ng-repeat="submenu in menu.menus" class="has-children">
                      <a href="{{submenu.action}}">{{submenu.title}}</a>
                          <ul ng-if="submenu.menus" ng-class="submenu.class">
                            <li ng-repeat="subsubmenu in submenu.menus">
                              <a href="{{subsubmenu.action}}">{{subsubmenu.title}}</a>
                            </li>
                          </ul>
                    </li>

                  </ul>
                </li>
              </ul>
        </div>
    </div>
    </div>
  </body>
</html>

https://next.plnkr.co/edit/3Ai7xECU1a1EPqkoxqwI?p=preview&preview - my code in plunker.com.

The question in Detail:

I am creating a menu list in my new web page and data are taken from the WordPress database of another site.

so whenever a new menu item is added to this WordPress database it should automatically change in my page too without re-writing the code.

In this above code, the menu items and sub menu items are manually written in the JSON so whenever any new menu item is added to the WordPress database I need to add it in the code too.

so Is there any way to get the new menu items adding to the database to appear in the menu on my new web page dynamically.

Any suggestions are greatly appreciated. Thanks in advance. The new result after I change the code

This is the sample databse table im trying to fetch: Table name: wp_post sample structure of the databse

//UPDATED//

//html//

 <!DOCTYPE html>
<html>
<head>


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> 

<script type="text/javascript" src="<?php echo base_url();?>Assets/js/angular_app.js" ></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js" >
</script>

<script 
type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-route.js"></script>



<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>Assets/css/angularmenu.css" />  


</head>
<body  ng-app="menuApp">

 <div class="container">
 <div id="wrapper1" class="container" ng-controller="menuController">
       <div id="nav1" >
             <ul >
                <li ng-repeat="menu in menus" >
                  <a href="{{menu.url}}" >{{menu.post_title}}</a>
                  <ul ng-controller="subController">
                   <li ng-repeat="submenu in submenus">
                      <a href="{{submenu.action}}">{{submenu.post_title}}</a>
                    </li>
                    </ul>
                    </li>
                    </ul>
                    </div>
                    </div>
                    </div>
                    </body>
                    </html>

//app.js//

var app = angular.module('menuApp', []);

  app.controller("menuController", function($scope,$http)
  {
    var baseUrl = 'http://localhost:8080/samplepage/';
    $http.get(baseUrl+'Home/getmenu').then(function(response)
    {    
    console.log(response);
    $scope.menus = response.data;
 });
});


  app.controller("subController", function($scope,$http)
  {
    var baseUrl = 'http://localhost:8080/samplepage/';
    $http.get(baseUrl+ 'Home/getsubmenu').then(function(response)
    {
        console.log(response);
        $scope.submenus = response.data;
    });
  });

I got the main menu and sub menu through this code dynamically . but but i get drop down for all the main menu. I only want a drop- down for the News.

  • it would be `{{menu.post_title}}` i suppose – Shashank Vivek Feb 02 '19 at 06:19
  • something like http://embed.plnkr.co/e3PFMloCYpwYB24ldSLn/ – Shashank Vivek Feb 02 '19 at 06:24
  • @ShashankVivek i changed that thank you and i will provide my code on plunkr.com – PHP Devloper Feb 02 '19 at 06:56
  • Even the menu title is fetched from the database so it not displaying properly in plunkr. please check above . i have given all the html,angular script and css . using another ng- reapeat i can create the submenu but im confused how can i put those sub menus under the news(check the image of the result). – PHP Devloper Feb 02 '19 at 07:37
  • This would also help me in seeing the response structure of API. Without seeing that I dont want to guess the object structure – Shashank Vivek Feb 02 '19 at 08:57
  • @ShashankVivek ok . i will do it using json . – PHP Devloper Feb 02 '19 at 09:12
  • https://embed.plnkr.co/plunk/BUjnqq6PbJ5fjA9Q - the plunker – PHP Devloper Feb 02 '19 at 10:40
  • And also the screen shot of what I'm trying to display is given above , please check that too ,for get a picture of how the menu should look like . – PHP Devloper Feb 02 '19 at 11:16
  • fixed your plinkr: https://plnkr.co/edit/3Ai7xECU1a1EPqkoxqwI?p=preview . Whats the problem in it ? There is a sub menu inside "News" menu. – Shashank Vivek Feb 03 '19 at 08:13
  • @ShashankVivek The submenu is added manually. $scope.menus = [ { title: "Home", type: "GET", url:'#' }, All the 'title' is given manually, instead I want it dynamically , whenever I add any sub menu or main menu it should automatically changed in my page too. I got the main menu by using ng-repeat , , The problem is if i use ng-repeat , there is no way to create a sub menu .. Is there any way for that . – PHP Devloper Feb 04 '19 at 04:54
  • Or any way to give href to the title - $scope.menus = [ { title: "Home", type: "GET", url:'#' }, – PHP Devloper Feb 04 '19 at 09:13
  • Can anyone give me a suggestion,Any suggestions is greatly apprechiated. – PHP Devloper Feb 05 '19 at 05:26
  • I am unable to understand your problem. Adding new data in json is correctly rendering the menu as I provided in https://plnkr.co/edit/3Ai7xECU1a1EPqkoxqwI?p=preview . Can you explain it better. Your code do not have `{{user.post_title}}` as u mentioned in comment. Please explain better in question and not in comment section. – Shashank Vivek Feb 05 '19 at 05:35
  • Actually I am creating a new web page and data are taken from the wordpress database of another site. I'm creating a menu list from this wordpress database. so whenever a new menu item is added to this wordpress database it should automatically changed in my page too without re-writing the code . In this above code the menu items and sub menu items are manually written so whenever any new menu item is added to the wordpress database i need to add it in the code too .. so Is there anyway to get the new menu items added to the database will also get appear in my new web page dynamically. – PHP Devloper Feb 05 '19 at 07:29
  • I hope u get it what i'm trying to do . If not i will explain it again – PHP Devloper Feb 05 '19 at 08:00

1 Answers1

1

Take a look at app.js where I have used $http to make API call to the server which will provide the response in the json format. You need to call the server using $http. I hope it helps.

Here is the plunkr where I am using json to fetch the data.replace it with the server call

angular.module('plunker', []).controller('MainCtrl', function($scope,$http) {
//  $http.get('/your_wordpress_API_to_provide_menu_data/',function(response){
 $http.get('data.json').then(function(response){    
    console.log(response)
    $scope.menus = response.data;
 })
});

update:

Check out this plunkr where sub menu is activated for news only

Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
  • Thank you for the response, I am working with codeigniter and i saved both app.js and data.json in Assets/js folder , Is that ok. – PHP Devloper Feb 05 '19 at 10:36
  • @PHPDevloper : Yes, but you dont need to save `data.json` as that data would come from `backend server`. I hope you got the solution. If yes, please mark it as an answer – Shashank Vivek Feb 05 '19 at 12:27
  • Thank you for responding , I almost near the expecting result , above i show the result i got by changing the code as per your suggestion. please check . I get the menu items by using that code , but my doubt is how will i create a dropdown below the news i dont see any options there. – PHP Devloper Feb 06 '19 at 05:50
  • @PHPDevloper : show me the response you are getting from DB – Shashank Vivek Feb 06 '19 at 13:12
  • Please check The new result after I change the code given above , That is the screen shot of the response getting from DB. – PHP Devloper Feb 07 '19 at 04:56
  • can any one help me with this or Is it that difficult to do ..can i try any other method. – PHP Devloper Feb 08 '19 at 05:22
  • @PHPDevloper: check the new plunkr that i have added as per your comments where you just need submenu for **News**. Also, note use `ng-class` for styling and removing it for other menus – Shashank Vivek Feb 10 '19 at 05:37
  • Thank You so much , I got the result .Once angin thank you so much for responding. Thank you – PHP Devloper Feb 11 '19 at 05:31
  • Can you explain just this line of code , how it works??
  • {{menu.title}}
    • – PHP Devloper Feb 11 '19 at 05:35
  • Im really sorry for asking too many question, I really want to learn this, Can you help me with one more doubt, how can i add links to these items . – PHP Devloper Feb 11 '19 at 06:25
  • The links are added with `href` as `href="{{submenu.action}}`. Try changing `#` values to something relevant and click. – Shashank Vivek Feb 11 '19 at 06:37
  • @PHPDevloper : You can remove `lastOrFirst`. its of no use. check https://plnkr.co/edit/Xxjl3iT6nEHWbdMiYqzw?p=preview. For `ng-class` , you can check on google https://stackoverflow.com/questions/16529825/angularjs-ngclass-conditional – Shashank Vivek Feb 11 '19 at 06:40
  • Please mark it as an answer & **upvote** so that it can help others who are looking for similar issue – Shashank Vivek Feb 11 '19 at 06:52