Generate Menu Html and bind div from Angularjs and mvc - asp.net-mvc

I am generating html menu string from XML in mvc controller and in angularjs i am passing the response to div like $("#divId").html(response).
Its working fine but some menus have sub menus so the open class is not calling so its not showing the submenus
please tell where i am doing wrong.

In angular, never use $() outside a directive (and generally use angular.element() instead ... But anyway you don't need it 99% of the time)
If you want to dynamically add some HTML into an angular template use the ng-bind-html property.
In your controller
$scope.mymenu = response;
In your html
<div ng-bind-html="response"></div>
You'll probably run into a case that angular don't trust your HTML. You'll have to use some angular services to say angular that your HTML should be trust (if it is a proper source).
Here is a good topic on how to make angular trust your HTML
Hope it helped.
EDIT
Here is a working plunker of this proposition
Looks like the first problem is that you try to write your HTML into the controller, wich is a bad thing.
Instead you should try to generate a JS object like this one :
$scope.mymenu = [{
name:"Adminitstration",
url:"/Administration",
},{
name: "My Dashboard",
url:"/mydashboard",
submenus: [{
name:"Activity dashboard",
url:"/activity",
}]
},{
name: "Profiles",
url:"/profiles",
submenus: [{
name:"Accounts",
url:"/accounts",
}]
}]
And use the angular templating to render an HTML according to theses
<ul>
<li ng-click="menu.submenusShow = !menu.submenusShow" ng-repeat="menu in mymenu">
{{menu.name}}
<ul ng-show="menu.submenusShow">
<li ng-repeat="submenu in menu.submenus">
{{submenu.name}}
</li>
</ul>
</li>
</ul>

Related

How to SSR nested components or slotted content with vueonrails?

I am trying to add Vue.js components to a Ruby-on-Rails app with SSR support by using Hypernova and vueonrails helpers.
I am able to render a registered component with Hypernova from a view (.html.erb) by using this helper:
<%= render_vue_component('HelloWorld', message: 'Hello World from SSR!') %>
Let's say my HelloWorld component has a <slot> in its template:
<template>
<div class="helloworld">
<h1>{{ message }}</div>
<slot></slot>
</div>
</template>
I want to replicate something like the following in the .html.erb using render_vue_component:
<hello-world message="Hello World from SSR!">
<hello-world>SSR Nested component</hello-world>
</hello-world>
How could I pass child elements to the render_vue_component helper so they get rendered by Hypernova?
Well, I analyzed the repo and by looking at the it, I think you should use the render_vue_component only for rendering the root component, like the main.js when you start a project with #vue/cli:
<%= render_vue_component('App', { router }) %>
I came to this conclusion by analyzing the vueonrails source code.
If you look to the render_vue_component function, it only takes the identifier and a data object, which internally calls render_react_component.
By this signature, it simply don't fit with Vue's render function signatures, which are (element, children) or (element, attributes, children).
Maybe you find a better support by opening an issue on the repo.
But I'd think twice before using this project because there isn't any documentation so far and checking the website, seems like they are more focused in selling books about the project than creating a documentation.

Adding element ID to navigation items in Grails Platform Core Navigation API

Is it possible to add HTML attributes such as id or class to each menu item generated by the Grails Platform Core Navigation API?
Perhaps something similar to:
home(controller: 'home', action:'index', titleText:'Home', elementId: 'navHome')
I am writing some functional tests which use the generated links. However, the link text (and possibly the URLs) might change. Using an ID would make the tests easier to maintain.
The Navigation API provides the ability to add additional values via the data map and custom menu item rendering.
Example:
Step 1
Add the data map to the menu item definition in config/AppNavigation.groovy.
home(controller: 'home', action:'index', titleText:'Home', data: [elementId: 'navHome'])
Step 2
Reference the data map using item.data.* in the GSP file.
<nav:primary scope="some scope" custom="true">
<li>
<p:callTag
tag="g:link"
attrs="${linkArgs + [class:active ? 'active' : ''] + [elementId:item.data.elementId]}"
><nav:title item="${item}"/></p:callTag>
</li>
</nav:primary>
This will result in the following HTML:
<ul class="nav primary">
<li>
Home
</li>
</ul>
See the nav:menu tag documentation in the Navigation API documentation for more information.

The Best Way to Highlight or Change Current Menu Item CSS Class in MVC

I have read a few articles as I was searching for a solution. They all seemed to favor a hard-coded or HTML Helper alternative; however, I wanted something simple and database driven. This is my best solution (submitted as an answer, by me).
Here are some other articles' solutions:
An easy way to set the active tab using controllers and a usercontrol in ASP.NET MVC?
asp.net mvc and css: Having menu tab stay highlighted on selection
ASP.NET MVC: Tabs ASCX - Adjust CSS class based on current page?
Just pass a TempData down from one of your controllers like this:
TempData("CurrentPage") = "Nutrition"
Then, in your View add a conditional like this:
<ul>
#For Each item In Model
Dim currentItem = item
If (Not String.IsNullOrEmpty(TempData("CurrentPage")) And TempData("CurrentPage") = currentItem.NAV_Element) Then
#<li><a class="current" href="#Html.DisplayFor(Function(modelItem) currentItem.NAV_Destination)">#Html.DisplayFor(Function(modelItem) currentItem.NAV_Element)</a></li>
Else
#<li>#Html.DisplayFor(Function(modelItem) currentItem.NAV_Element)</li>
End If
Next
</ul>
I have accomplished this in the past by using the route values to generate a href and then setting the parent tab to active based on that.
This is a bootstrap flavoured version:
<ul class="nav nav-tabs">
<li>Some page</li>
<li>Some other Page</li>
</ul>
<script type="text/javascript">
$(function(){
$('a[href="#Url.Action(ViewContext.RouteData.Values)"]').parents("li").addClass("active");
};
</script>
Si
I know I am too late to answer this but if you are using ASP MVC and looking for highlighting Menu items using controller and action, here is the solution which is working perfectly fine for me -
<li> <i class="fa fa-user-plus"></i> <span>Signup</span> </li>

Displaying a RoR app/page in an external html page

I was wondering if anyone can help me with the following problem:
I have a external (and normal) html page that uses javascript to display other html
files in an object tag. These files are in the same directory, so theres no problem there.
How can I link and display my Ruby on Rails application/pages in this same object tag and
html file? i.e. How do I integrate the two?
Thanx
These are the elements which send the data over and the object that displays the page:
<div id = "leftBar">
<div>
<nav>
<ul type="square">
<li>
<a id = "menuLink" href = "Page.html" onclick="loadPage(this); return false;">Solar Flare
</a>
</li>
</nav>
</div>
</div>
<div id = "mainPage">
<object id = "obj" type = "text/html" data= "Main.html"> </object>
</div>
This is the JavaScript function that displays the page in the object tag:
function loadPage(link)
{
document.getElementById("obj").data = link.href;
}
Ruby on Rails can only be run at a server. Before you can access the pages inside a Rails project, you need to start such a server on localhost or deploy your application to a webserver. For example, in a Rails 3 project, you can start a server with:
rails server
To access the pages inside a Rails project, just point to http://localhost:3000:
<a id="menuLink" href="http://localhost:3000" onclick="loadPage(this); return false;">Your Rails project</a>
This seems weird to me. Maybe you can explain a little more what you want to achieve.
In the meantime: if your Rails application is up an running anywhere on a server then you can access it like any other link. And if you want to embedd any page (no matter if Rails, PHP or a plain HTML page) you could use for example the iframe tag.

Jquery Tab: Can I use Html.ActionLink to fetch server data?

I'm using JQuery UI 1.7/PACKT book. It said on p.63 that the content of a page can be loaded just by using this statement
<li>AJAX Tab</li>
How can I obtain something with Html.ActionLink()
Thanks for helping.
If you are using the jQuery UI Tabs, Html.ActionLinks will work exactly the same way. The key is to ensure that you have your unordered list items within a div tag with an id of "tabs."
Something like this:
<div id="tabs">
<ul>
<li><%= Html.ActionLink("Tab Name 1", "Action1", new { Controller = "ControllerName" })%></li>
<li><%= Html.ActionLink("Tab Name 2", "Action2", new { Controller = "ControllerName" })%></li>
</ul>
</div>
Just include this within your .aspx file that you want the tabs to appear, and make sure to include the jquery and jquery-ui scripts, and it should work for you. You can read about the various Html.ActionLink parameters by following the link. That should help you understand the ActionLink better.
Hope that helps! Please let me know if you need any additional clarification.

Resources