JavaScript function for jQuery tabs - javascript

I've been Googling for the past 2 days trying to figure out how to add events to my tabs, but I'm having trouble getting any of the solutions to work. I'm completely over my head with this stuff, so please include simple explanations. What I have so far are some tabs (in which I just copied and pasted):
<div id="tabs" role="tabpanel">
<!-- Nav tabs -->
<ul id="tabs" class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">Active</li>
<li role="presentation">Inactive</li>
<li role="presentation">Retired</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="Active"></div>
<div role="tabpanel" class="tab-pane" id="Inactive"></div>
<div role="tabpanel" class="tab-pane" id="Retired"></div>
</div>
</div>
I also copied and pasted a JavaScript function that looks unlike any other JavaScript function I've dealt with and it doesn't work(the alert statement is never fired):
<script type="text/javascript" >
$(function () {
$('#tabs').tabs({
activate: function (event, ui) {
var $activeTab = $('#tabs').tabs('option', 'active');
if ($activeTab == 1) {
alert(Tab 1!);
}
if ($activeTab == 2){}
if ($activeTab == 3){}
}
});
});
</script>
I'm using the Spring MVC and my intention is to send the value of the tab back to the controller through a JavaScript function (as I see no other way around it). Based on that value, a table on the page is populated with certain data ('modules' in this case). I've created a controller method and I'm not sure whether it should be get or post or how to get the tab values in there. Here's my attempted controller method code:
#RequestMapping(method = RequestMethod.POST, value = "/update")
public final String changeTab(final ModelMap model, #RequestParam(value = "id") String id) {
System.out.println(id);
//model.addAttribute("modules", moduleDao.sortStatus(status));
return "module/admin";
}
What can I do to my code to achieve this? Am I even on the right track? Any help is much appreciated.

There are couple of issues with you tab creation html. Attached is your modified code that works when I gave it a try. You were missing id attribute on tab div that should match href. Hope this helps with the jQuery part.
W.r.t to the second question on binding with spring, now that you have event handler attached to your tabs, you can trigger a AJAX post or get from the event handler based on the tab and send the values of tab (am not sure what value you wanted to send). You can check how to do get/post from "http://api.jquery.com/jquery.ajax/". Your question does not explain me what sort of response you are expecting from controller, so I cannot help you on that. Hope this helps.
<script type="text/javascript" >
$(function () {
$('#tabs').tabs({
activate: function(event ,ui){
var $activeTab = $('#tabs').tabs('option', 'active');
if ($activeTab == 1) {
alert("tab1");
}
}
});
});
</script>
<body>
<div id="tabs" role="tabpanel">
<!-- Nav tabs -->
<ul id="tabs" class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">Active</li>
<li role="presentation">Inactive</li>
<li role="presentation">Retired</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div id="tabs-1" role="tabpanel" class="tab-pane active" ></div>
<div id="tabs-2" role="tabpanel" class="tab-pane" ></div>
<div id="tabs-3" role="tabpanel" class="tab-pane" ></div>
</div>
</div>
</body>

[edited]
There's something in your HTML that's breaking the jQuery tabs code.
Consider this fiddle: http://jsfiddle.net/1r712od4/2/ - it works fine.
Try taking the unneeded aria-controls and classes from your code and trying again.
The JS you need is something like:
$('#tabs').tabs({
activate: function (event, ui) {
var activeTab = ui.newTab.data("tabid");
if (activeTab == 1) {
alert ("Tab 1!");
}
}
});
As for the Spring code, someone else will have to help you out with that!

Related

Bootstrap tab pane will not toggle when using href hash route

Would like to use a basic hash router to drive navigation of bootstrap 4.6 tab component.
However when I call the .tab('show') function, the right tab toggles but it's respective tab-pane section does not get toggled. I made sure the tab-panes id attribute has the same value as the tabs href attribute.
I was able to make it work by manually showing and hiding the tab panes. But bootstrap has this functionality built in. Does anyone know how to do it ?
Thanks
hashRouteInit = function(callback) {
window.addEventListener('hashchange', callback, false);
}
const routes = {
'#/units': function() {
console.log('units');
},
'#/clients': function() {
console.log('clients');
},
'#/searches': function() {
console.log('searches');
},
};
hashRouteInit(function() {
console.log(window.location.hash);
console.log(routes);
if (routes[window.location.hash] != undefined) {
/*how can i remove the following 3 lines and let .tab('show') show the tab pane*/
/*$(`.tab-content .show`).hide();
$(`.tab-content .show`).removeClass('show');
$(`.tab-content .show`).removeClass('active');*/
$(`#stats-tabs a[href="${window.location.hash}"]`).tab('show');
/*how can i remove the following 3 lines and let .tab('show') show the tab pane*/
/*$(`.tab-content .tab-pane[id="${window.location.hash}"]`).addClass('show');
$(`.tab-content .tab-pane[id="${window.location.hash}"]`).addClass('active');
$(`.tab-content .tab-pane[id="${window.location.hash}"]`).show();*/
routes[window.location.hash]();
}
});
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div id="container">
<ul class="nav nav-tabs" id="stats-tabs" role="tablist">
<li class="nav-item" role="presentation"><a class="nav-link active" id="units-tab" href="#/units" role="tab" aria-controls="units" aria-selected="true">Units</a></li>
<li class="nav-item" role="presentation"><a class="nav-link" id="clients-tab" href="#/clients" role="tab" aria-controls="clients" aria-selected="false">Clients</a></li>
<li class="nav-item" role="presentation"><a class="nav-link" id="searches-tab" href="#/searches" role="tab" aria-controls="searches" aria-selected="false">Searches</a></li>
</ul>
<div class="tab-content pt-2" id="stats-tabs-content">
<div class="tab-pane fade show active" id="#/units" role="tabpanel" aria-labelledby="units-tab">units</div>
<div class="tab-pane fade" id="#/clients" role="tabpanel" aria-labelledby="clients-tab">clients</div>
<div class="tab-pane fade" id="#/searches" role="tabpanel" aria-labelledby="searches-tab">
<div id="search-overview-searches">searches</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.0/js/bootstrap.min.js"></script>
</body>
Apparently the HTML ID attribute has to start with a letter(https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id)
Once I set the href attributes to #name and used the same value but without the number sign for the ID of the tab pane. It started working.
Note that the spec(https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#:~:text=The%20id%20attribute%20specifies%20its,not%20contain%20any%20space%20characters.) doesn't say anything about not using slashes to construct a hierarchy in the id string. However if i want to have another tab section as a child in a tab pane, i won't be able to have an id like this(parent/child) and have bootstrap tabs work. So you are only left with - or _ which doesn't seem so intuitive in the URI. Especially once you wan't to add an numerical identifier onto it. Like so parent/child/3. Can't do that, but the spec doesn't say anything about not allowing slashes.
Does anyone know why forward slashes are not allowed in the id string if the first char is not a forward slash?

How to link to a specific tab on a page | Bootstrap & Rails

I have an about page with different tabs that I want to link to individually through my footer. I've checked out other solutions to this but I'm not sure of two things. I've tried to use the following javascript to solve this, but I'm not sure how to configure what is here:
A) How do I configure the JavaScript to work with my HTML?
B) Do I need to duplicate the following JS for each tab on the page?
// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('/info#advertise-tab')) {
$('.nav-tabs li a[href="info' + url.split('#')[1] + 'advertise-tab"]').tab('shown.bs.tab');
}
// Change hash for page-reload
$('.nav-tabs li a').on('shown.bs.tab', function (e) {
window.location.hash = e.target.hash;
})
My page's view:
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" id="advertise-li">Advertise</li>
<li role="presentation" id="legal-li">Legal</li>
</ul>
<div class="tab-content" id="infoTabContent">
<div role="tabpanel" class="tab-pane fade" id="advertise-tab">
<p>stuff</p>
</div>
<div role="tabpanel" class="tab-pane fade" id="legal-tab">
<p>other stuff</p>
</div>
</div>
Page footer links:
Advertise
Legal
Here is the jsfiddle

How to use an input box with an keyUp function to dynamically display matched list elements

I created a site in codepen.io that links to a list of twitch streams, then displays the online streamers in one tab, offline in another, and both in a third. Each user is appended to the appropriate tab via a JQuery function. (this works)
I'm attempting to search through the tab with an input element and display the matched twitch streams.
I've gotten as far as using an at keyup function on the text element, but I do not know how to apply this to a tab that has its elements appended to it. The only similar questions asked have to do with simple lists, not tabs.
Thanks for any and all help.
This is where I've gotten so far:
The html:
<h1 id="head">
<input type="text" id="inputBox" class="rounded" value="">
</h1>
<ul class="nav nav-tabs" id = "list">
<li class="nav active" id ="AllTab">All</li>
<li class="nav" id ="OnlineTab">Online</li>
<li class="nav" id="OfflineTab">Offline</li>
</ul>
<div class="tab-content" id="stuff">
<div class="tab-pane fade in active" id="All">
</div>
<div class="tab-pane fade" id="Online">
</div>
<div class="tab-pane fade" id="Offline" >
</div>
</div>
The javascript in question:
$('#inputBox').keyup(function(){
var valThis = $(this).val();
$('.nav active').each(function(){
var text = $(this).text().toLowerCase();
(text.indexOf(valThis) != -1) ? $(this).show() : $(this).hide();
});
});
Try this - fiddle
$('#inputBox').keyup(function () {
var valThis = $(this).val().toLowerCase();
$('.nav,.tab-pane').hide();
$('.nav').each(function () {
var text = $(this).text().toLowerCase();
if (text.indexOf(valThis) > -1) {
$(this).show();
$('#'+$(this).attr("id").replace('Tab','')).show();
} else {
$(this).hide();
$('#'+$(this).attr("id").replace('Tab','')).hide();
}
});
});
Also Angular JS makes impementing such type of functionalities on large data very easy. You might want to check it out.

Bootstrap: Tab doesn't show content correctly

In my page i use bootstrap's tab, but when i click each of tab, content of tab doesn't show correctly, this is my code:
<div class="w470px exam" style="border: 1px solid #ddd; margin-top: 30px;">
<ul id="myTab" class="nav nav-tabs nav-justified">
<li class="active">Home</li>
<li>Favoriets</li>
<li>Friends</li>
<li>Experience</li>
</ul>
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade in active" id="tab-home">
HOME
</div>
<div class="tab-pane fade active" id="tab-fav">
My favoriets
</div>
<div class="tab-pane fade active" id="tab-fr">
My Friends
</div>
<div class="tab-pane fade active" id="tab-ex">
My experience
</div>
</div>
<script type="text/javascript">
$(function () {
var navTabs = $('.nav-tabs a');
var hash = window.location.hash;
hash && navTabs.filter('[data-value="' + hash + '"]').tab('show');
navTabs.on('shown', function (e) {
var newhash = $(e.target).attr('data-value');
window.location.hash = newhash;
});
})
</script>
</div>
JSFIDDLE
Where am i wrong? How can i fix it?
You have active class on all tab-pane divs. Remove it from all of them but the first one. BTW, in bootstrap 3, the event it is not show, it is shown.bs.tab Bootstrap 3 tabs.
I personally use this code: Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload or Hyperlink, the only change I made, was to replace the on('show' with on('shown.bs.tab' .
On your JSFiddle a reference to the jQuery library is missing. Once adding a jQuery reference under Frameworks & Extensions it's switching tab content.
Under the Bootstrap documentation is states that jQuery is required for bootstrap javascript features: http://getbootstrap.com/getting-started/#whats-included
Also, your function is being using jQuery notation.

how to activate particular tab on the basis of previous page in second html page load

I have a pie chart. Functionality is that when I select a particular section of pie chart, then it redirects to the next page with selected tab. I am able to get value of that pie chart section by using this code:
var search = document.URL.split('?')[1];
Now my fight is how to enable particular tab on the next page on the basis of value of "search" variable.
Next when user comes direct to this page then 1st tab is enable by default. for this I used the code:
<div class="tab-pane in active" id="tab1">
I have tried in many ways(like $("ul.tabs li:first").addClass("active").show() or using eq()) but have no success. Please let me know if anyone can help me out.
This should do the job:
<script>
$(function() {
$("#tabs").tabs();
var search = document.URL.split('?')[1];
switch (search) {
case "error":
$("#tab1 > a").click();
break;
case "suspect":
$("#tab2 > a").click();
break;
}
});
</script>
<div id="tabs">
<ul class="nav nav-tabs" id="myTab">
<li id="tab1" class="active"> Error</li>
<li id="tab2"><a href="#suspect" data-toggle="tab" >Suspect </a></li>
</ul>
<div class="tab-pane in active" id="error">
Error
</div>
<div class="tab-pane" id="suspect">
Suspect
</div>
</div>

Categories