how to run ajax from Drop down item in laravel - javascript

Good Morning,
I am new to coding and learning laravel for home project. I am trying to get data from database as per dropdown selection. The code for dropdown item is as below
<div class="dropdown">
<button class="btn p-0" type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="bx bx-dots-vertical-rounded"></i>
</button>
<div id="milkstatementdropdown" name="milkstatementdropdown"class="dropdown-menu dropdown-menu- end" aria-labelledby="milkstatementdropdown">
<li><a class="dropdown-item" href="javascript:void(0);">Yesterday</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Last 7 Days</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Last Month</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Last 3 Month</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Last 6 Month</a></li>
</div>
</div>
`
After selection of drop down item, need to get value and then pass the same to controller using AJAX.
below is the script for getting the selection value and process ajax.
`
$("#milkstatementdropdown").change(function(){
var duration = $(this).val();
alert(course);
// ajax call
});
`
i am not able to get the Alert..
Did i miss something and make mistake in code...
Thanks in Advance.

try this
$('#id').on('select2:close', function () {
//do anything
});

Try this one // You can use Anything
$('#milkstatementdropdown li a').on('click', function(){
alert($(this).text());
//do anything
});

You have to use onClick beacuase your dropdown is not an Select Tag:-
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Yesterday')">Yesterday</a></li>
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last 7 Days')">Last 7 Days</a></li>
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last Month')">Last Month</a></li>
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last 3 Month')">Last 3 Month</a></li>
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last 6 Month')">Last 6 Month</a></li>
And then in your Javascript part you have make get_data function to hanle onClick event.
function get_data(duration){
// Now you can get data from controller according to received 'duration'
// ajax call
}

Related

Bootstrap Navbar Toggler - Custom JS causes navbar menu to close on dropdown click

I'm new to JS and am in the process of learning while coding a website. I'm using a template that I found online with some custom modifications so I'm not super familiar with the JS used. Here is the issue.
While using the navbar toggler there is some custom JS to close the menu when the once a click happens in the menu. The issue is that I added a dropdown into the menu, and when it's clicked instead of showing the drop down details, the JS executes and closes the menu. here is the JS:
window.addEventListener('DOMContentLoaded', event => {
// Navbar shrink function
var navbarShrink = function () {
const navbarCollapsible = document.body.querySelector('#mainNav');
if (!navbarCollapsible) {
return;
}
if (window.scrollY === 0) {
navbarCollapsible.classList.remove('navbar-shrink')
} else {
navbarCollapsible.classList.add('navbar-shrink')
}
};
// Shrink the navbar
navbarShrink();
// Shrink the navbar when page is scrolled
document.addEventListener('scroll', navbarShrink);
// Activate Bootstrap scrollspy on the main nav element
const mainNav = document.body.querySelector('#mainNav');
if (mainNav) {
new bootstrap.ScrollSpy(document.body, {
target: '#mainNav',
offset: 74,
});
};
// Collapse responsive navbar when toggler is visible
const navbarToggler = document.body.querySelector('.navbar-toggler');
const responsiveNavItems = [].slice.call(
document.querySelectorAll('#navbarResponsive .nav-link')
);
responsiveNavItems.map(function (responsiveNavItem) {
responsiveNavItem.addEventListener('click', () => {
if (window.getComputedStyle(navbarToggler).display !== 'none') {
navbarToggler.click();
}
});
});
});
Only the bottom function is running the menu click/close function, but I provided all the code as for the custom menu actions as I'm not super familiar with JS. I understand, more or less, what it is doing, but I'm not sure how to modify it appropriately.
I'd imagine there is a simple way to modify that last "if" statement to check to see what was actually clicked (for example, if a .dropdown-menu was clicked). I just don't know how to use the code that is there and check what click caused the event trigger. I'd like to keep the close behavior when the .dropdown-items and other .nav-items are clicked. I just don't want the behavior when the .dropdown-menu is clicked.
Here is the html:
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav text-uppercase ms-auto py-4 py-lg-0">
<li class="nav-item"><a class="nav-link" href="#portfolio">Products</a></li>
<li class="nav-item"><a class="nav-link" href="#services">Services</a></li>
<li class="nav-item"><a class="nav-link" href="#how-to-order">How Order</a></li>
<li class="nav-item dropdown"><a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">F.A.Q.</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="dropdownMenuButton2">
<li><a class="dropdown-item" href="#">Knowledge Base</a></li>
<li><hr class="dropdown-divider"></li>
<li><h4 class="dropdown-header">Most Common Questions:</h4></li>
<li><a class="dropdown-item" href="#">Where are you located</a></li>
<li><a class="dropdown-item" href="#">How long is shipping</a></li>
<li><a class="dropdown-item" href="#">How do I pay</a></li>
<li><hr class="dropdown-divider"></li>
</ul>
</li>
<li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
</ul>
</div>
I'm pretty sure this is a super simple JS problem that any coder worth his $.02 would know, but not knowing JS I don't even know where or what to search for. Any help?
The issue with your code is that when you are using querySelectorAll to access the nav-links the code is trying to access all the nav-links i.e., you are considering all the nav-links then the dropdown in your nav which is also a nav-link is also considered. So the better way to solve this is you can create a new class and attach it to the dropdown nav-link in your case F.A.Q and exclude that class from the function.
Your code:
<li class="nav-item dropdown"><a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">F.A.Q.</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="dropdownMenuButton2">
<li><a class="dropdown-item" href="#">Knowledge Base</a></li>
<li><hr class="dropdown-divider"></li>
<li><h4 class="dropdown-header">Most Common Questions:</h4></li>
<li><a class="dropdown-item" href="#">Where are you located</a></li>
<li><a class="dropdown-item" href="#">How long is shipping</a></li>
<li><a class="dropdown-item" href="#">How do I pay</a></li>
<li><hr class="dropdown-divider"></li>
</ul>
</li>
const responsiveNavItems = [].slice.call(
document.querySelectorAll('#navbarResponsive .nav-link')
);
You can add a class called faq and exlude it from the querySelectorAll using :not()
<!-- added a new class faq to the dropdown link -->
<li class="nav-item dropdown"><a class="nav-link dropdown-toggle faq" data-bs-toggle="dropdown" role="button" aria-expanded="false">F.A.Q.</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="dropdownMenuButton2">
<li><a class="dropdown-item" href="#">Knowledge Base</a></li>
<li><hr class="dropdown-divider"></li>
<li><h4 class="dropdown-header">Most Common Questions:</h4></li>
<li><a class="dropdown-item" href="#">Where are you located</a></li>
<li><a class="dropdown-item" href="#">How long is shipping</a></li>
<li><a class="dropdown-item" href="#">How do I pay</a></li>
<li><hr class="dropdown-divider"></li>
</ul>
</li>
// select all links except faq class
const responsiveNavItems = [].slice.call(
document.querySelectorAll('#navbarResponsive a.nav-link:not(.faq)')
);
Just add the below scripts within your tags, preferably before the closing tag
<!-- JavaScript -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

Static HTML - localization using jquery.i18n.js not working

I've tried to make a simple HTML test where the content will be translated into specific languages using jquery.i18n.js library. It partially works meaning the default (English) text is being loaded, but the lang switch is not.
my jsfiddle is here:
https://jsfiddle.net/g8uwotsx/12/
HTML code:
<header>
<nav>
<ul class="switch-locale">
<li><a class="dropdown-item" href="#" data-locale="en">EN</a></li>
<li><a class="dropdown-item" href="#" data-locale="pl">PL</a></li>
<li><a class="dropdown-item" href="#" data-locale="fr">FR</a></li>
<li><a class="dropdown-item" href="#" data-locale="de">DE</a></li>
<li><a class="dropdown-item" href="#" data-locale="it">IT</a></li>
<li><a class="dropdown-item" href="#" data-locale="es">ES</a></li>
<li><a class="dropdown-item" href="#" data-locale="ru">RU</a></li>
</ul>
</nav>
</header>
<div class="container">
<div class="row">
<div class="col text-center">
<h1 data-i18n="app-title">placeholder</h1>
</div>
</div>
</div>
JS code:
jQuery(function() {
$.i18n().load( {
'en': {
'app-title': 'english headline'
},
'it': {
'app-title': 'italian headline'
},
'es': {
'app-title': 'spanish headline'
}
}).done(function() {
$('.switch-locale').on('click', 'a', function(e) {
e.preventDefault();
$.i18n().locale = $(this).data('locale');
});
});
$('body').i18n();
});
CSS code:
.switch-locale li {
display:inline-block;
list-style-type:none;
text-align:center;
}
.switch-locale {
text-align:center;
border-bottom:1px solid grey;
}
the inspiration was official documentation and this link:
https://phrase.com/blog/posts/jquery-i18n-the-advanced-guide/
expected behaviour would be:
by default user sees only English texts
after changing the language via menu the text will be replaced with a local one
in this example I'm using a solution without separate json files per lang (in the final one I would like to make it that way)
Issues:
text is being displayed only in English
text is not translating despite the clicking on different language

Create a dropdown directive

I want to create a custom dropdown directive using Angular, This is as far I have reached.
I am trying close the dropdown on click outside or on esc key press. Also how can I enable inside click because right now the dropdown is closing if i click any of the items inside dropdown.
#HostBinding('class.show') isOpen = false;
#HostListener('click') toggleDropdown() {
this.isOpen = !this.isOpen;
const elem = this._elem.nativeElement.querySelector('.dropdown-menu');
if (this.isOpen) {
this._renderer.addClass(elem, 'show');
} else {
this._renderer.removeClass(elem, 'show');
}
}
<div class="dropdown" dropdown>
<a
href="#"
>
Dropdown link
</a
>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div>
I have modified your directive a little and added the features which you wanted. Please check and let me know if its ok.
on click of dropdown items its not closing the dropdown.
on click of outside the dropdown list its closing the list
https://stackblitz.com/edit/angular-ivy-gu9ndu?file=src%2Fapp%2Fapp.component.ts

Change language code within URL javascript/nodejs

I have common header file includes navbar in which have multilanguage dropdown.
when i select dropdown language, translates page without any issues.
But when i move to other pages, if should reflect the language selected on first page (eg) if th is selected on first page, other page should be /th instead of '/en'
currently common header file is always /en, when i chose dropdown how i update a href links javascript
need to update all href links when particular language is selected from dropdown.
//header.ejs
<body>
<nav>
<ul>
<li class="nav-item">
<a class="nav-link" href="/en/about" style="margin-right: 1.5rem!important;">about us</a>//change this link
</li>
<li class=" nav-item">
<a class="nav-link" href="/en/contact"
style="margin-right: 1.5rem!important;">contact us</a>
</li>//change this link
</ul>
<div class="dropdown">
<button id="language" class="btn btn-warning dropdown-toggle" type="button" id="dropdownMenu2"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="clickButton()">
English
</button>
<div id="languagelist" class="dropdown-menu" aria-labelledby="dropdownMenu2" onclick="clickItem(); return false">
<a class="dropdown-item" href="javascript:" onclick="setLanguage('en')">English</a>
<a class="dropdown-item" href="javascript:" onclick="setLanguage('th')">Thai</a>
</div>
</div>
</nav>
</body>
on load see if language is set or not in cookie.
set selected language in cookie on setLanguage call.
$(document).ready(function() {
//read previously set cookie value
var selectedLanguage = $.cookie( 'selectedLanguage' );
// 1. on load see if language is set or not in cookie.
selectedLanguage = selectedLanguage ? selectedLanguage : 'en';
setLanguage(selectedLanguage);
function setLanguage(lan){
$.cookie('selectedLanguage', lan);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js" integrity="sha256-1A78rJEdiWTzco6qdn3igTBv9VupN3Q1ozZNTR4WE/Y=" crossorigin="anonymous"></script>

i did not understand how can i use AlloyUI dropdown script

i did not understand how can i use AlloyUI dropdown script. i have write this code 5 time on my same page.
<div id="myDropdown" class="dropdown">
<button id="myTrigger" class="btn btn-default dropdown-toggle" type="button">
Dropdown
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">Action</a></li>
</ul>
</div>
in this html have same classes and id, and i use this script. i take this code from alloy. follow this http://alloyui.com/examples/dropdown/
YUI().use(
'aui-dropdown',
function(Y) {
new Y.Dropdown(
{
boundingBox: '#myDropdown',
trigger: '#myTrigger'
}
).render();
}
);
i have 5 dropdown on my same page with same html code. when i run this code my first drop down is working and other is not working.

Categories