In this particular article I am showing data in tiles format.

So now we can display data in any format and have custom paging, sorting, and search.

For this you require a SharePoint site with Events List. The Events list needs to have the following columns.

Also you need a Location List.This Location List has a Title column which we need to refer to in Events List.

I am attaching both the list templates.



Owner is Title Column.

The Project basically contains a search box and a button and below the data is displayed in horizontal format. At the end you can see custom pager.



The Data is sorted on the basis of Title Column; i.e, Owner.

When you enter a key word in a search box and click on “Search Events”, it searches based on Title (Owner) column.



The code has page size 4 which can be changed.


  1. <!DOCTYPEHTMLPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4. <title>Events</title>
  5. <linkrel="stylesheet"type="text/css"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
  6. <scripttype="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js">
  7. </script>
  8. <scriptsrc="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"type="text/javascript">
  9. </script>
  10. <scripttype="text/javascript"src="/_layouts/15/sp.runtime.js">
  11. </script>
  12. <scripttype="text/javascript"src="/_layouts/15/sp.js">
  13. </script>
  14. <styletype="text/css">
  15. .pager {
  16. padding-left: 9px;
  17. /*margin: 20px 0;*/
  18. text-align: center;
  19. list-style: none;
  20. float: left;
  21. }
  22. .boxspace
  23. {
  24. padding:9px;
  25. }
  26. </style>
  27. <scripttype="text/javascript">
  28. var pageIndex = 1; // default page index value
  29. var pageSize = 4;//defualt page size
  30. var nextPagingInfo;
  31. var previousPagingInfo;
  32. var position;
  33. var splistitems;
  34. var searchvalue;
  35. var sflag;
  36. var sortColumn = 'Title';//name if sort column
  37. var listname = 'Events';//name if list
  38. //next click code
  39. function btnnextclick() {
  40. pageIndex = pageIndex + 1;
  41. if (nextPagingInfo) {
  42. position = new SP.ListItemCollectionPosition();
  43. position.set_pagingInfo(nextPagingInfo);
  44. }
  45. else {
  46. position = null;
  47. }
  48. getListItems();
  49. }
  50. //prev click code
  51. function btnprevclick() {
  52. pageIndex = pageIndex - 1;
  53. position = new SP.ListItemCollectionPosition();
  54. position.set_pagingInfo(previousPagingInfo);
  55. getListItems();
  56. }
  57. $(document).ready(function () {
  58. //fetch items from list on page load
  59. getListItems();
  60. });
  61. function getListItems(searchbtnclickflag) {
  62. try {
  63. sflag = searchbtnclickflag;//value will be undefined as search button is not clicked
  64. var context = new SP.ClientContext.get_current();
  65. // Load the web object
  66. this.web = context.get_web();
  67. //Get the list
  68. var list = this.web.get_lists().getByTitle(listname);
  69. var spQuery = new SP.CamlQuery();
  70. //if search button is click then if condition will execute to set the default position of pager
  71. if (sflag == "1") {
  72. spQuery.set_listItemCollectionPosition(undefined);
  73. }
  74. else {
  75. spQuery.set_listItemCollectionPosition(position);
  76. }
  77. //on page load search input box is not created,hence this condition will execute.
  78. if (document.getElementById('search') == null) {
  79. spQuery.set_viewXml("
  80. <View>
  81. <Query>
  82. <OrderBy>
  83. <FieldRef Name='" + sortColumn + "' Ascending='True'/>
  84. </OrderBy>
  85. </Query>" + "
  86. <RowLimit>" + pageSize + "</RowLimit>
  87. </View>");
  88. }
  89. //when search box does not contain any text
  90. elseif (document.getElementById('search').value == "") {
  91. spQuery.set_viewXml("
  92. <View>
  93. <Query>
  94. <OrderBy>
  95. <FieldRef Name='" + sortColumn + "' Ascending='True'/>
  96. </OrderBy>
  97. </Query>" + "
  98. <RowLimit>" + pageSize + "</RowLimit>
  99. </View>");
  100. }
  101. //when search value contains text
  102. else {
  103. searchvalue = document.getElementById('search').value;
  104. spQuery.set_viewXml("
  105. <View>
  106. <Query>
  107. <Where>
  108. <Contains>
  109. <FieldRef Name='Title' />
  110. <Value Type='Text'>" + document.getElementById('search').value + "</Value>
  111. </Contains>
  112. </Where>
  113. <OrderBy>
  114. <FieldRef Name='" + sortColumn +
  115. "' Ascending='True'/>
  116. </OrderBy>
  117. </Query>" + "
  118. <RowLimit>" + pageSize + "</RowLimit>
  119. </View>");
  120. }
  121. splistitems = list.getItems(spQuery);
  122. context.load(splistitems);
  123. context.executeQueryAsync(Function.createDelegate(this,
  124. get_splistitems_onSuccess), Function.createDelegate(this,
  125. get_splistitems_onFailure));
  126. }
  127. catch (e) {
  128. alert("An error occurred while fetching data. Please contact your system administrator.");
  129. }
  130. }
  131. function get_splistitems_onSuccess() {
  132. if (splistitems.get_count() > 0) {
  133. var splistitemCollection = splistitems.getEnumerator();
  134. var table = "
  135. <input type='text'class='form-control' id='search' value='" + searchvalue + "'/>
  136. <br/>
  137. <button id='btnsearch' class='btn btn-default' type='button' onclick='getListItems(1)' value='Search Events'>Search Events</button>
  138. <br/>
  139. <table class='status'>
  140. <tr>";
  141. while (splistitemCollection.moveNext()) {
  142. var listItem = splistitemCollection.get_current();
  143. table += "
  144. <td class='boxspace'>
  145. <div class='well'>
  146. <span class='span2 title boldName id='" + listItem.get_item('ID') + "span1" + "'>" + listItem.get_item('Title') + "</span>
  147. <br/>
  148. <span class='span2 title boldCategory' id='" + listItem.get_item('ID') + "span2" + "'>" + listItem.get_item('StartDate') + "</span>
  149. <br/>
  150. <br/>
  151. <span class='span2 title boldCode' id='" + listItem.get_item('ID') + "span3" + "'>" + listItem.get_item('Location').get_lookupValue() + "</span>
  152. </div>
  153. </a>
  154. </td>";
  155. }
  156. table += "
  157. </tr>
  158. </table>";
  159. tableview.innerHTML = table + "
  160. <br/>
  161. <div class='pager'>
  162. <button id='btnBack' type='button' onclick='btnprevclick()'>"
  163. + "< Back" + "
  164. </button>" +
  165. "
  166. <span id='pageInfo'></span>" +
  167. "
  168. <button id='btnNext' type='button' onclick='btnnextclick()'>Next></button>" +
  169. "
  170. </div>";
  171. if (document.getElementById('search').value == "undefined") {
  172. document.getElementById('search').value = "";
  173. }
  174. managePagerControl();
  175. }
  176. else {
  177. alert("No Data found");
  178. }
  179. }
  180. function managePagerControl() {
  181. if (sflag == "1") {
  182. pageIndex = 1;
  183. }
  184. if (splistitems.get_listItemCollectionPosition()) {
  185. nextPagingInfo = splistitems.get_listItemCollectionPosition().get_pagingInfo();
  186. } else {
  187. nextPagingInfo = null;
  188. }
  189. //The following code line shall add page information between the next and back buttons
  190. $("#pageInfo").html((((pageIndex - 1) * pageSize) + 1) + " - " + ((pageIndex * pageSize) - (pageSize - splistitems.get_count())));
  191. previousPagingInfo = "PagedPrev=TRUE&Paged=TRUE&p_ID=" + splistitems.itemAt(0).get_item('ID') + "&p_" + sortColumn + "=" + encodeURIComponent(splistitems.itemAt(0).get_item(sortColumn));
  192. if (pageIndex <= 1) {
  193. $("#btnBack").attr('disabled', 'disabled');
  194. }
  195. else {
  196. $("#btnBack").removeAttr('disabled');
  197. }
  198. if (nextPagingInfo) {
  199. $("#btnNext").removeAttr('disabled');
  200. }
  201. else {
  202. $("#btnNext").attr('disabled', 'disabled');
  203. }
  204. }
  205. function get_splistitems_onFailure() {
  206. alert("An error occurred while fetching data. Please contact your system administrator."+e);
  207. }
  208. </script>
  209. </head>
  210. <body>
  211. <!--HTML Content-->
  212. <divclass="Status"id="tableview">
  213. </div>
  214. </body>undefined</html>