The Script Below is the script I used for putting SharePoint 2013 web parts into a jQuery UItabbed view.
- <!-- Reference the jQueryUI theme's stylesheet on the GoogleCDN. Here we're using the "Start" theme -->
- <link type="text/css" rel="stylesheet"href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/themes/start/jquery-ui.css"/>
- <!-- Reference jQuery on the Google CDN -->
- <script type="text/javascript"src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
- <!-- Reference jQueryUI on the Google CDN -->
- <script type="text/javascript"src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
-
- <style type="text/css">
- </style>
-
- <script type="text/javascript">
- jQuery(document).ready(function($){
- $(".s4-wpcell").hide();
- $(".s4-wpcell-plain").hide();
-
-
- setTimeout(function(){
- HillbillyTabs(["Issue","ImageViewer","Time","Speakers","States","Cities","SlideShow"]);
- }, 0);
-
- });
-
- function HillbillyTabs(webPartTitles)
- {
- for(index inwebPartTitles)
- {
- var title =webPartTitles[index];
- $("#HillbillyTabs").append('<li><ahref="#Tab'+index+'" id="TabHead'+index+'"onclick="SetActiveTab(this.id);">'+
- title+'</a></li>').after('<divid="Tab'+index+'"></div>');
- $("span:contains('"+title+"')").each(function(){
- if($(this).text() == title){
- varwebPart =$(this).hide().closest("span").closest("[id^='MSOZoneCell_WebPart']");
- if($(webPart).contents().html() != undefined)
- {
- webPart = $(webPart).contents();
- }
- $("#Tab" + index).append((webPart));
- }});
- }
- $("#tabsContainer").tabs();
- $(".s4-wpcell").show();
- $(".s4-wpcell-plain").show();
-
- }
- </script>
- <div id="tabsContainer"><ulid="HillbillyTabs"></ul></div>
To use the script
To use the scripts, follow these steps:
- Upload the above script to a location you can reference in your site (like possibly the Site Assets Document Library)
- Create a Web Part Page.
- Add the various Web Parts onto the page that you want to appear in the tabbed view.
- Add a Content Editor Web Part to the page.
- Edit the above script so that the call to the “HillbillyTabs”function contains the titles of the Web Parts you wish to appear in the tabbed view. For instance. If you had a Web Part with the title “Projects” and another Web Part with the title “Tasks”, the section of script you would change would look like this:
- jQuery(document).ready(function($){
- HillbillyTabs(["Projects","Tasks"]);
- });
- Link the above script to the Content Editor Web Part.
Persisting tabs
One thing you will notice is that the active tab is not persisted should the pagereload. This may not be a big deal, but I know some people find it annoying. To get around this issue I updated the above script to the script below to use cookies to make the currently selected tab stay selected should the user do something that causes the page to reload. Just follow the same instructions as above but replace the script with the one below.
- <!-- Reference the jQueryUI theme's stylesheet on the GoogleCDN. Here we're using the "Start" theme -->
- <link type="text/css" rel="stylesheet"href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/themes/start/jquery-ui.css"/>
- <!-- Reference jQuery on the Google CDN -->
- <script type="text/javascript"src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
- <!-- Reference jQueryUI on the Google CDN -->
- <script type="text/javascript"src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
-
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- $(".s4-wpcell").hide();
- $(".s4-wpcell-plain").hide();
-
-
- HillbillyTabs(["Web Part Title 1","Web Part Title2"]);
-
- ShowActiveTab();
-
- });
-
- functionHillbillyTabs(webPartTitles)
- {
- for(index inwebPartTitles)
- {
- var title =webPartTitles[index];
- $("#HillbillyTabs").append('<li><ahref="#Tab'+index+'" id="TabHead'+index+'"onclick="SetCookie(this.id);">'+
- title+'</a></li>').after('<divid="Tab'+index+'"></div>');
- $("span:contains('"+title+"')").each(function(){
- if($(this).text() == title){
- varwebPart = $(this).hide().closest("span").closest("[id^='MSOZoneCell_WebPart']");
- if($(webPart).contents().html() != undefined)
- {
- webPart = $(webPart).contents();
- }
- $("#Tab" + index).append((webPart));
- }});
- }
- $("#tabsContainer").tabs();
- $(".s4-wpcell").show();
- $(".s4-wpcell-plain").show();
-
- }
-
-
-
- function SetCookie(id)
- {
- var date = newDate();
-
- date.setTime(date.getTime()+(300*1000));
- var expires ="; expires="+date.toGMTString();
- document.cookie= "ActiveTab="+id+expires+"; path=/";
- }
-
- functionShowActiveTab()
- {
- var name ="ActiveTab";
- var cookieArray =document.cookie.split(";");
- for (index incookieArray)
- {
- varkeyValuePair = cookieArray[index].split("=");
- var key =keyValuePair[0];
- key = key.replace(/^\s+|\s+$/g, "");
- if (key ==name)
- {
- var value= keyValuePair[1];
- $("#" + value).click();
- return;
- }
- }
- }
-
-
- </script>
- <div id="tabsContainer"><ulid="HillbillyTabs"></ul></div>
“Breaking Layouts” orSome Web Parts not appearing properly
A few people have indicated that they have a problem with the “layouts breaking” or a web part not being displayed properly for some web parts in a tab, I’ve been able to track this down to scripts executing after jQuery.ready() to do things like place items in calendars. To alleviate this problem I added a delay to the execution of the tabs functionality to give these other scripts time to execute. If you are having a problem with shifting layouts, try using the below script. The script delays for 800ms, if you find this id not long enough try increasing this value in the “setTimeout” function call below. I’m not enamored with this solution, and if a better solution comes to mind I’ll be sure to let you know!