A very common request for changes to SharePoint list views is how to set the column width. This is not possible to do using the ootb "List Settings", and the common suggested fix is to use SharePoint Designer (SPD) and convert the view into an "XSLT Data View": How can I manage columns widths in list views? Most large companies do, however, prevent the use of SPD.
With jQuery there is no need to use SPD or to convert the view. In the following example jQuery will change the width of the two columns "Status description" and "Type of Work" by changing their CSS style attribute.
Start by looking up the HTML markup for the two table headers using View-Source. I'm using jQuery filters that look for the TH elements using a CSS class selector and a content filter. Then add a Content Editor web-part (CEWP) to your page and enter this script in the source editor:
<!--ADJUST TABLE COLUMN WIDTH-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("TH.ms-vh2-nograd:contains('Status description')").css("width", "150px");
$("TH.ms-vb:contains('Type of Work')").css("width", "150px");
});
</script>
Note that not all column header text containers are TH elements, that the CSS class name varies by column type and that the jQuery contains(text) selector content filter is case-sensitive. I've also used the shorthand to the $(document).ready() function in the script.
You can also speed up the selector by explicitly specifying the jQuery context, such as the id of an enclosing table DOM element - typically "#MSO_ContentTable". Thanks to Paul Grenier for this tip over at the EndUserSharePoint cross-posting. Also consider using the :first filter to stop looking for elements after finding the first matching column.
You can be even more specific and have different layouts for different views of the list, as the id of the inner table representing the view is the combination of the GUIDs for the list and the view. The jQuery context would then apply to a specific view. I would recommend using the SharePoint JavaScript ContextInfo object for the view to get the GUIDs using the ctx.listName and ctx.view properties.
Next time, I'll show how to create a frozen header row that will stay fixed even when scrolling.
Wednesday, June 10, 2009
Subscribe to:
Post Comments (Atom)

9 comments:
Very nicely done! I'd like to cross-post this and your next article on freezing column headers at EndUserSharePoint.com, if you don't mind.
Regards,
Mark
Mark Miller
Founder and Editor,
EndUserSharePoint.com
Hi Mark
As I love the stuff found at EndUserSharePoint.com, cross-posting is OK with me.
Innovation through sharing, and flexibility and power to the users is our goal.
Posted. http://www.endusersharepoint.com/?p=1743
I hope you'll consider writing a series for us. Thanks. -- Mark
Very useful find.
It will be veryuseful for demanding clients :)
How do you get this to work for a document library? We have long filenames and want to shorten the name column in a particular lib. It appears that each column is defined by a TH wrapping a DIV wrapping a TABLE which has width:100% and DisplayName="Name". Not sure if my jquery selector is wrong, or if this just won't work.
Yes, some of the columns have complicated innerHtml, such as choice columns that use TD and not TH. So most likely your selector is wrong: e.g. if the shown header is not a text node, then you must make the selector filter match against something else.
You can also use a parent child selector that avoids knowing the CSS class of the column:
$("tr.ms-viewheadertr th:contains('Type of Work')")
It doesn't matter that some inner element har width=100% it will just use 100% of the width of its container element.
There are also some useful jQuery debugger to be found, just google and chose.
Being a novice I’m unlikely to modify column width in this manner. Is there a simple explanation for what determines width when I import a spread sheet? For sure something happens in the process… not all similar lists I import look alike. What causes one text field to be different from another?
The column width is by default set to auto when there is no explicit width settings. In auto mode, the total width of the table combined with the number of columns and the size/word-wrapping of the content inside the column cells, will determine the distribution - in an unpredicable manner. That is just the way HTML tables work.
Brilliant! Excellent solution! Thanks so much.
Post a Comment