Bootstrap 4 navbar with Angular - how to activate item - angular-ui-router

I am putting together B4 navbar with Angular router states. Here is my code:
HTML:
<ul class="nav" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" ui-sref="state1" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" ui-sref="state2" href="#">Link</a>
</li>
</ul>
<div ui-view></div>
Controller:
var myApp = angular.module("myApp",[ "ui.router", "AppCtrls"]);
myApp.config(function ($stateProvider, $urlRouterProvider){
$stateProvider.state("state1", {
url: "#",
template: "<p>State 1</p>",
controller: "Ctrl1"
}).state("state2", {
url: "#",
template: "<p>State 2</p>",
controller: "Ctrl2"
});
});
var ctrls = angular.module("AppCtrls", []);
ctrls.controller( "Ctrl1", function($scope) {
console.log("Ctrl1 loaded.");
});
ctrls.controller( "Ctrl2", function($scope) {
console.log("Ctrl2 loaded.");
});
What I want to achieve is to have a default active item with its content being shown. I am setting up the first link to active but it doesn't do anything.
Here is my
Fiddle

try this code for HTML :
<ul class="nav" id="myTab" role="tablist">
<li class="nav-item" ui-sref-active="active">
<a class="nav-link" ui-sref="name1">Active</a>
</li>
<li class="nav-item" ui-sref-active="active">
<a class="nav-link" ui-sref="name2">Link</a>
</li>
</ul>
<div ui-view></div>
And this Angular :
var myApp = angular.module("myApp",[ "ui.router"]);
myApp.config(function($stateProvider, $urlRouterProvide) {
$urlRouterProvider.otherwise("/");
$stateProvider
.state('name1',{
url: "/name1",
conontroller:'ctrl1',
templateUrl: "name1.html"
})
.state('name2',{
url: "/name2",
conontroller:'ctrl2',
templateUrl: "name2.html"
})
});
myApp.controller( "ctrl1", function($scope) {
console.log("Ctrl1 loaded.");
});
myApp.controller( "ctrl2", function($scope) {
console.log("Ctrl2 loaded.");
});

Related

VueJs and Laravel dom contents not updating

I have this method when I click to view any member it shows the detail of the member in the modal, but it's not working as I expected
vue js code
const member_modal = ref(null);
const viewMember = (member) => {
axios.get("/api/member/" + member.id).then((response) => {
member_modal.value = response.data;
console.log(member_modal.value);
$("#viewMemberModal").modal("show");
});
};
code inside the modal (DOM)
<a href="#" #click.prevent="viewMember(member)"
><i class="fa fa-eye ml-2"></i
></a>
<img class="profile-user-img img-fluid img-circle"
src="{{ member_modal.image }}"
alt="User profile picture"
/>
</div>
<h3 class="profile-username text-center">
{{ member_modal.full_name }}
</h3>
<ul class="list-group list-group-unbordered mb-3">
<li class="list-group-item">
<b>Gender</b>
<a class="float-right">{{ member_modal.gender }}</a>
</li>
<li class="list-group-item">
<b>Phone</b>
<a class="float-right">{{ member_modal.phone }}</a>
</li>
<li class="list-group-item">
<b>Email</b>
<a class="float-right">{{ member_modal.email }}</a>
</li>
</ul>
I want when I click on the view member button it shows the specific member data in the modal, but it's not updating the values in the (dom), when I click the view member, it fetches the data from the database successfully and assigns the response result to the member_modal but the change is not showing the data in the modal

NavBar using the controller for the page it is on instead of the controller set in ActionLink

I am new to MVC and bootstrap and have a problem with the navbar. When I am on the home page the navbar works like it is suppose to, but when I change to a new page which uses the ActiveUser controller. The controller in the link changes from the Homecontroller to the ActiveUser controller which is not what suppose to happen. The dropdown menu part is just a test to see if it does the same and it does.
NavBar code
<nav class="navbar navbar-expand-md">
<div class="container">
#Html.ActionLink("TADS", "About", "Home", new { #class = "navbar-brand" })
<div class="navbar-nav mr-auto">
<ul class="navbar-nav nav">
<li class="nav-item">
#Html.ActionLink("Home", "Index", "Home", new { #class = "nav-link" })
</li>
<li class="nav-item">
#Html.ActionLink("About", "About", "Home", new { #class = "nav-link" })
</li>
<li class="nav-item">
#Html.ActionLink("Contact", "Contact", "Home", new { #class = "nav-link" })
</li>
<li class="dropdown">
Dropdown
<div class="dropdown-menu">
#Html.ActionLink("Home", "Index", "Home", new { #class = "dropdown-item" })
#Html.ActionLink("About", "About", "Home", new { #class = "dropdown-item" })
#Html.ActionLink("Contact", "Contact", "Home", new { #class = "dropdown-item" })
</div>
</li>
</ul>
</div>
</div>
</nav>
When on the Home Page using the home controller the navbar is converted correctly in the the browser
<div class="navbar-nav mr-auto">
<ul class="navbar-nav nav">
<li class="nav-item">
<a class="nav-link" href="/home/Index?Length=4">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Home/About?Length=4">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Home/Contact?Length=4">Contact</a>
</li>
<li class="dropdown">
Dropdown
<div class="dropdown-menu">
<a class="dropdown-item" href="/home/Index?Length=4">Home</a>
<a class="dropdown-item" href="/Home/About?Length=4">About</a>
<a class="dropdown-item" href="/Home/Contact?Length=4">Contact</a>
</div>
</li>
</ul>
</div>
But when I change to Active user page the navbar is not converted correctly. The homecontroller is replaced with the ActiveUser controller which is not what it should be doing.
<div class="navbar-nav mr-auto">
<ul class="navbar-nav nav">
<li class="nav-item">
<a class="nav-link" href="/ActiveUser/index?Length=4">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/ActiveUser/About?Length=4">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/ActiveUser/Contact?Length=4">Contact</a>
</li>
<li class="dropdown">
Dropdown
<div class="dropdown-menu">
<a class="dropdown-item" href="/ActiveUser/index?Length=4">Home</a>
<a class="dropdown-item" href="/ActiveUser/About?Length=4">About</a>
<a class="dropdown-item" href="/ActiveUser/Contact?Length=4">Contact</a>
</div>
</li>
</ul>
</div>
Any help will be greatly appreciated.
I finally found the answer to the one part of the question on Stackoverflow at these posts:
What is the purpose of `area = "" ` in an Html.ActionLink?
How to correctly use Html.ActionLink with ASP.NET MVC 4 Areas

MVC display UserName on layout page

In my app, users can login via a barcode scan.
The scan triggers a controller action like so:
function scanner(value) {
var badge = (value.startsWith('B-') || value.startsWith('B-')); //b- of B- = badge scan
if (badge)
{
$.ajax({
type: 'GET',
url: '/Login/Login',
data: {'badge': value},
succes: function () {
console.log("logged in");
}
});
}
else return;
}
Controller action:
public ActionResult Login(string badge)
{
string userName = string.Empty;
if (badge != null)
{
int oncid;
if (Int32.TryParse(badge.Substring(2), out oncid))
{
Core.Employee employee = Data.Oracle.GetEmployee(oncid.ToString());
if (employee != null)
{
userName = employee.FirstName + ' ' + employee.Name;
}
}
else
userName = "invalid badge";
}
ViewBag.UserName = userName;
return PartialView("_Login");
}
in _Layout.cshtml:
<nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#"><img src="~/Content/Images/logo.png" height="36" width="36" class="d-inline-block align-top"> App Name</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto mt-2 mt-lg-0">
<li class="nav-item active">
#*<a class="nav-link" href="#Url.Action("Index", "Home")">Home <span class="sr-only">(current)</span></a>*#
<a class="nav-link" href="#Url.Action("Index", "Home")"><span class="fas fa-home fa-2x" aria-hidden="true"></span></a>
</li>
<li class="nav-item">
<a class="nav-link" id="smrButton" href="#">SMR</a>
</li>
</ul>
</div>
#Html.Partial("_Login")
</div>
</nav>
_Login.cshtml just has #ViewBag.UserName for now.
If i set a breakpoint here, i can see that ViewBag.UserName holds the correct value, however, nothing is displayed in my page layout. What am i missing here?
Try using Session instead of ViewBag. For example:
Session["username"] = userName;
And call it in the view, simple as that

How to autocomplete a <li> with <a>

I have generated a list with links inside in hierarchy. I am trying to make a form/input to let people type in words and it fetch the list(in hierarchy) with autocomplete. The autocomplete results are links. Any help would be great. Thanks
Something like this
<ul>
<li>
1
<ul>
<li>1.1</li>
<li>1.2</li>
</ul>
</li>
<li>
1
<ul>
<li>1.1</li>
<li>1.2</li>
</ul>
</li>
</ul>
Collect the autocomplete strings in an array using a JQuery selector and a map function
$(function() {
var availableTags = $('ul > li > a').map(function() {
return this.href;
}).toArray();
$("#tags").autocomplete({
source: availableTags
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<label>Items autocomplete</label>
<input id="tags" type="text" style="width: 200px;">
<span id="results_count"></span>
<p>
<ul>
<li>
1
<ul>
<li>1.1
</li>
<li>1.2
</li>
</ul>
</li>
<li>
1
<ul>
<li>1.1
</li>
<li>1.2
</li>
</ul>
</li>
</ul>
EDIT: second version where autocomplete uses the text in the <li>'s
$(function() {
var availableTags = $('ul > li > a').map(function() {
return $(this).text();
}).toArray();
$("#tags").autocomplete({
source: availableTags
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<label>Items autocomplete</label>
<input id="tags" type="text" style="width: 200px;">
<span id="results_count"></span>
<p>
<ul>
<li>
xxxx
<ul>
<li>xxyy
</li>
<li>xxzz
</li>
</ul>
</li>
<li>
xxyy
<ul>
<li>yyzz
</li>
<li>xxyz
</li>
</ul>
</li>
</ul>
Jsfiddle
see datalist and list in html5
datalist
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
$(function() {
var availableTags = $('ul > li > a').map(function() {
console.log($(this));
return {
label: $(this).text(),
value: $(this).attr('href')
};
}).toArray();
$("#tags").autocomplete({
source: availableTags,
select: function (event, target) {
var link = target.item.value;
console.log(link);
location.href = link;
}
});
});

Bootstrap dropdown with Ajax

I am using Bootstrap 3 and I am trying to obtain an action following the click on a dropdown.
My current issue is that I got the entire page reloaded instead of updating the content of a specific .
This is my current code:
<div class="dropdown pull-right">
<button type="button" class="btn btn-default" data-toggle="dropdown">Mybutton!</button>
<ul class="dropdown-menu test" role="menu">
<li role="presentation" class="dropdown-header">Select your action</li>
<li>Action 1</li>
<li>Action 2</li>
<li>Action 3</li>
</ul>
</div>
Then I have a div that where I would like to show the result:
<div id="result">
<?php
$test = $_POST['sentData'];
echo $test;
echo 'something else to write here';
?>
</div>
This is the ajax call:
$(document).ready(function() {
$('.test li a').on('click', function () {
var sentData = $(this).html();
$.ajax({
type: "POST",
url: "design7.php",
data: "sentData=" + sentData,
dataType: "html",
success: function(data)
{
$('#result').html(data);
}
});
});
});
I have tried to find a solution but my skills are limited. I will appreciate any help.

Resources