Taming the jumpy categorized Views in XPages
I am not a fan of categorized views in web applications and have suggested alternative uses before. However beautiy is in the eye of the beholder, so categorized views are used. When a category is rendered it sits on its own tablerow (tr) in a tabletag (td) that has a colspan attribute. This effectively disables the column width settings for your view until you expand the category to show a full row of data resulting in some "screen jumping". While that won't sink the Titanic it is a little odd. Short of rolling your own rendering using a repeat control, there is a fix consisting of 2 JavaScript functions (one for the server, one for the client and a little addition in the source code panel. This works in all categorized views. However when you have a column with a column total rendering might be different since XPages doesn't render the colspan attribute then. Lets get started. Open your source pane and locate the view panel and the xp:this.facets. Here you insert a scriptBlock like this:
(Replace { } with < and > except inside the CDATA section). Then you add 2 JavaScript libraries to your XPage:
The Client Side JS:
As usual: YMMV
-
{xp:this.facets}
-
{xp:pager partialRefresh="true" layout="Previous Group Next"
-
xp:key="headerPager" id="pager1"}
-
{/xp:pager}
-
{xp:scriptBlock xp:key="south" id="scriptBlock1"}
-
{xp:this.value} {![CDATA[#{javascript:return getClientJSforTableLayout("viewPanel1");} ] ] } {/xp:this.value}
-
{/xp:scriptBlock}
-
{/xp:this.facets}
CatViewPatches.js
and TableSupportFunctions.jss
. They will fix the column width. One caveat: you have to specify the width for all columns otherwise the unspecified columns get 100/{number-of-columns}% width assigned.The ServerSide JS:
-
function getClientJSforTableLayout (tableID ) {
-
/**
-
* This code has as result JavaScript source code that is send down to the client]
-
*/
-
var curTable = getComponent ( "viewPanel1" ) ;
-
var curID = getClientId ( "viewPanel1" ) ;
-
var tablewidth = extractWidthFromStyle (curTable. viewStyle ) ;
-
var result = "fixCatTableWidth(\"" ; // This holds the JavaScript we return to the browser
-
result += curID + "\",\"" ;
-
// Here we compute the sizeString
-
-
var kids = curTable. getChildren ( ) ;
-
var defaultPercent = Math. floor ( 100 /kids. size ( ) ) + "% " ; // You have to love flexi data types
-
-
for ( var x in kids ) {
-
var styleCandidate = x. style ;
-
if (styleCandidate == null ) {
-
result += defaultPercent ;
-
} else {
-
var candidate2 = extractWidthFromStyle (styleCandidate ) ;
-
if (candidate2 == "" ) {
-
result += defaultPercent ;
-
} else {
-
result += candidate2 ;
-
}
-
}
-
-
result += " " ;
-
-
}
-
-
result += "\",\"" +tablewidth + "\");" ;
-
return result ;
-
}
-
-
function extractWidthFromStyle (styleCandidate ) {
-
var result = "" ;
-
var whereisWidth = styleCandidate. indexOf ( "width:" ) ;
-
if (whereisWidth < 0 ) {
-
result = "" ;
-
} else {
-
var workString = styleCandidate. substr (whereisWidth + 6 ) ;
-
var hasSemiColon = workString. indexOf ( ";" ) ;
-
if (hasSemiColon < 0 ) {
-
result = workString ;
-
} else {
-
result = workString. substr ( 0 ,hasSemiColon ) ;
-
}
-
}
-
return result ;
-
}
-
function fixCatTableWidth (tableID , sizeString , tableSize ) {
-
// Reset the outer table width
-
var outer = dojo. byId (tableID + "_OUTER_TABLE" ) ;
-
if (tableSize == null || tableSize == "" ) {
-
outer. style. width = "100%" ;
-
} else {
-
outer. style. width = tableSize ;
-
}
-
var newWidth = sizeString. split ( " " ) ;
-
var max = newWidth. length ;
-
// Get the table and all header elements
-
var table = dojo. byId (tableID ) ;
-
var allHeaders = table. getElementsByTagName ( "th" ) ;
-
var maxHeaders = allHeaders. length ;
-
for (i = 0 ;i <maxHeaders ;i ++ ) {
-
var curWidth = (i > max ) ? newWidth [max ] : newWidth [i ] ;
-
allHeaders [i ]. style. width = curWidth ;
-
}
-
}
Posted by Stephan H Wissel on 29 June 2010 | Comments (6) | categories: XPages