Implementation Of Jquery Accordion For SharePoint List Data

Let’s get started.

Basically, an accordion is a panel, which displays collapsible content for presenting information in a limited amount of space

HTML code given below along with jQuery is used to display the accordion.

  1. <!doctype html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>jQuery UI Accordion Example </title>  
  7.     <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  8.     <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  9.     <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  10.     <script>  
  11.         $(function() {  
  12.             setdata()  
  13.             $("#accordion-1").accordion({  
  14.                 heightStyle: "content"  
  15.             });  
  16.         });  
  17.     </script>  
  18.     <style>  
  19.         #accordion-1 {  
  20.             font-size: 14px;  
  21.         }  
  22.     </style>  
  23. </head>  
  24.   
  25. <body>  
  26.     <div id="accordion-1">  
  27.         <h3>Tab 1</h3>  
  28.         <div>  
  29.             <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>  
  30.         </div>  
  31.         <h3>Tab 2</h3>  
  32.         <div>  
  33.             <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>  
  34.         </div>  
  35.         <h3>Tab 3</h3>  
  36.         <div>  
  37.             <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>  
  38.         </div>  
  39.     </div>  
  40. </body>  
  41.   
  42. </html>  

In the above code we are loading two jquery files(jquery.js and jquery-ui.js) and one css file which is related jquery ui, the html file when opened in browser will be like below,


Each of the divisions can be collapsible and when opened, it displays the content present within it.

Implementing Accordion in Sharepoint

We can implement accordion for Sharepoint list and the list should have two columns, where one is a single line of text for holding heading and other is multiline text for the content.

The picture given below shows sample data, which we will create for the list.


The list given above shows the countries in the title column and the description column will have the content to be displayed.

The code given below is used to display the AccordionList data in jQuery accordion format.

  1. <!doctype html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>jQuery UI Accordion Example </title>  
  7.     <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  8.     <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  9.     <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  10.     <script type="text/javascript">  
  11.         $(function() {  
  12.             setdata();  
  13.             $("#accordion-1").accordion({  
  14.                 heightStyle: "Content",  
  15.                 collapsible: true  
  16.             });  
  17.         });  
  18.   
  19.         function setdata() {  
  20.             SP.SOD.executeFunc('sp.js''SP.ClientContext', Test);  
  21.         }  
  22.   
  23.         function Test() {  
  24.             $("#India").empty();  
  25.             $("#Australia").empty();  
  26.             $("#England").empty();  
  27.             var clientcontext = new SP.ClientContext();  
  28.             var web = clientcontext.get_web();  
  29.             var list = web.get_lists().getByTitle('AccordionList');  
  30.             listitems = list.getItems(SP.CamlQuery.createAllItemsQuery());  
  31.             clientcontext.load(listitems);  
  32.             clientcontext.executeQueryAsync(success, failure);  
  33.         }  
  34.   
  35.         function success() {  
  36.             var listitemenumerator = listitems.getEnumerator();  
  37.             while (listitemenumerator.moveNext()) {  
  38.                 var item = listitemenumerator.get_current().get_item('Title');  
  39.                 var description = listitemenumerator.get_current().get_item('Description');  
  40.                 if (item == "India") {  
  41.                     $("#India").append("<p>" + description + "</p>");  
  42.                 } else if (item == "Australia") {  
  43.                     $("#Australia").append("<p>" + description + "</p>");  
  44.                 } else {  
  45.                     $("#England").append("<p>" + description + "</p>");  
  46.                 }  
  47.             }  
  48.         }  
  49.   
  50.         function failure() {  
  51.             alert("request failed" + args.get_message());  
  52.         }  
  53.     </script>  
  54.     <style>  
  55.         #accordion-1 {  
  56.             font-size: 20px;  
  57.         }  
  58.     </style>  
  59. </head>  
  60.   
  61. <body>  
  62.     <div id="accordion-1">  
  63.         <h3>About India</h3>  
  64.         <div id="India"> </div>  
  65.         <h3>About Australia </h3>  
  66.         <div id="Australia"> </div>  
  67.         <h3>About England</h3>  
  68.         <div id="England"> </div>  
  69.     </div>  
  70. </body>  
  71.   
  72. </html>  

Summary of the code

  • The function setdata will load all the required JS files for Sharepoint client side object model, fetch the current context of Sharepoint site and get the required list, all the items in the list are obtained.
  • The Success function will get each item, using itemenumerator and for each item, we are checking the title with the predefined values (like India, Australia, England) if the title equal the value, then we are binding the respective div with the description value of that particular item.
  • After completion of Success function, accordion function is called on the main div tag ( just after body tag), which displays the div in the accordion format.

The figure given below shows how accordion is displayed.

When First div is opened, the screenshot will be displayed, as shown below.


When Second div is opened, the screenshot will be displayed, as shown below.



When Third div is opened, the screenshot will be displayed, as shown below.



The code can be tweaked, as per various requirements. This is a just a simple implementation.