Cauvery Vr.

Cauvery Vr.

  • NA
  • 40
  • 7.7k

Sticky columns when scrolling right

Apr 17 2017 2:18 AM
The code in the below link works fine to display sticky Column headings
 
http://www.enjoysharepoint.com/Articles/Details/steps-to-freeze-header-row-for-title-in-sharepoint-2013-21223.aspx
 
I modified this code such that it displays the first column when i scroll to the right. But it is not working properly.
 
i have attached the resulting image and below is the modified javascript code.
 
Kindly please help.
 
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript">  
  2. </script>  
  3. <script type="text/javascript">  
  4.   
  5. $(document).ready(function(){  
  6. stickyHeaders()  
  7. })  
  8.   
  9. function stickyHeaders(){  
  10.     if( jQuery.inArray( "spgantt.js", g_spPreFetchKeys ) > -1)  
  11.     {  
  12.         SP.SOD.executeOrDelayUntilScriptLoaded(function ()   
  13.         {  
  14.             findListsOnPage();  
  15.         }, "spgantt.js");  
  16.     } else {  
  17.         findListsOnPage();  
  18.     }  
  19.     $(window).bind('hashchange', findListsOnPage);  
  20. }  
  21.   
  22. function findListsOnPage() {  
  23.     var lists          = $('.ms-listviewtable')  
  24.     var quickEditLists = [];  
  25.     var listViews      = [];  
  26.   
  27.     $(lists).each(function(i){  
  28.         if($(this).find('div[id^="spgridcontainer"]').length > 0 )  
  29.         {  
  30.             quickEditLists.push($(this))  
  31.         } else if( $(this).hasClass("ms-listviewgrid") == false )   
  32.         {  
  33.             listViews.push($(this))  
  34.         }  
  35.     })  
  36.   
  37.     if(quickEditLists.length > 0)   
  38.     {  
  39.         SP.GanttControl.WaitForGanttCreation(function (ganttChart) {  
  40.             initializeStickyHeaders(quickEditLists, "qe");  
  41.         });  
  42.     }  
  43.   
  44.     if(listViews.length > 0)   
  45.     {  
  46.         initializeStickyHeaders(listViews, "lv");  
  47.     }  
  48. }  
  49.   
  50. function initializeStickyHeaders (lists, type) {  
  51.     var top_old        = [], top_new        = [],  
  52.     bottom_old     = [], bottom_new     = [],  
  53.     stickies       = [], headers        = [],  
  54.     indexOffset    = 0 ;  
  55.   
  56.     var style = "position:fixed;" +  
  57.     "left:50px;" +  
  58.     "z-index:1;" +  
  59.     "background-color:beige;" +  
  60.     "box-shadow:3px 3px 5px #DDDDDD;" +  
  61.     "display:none"  
  62.   
  63.     $(window).unbind('resize.' + type);  
  64.     $(window).bind  ('resize.' + type, updatestickies );  
  65.   
  66.     $('#s4-workspace').unbind('scroll.' + type);  
  67.     $('#s4-workspace').bind  ('scroll.' + type, updatestickies );  
  68.   
  69.     $(lists).each(function()  
  70.     {  
  71.         headers.push($(this).find($('.ms-listviewtable tbody tr td:nth-child(2)')))  
  72.         //alert(headers.length);  
  73.     });  
  74.   
  75.     $(headers).each(function (i)   
  76.     {  
  77.         var table = $(this).closest("table");  
  78.         if(table.find("tbody > tr").length > 1)   
  79.         {  
  80.   
  81.             table.parent().find(".sticky-anchor").remove()  
  82.             table.parent().find(".sticky").remove()          
  83.   
  84.             var anchor = table.before('<div class="sticky-anchor"></div>')  
  85.             stickies.push($(this).clone(true,true).addClass("sticky").attr('style', style).insertAfter(anchor))  
  86.   
  87.             var tbodies = $(this).parent("thead").siblings("tbody")  
  88.             if(tbodies.length > 1)   
  89.             {  
  90.                 tbodies.bind("DOMAttrModified"function(){  
  91.                     setTimeout(function(){  
  92.                         $('#s4-workspace').trigger("scroll"true)  
  93.                     }, 250)  
  94.                 })  
  95.             }  
  96.         } else {  
  97.             headers.splice(i-indexOffset,1)  
  98.             indexOffset++;  
  99.         }  
  100.     })  
  101.   
  102.     //Do it once even without beeing triggered by an event  
  103.     updatestickies();  
  104.   
  105.     function updatestickies (event, DOMchangeEvent)   
  106.     {  
  107.         $(headers).each(function (i) {  
  108.             if(DOMchangeEvent)   
  109.             {  
  110.                 width();  
  111.                 return false;  
  112.             }  
  113.   
  114.             function width()   
  115.             {  
  116.                 stickies[i].height(headers[i].height()).find('td:nth-child(2)').each(function () {  
  117.                     $(this).height(headers[i].find('td:nth-child(2)').height())  
  118.                 })  
  119.             }  
  120.   
  121.             top_old[i]    = top_new[i]  
  122.             top_new[i]    = Math.round($(this).offset().left - 45)  
  123.             bottom_old[i] = bottom_new[i]  
  124.             bottom_new[i] = Math.round(top_new[i] - 30 + $(this).closest('table').width())  
  125.             var x=1;  
  126.   
  127.             stickies[i].offset({  
  128.                 top: Math.round(headers[i].closest("td").offset().top)   
  129.                   
  130.             });  
  131.   
  132.             if(top_old[i] >= 0 && top_new[i] <= 0 || bottom_old[i] <= 0 && bottom_new[i] >= 0 || top_old[i] === undefined && bottom_old[i] === undefined&& top_new[i] < 0 && bottom_new[i] > 0 )   
  133.             {  
  134.                 width();  
  135.                 stickies[i].fadeIn();  
  136.             }   
  137.             else if (top_old[i] <= 0 && top_new[i] >= 0 || bottom_old[i] >= 0 && bottom_new[i] <= 0 )   
  138.             {  
  139.                 stickies[i].fadeOut();  
  140.             }  
  141.         })  
  142.     }//update stikies  
  143. }//initialize sticky headers  
  144.   
  145.   
  146. </script>