Quantcast
Channel: Second Life of a Hungarian SharePoint Geek
Viewing all articles
Browse latest Browse all 206

Creating resizable Columns in SharePoint Views

$
0
0

Recently I was experimenting with solutions that would enable users to resize the width of the columns of a SharePoint view. As you might know, SharePoint calculates the width of columns automatically, and the result is not always optimal. Although it’s relative easy to set the width using SharePoint Designer I was looking for a method that one can use through the web UI, without any external applications.

One of the methods I’ve tried out was to attach a keyboard event handler to the div element of the field header caption. I chose the div, and not the parent th element, because other event handlers were already registered for th, and I didn’t want to interfere with them. When the field caption has the focus (see image below), and the user presses Shift + Left arrow keys, the width of the column is decreased. When the user presses Shift + Right arrow keys, the width of the column is increased.

image

I used jQuery and the following script (injected through the Content Editor Web Part into the view page) to achieve the goal:

Code Snippet
  1. <script src="/_layouts/adesso/jQuery/jquery-1.8.3.min.js"></script>
  2.  
  3. <script language="ecmascript" type="text/ecmascript">
  4.  
  5. $(document).ready(startScript);
  6.  
  7. function startScript() {
  8.  
  9.     $('div.ms-vh-div').keydown(function (e) {
  10.         if (e.keyCode != 16) {
  11.             var widthChange = 20;
  12.             var th = $(e.srcElement).closest('th');
  13.             var widthTh = $(th).width();
  14.             if (e.keyCode == 37) {
  15.                 $(th).width(widthTh – widthChange);
  16.             }
  17.             else if (e.keyCode == 39) {
  18.                 $(th).width(widthTh + widthChange);
  19.             }
  20.         }
  21.  
  22.     });
  23. }
  24.  
  25. </script>

The result was quite promising, however it has a definitive shortage as well: the settings are not persisted, so the user has to repeat the resizing each time. Theoretically we could store the customized widths in cookies or in SharePoint lists (per user), and reapply the settings on each page load.



Viewing all articles
Browse latest Browse all 206

Trending Articles