1,804 articles and 15,137 comments as of Sunday, May 15th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Monday, June 1, 2009

jQuery for Everyone: Hourly Summary Web Part

On our Stump the Panel forum, bmix offered this problem:

We’re trying to figure out if there’s any way to show a total of how many people are scheduled for a particular hour of the day.

Well, after refining the problem statement, I put my owsapi to good use. The code below can be placed into a CEWP. You will need jQuery 1.3.1 as well as my owsapi code. Alter the paths to the scripts as needed in the first few lines of the code.

Of course, the CSS styling can be improved on, but this screenshot will give you the basic idea.

The web part will hide the Hourly Summary HTML on any calendar view other than Day-view. On Day view, it pulls the date you are viewing and fetches the data using the owsapi and some dynamic filtering.

UPDATE: Note, if you’re using IE8, I’ve seen problems where the browser cache keeps an old version of the ows xml. You have to clear the cache or change your browser settings for a new file to be retrieved each time the page loads.

Click “Read more…” to see the code. I will also post this web part on the codeplex project examples.

<script type="text/javascript">
if(typeof jQuery=="undefined"){
	document.write("<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js' type='text/javascript'><\/script>");
}
</script>
<script type="text/javascript">
document.write("<script src='/javascripts/ows.js' type='text/javascript'><\/script>");
</script>
<script type="text/javascript">
$(function(){
	var clean = function(str){
        return str.replace(sp," ");
    };

	var getKv = function(){
		var params = window.location.search.substring(1).split("&"),
			kv = {},sp=/%20|\+/g;
		$.each(params,function(i,e){
            var p=e.split("=");
            kv[p[0]]=decodeURIComponent(p[1]);
        });
        return kv;
	};

	var owsObj = function(){
		var obj = $("[id*='ExportToDatabase']").attr("onmenuclick").split(',');
		return obj;
	};

	var owsCalList = function(){
		var obj = owsObj();
		var list = obj[1].replace(/'/g,'');
		return list;
	};

	var owsCalView = function(){
		var obj = owsObj();
		var view = obj[2].replace(/'/g,'');
		return view;
	};

	var _today = function(todayStr){
		var x;
		if (!todayStr){
			x = new Date();
		}else{
			x = new Date(todayStr);
		}
		var m = x.getMonth()+1,
		y = x.getFullYear(),d = x.getDate();
		if (m<10){m="0"+m;}
		if (d<10){d="0"+d;}
		var today = y+'-'+m+'-'+d+' ';
		return today;
	};

	var getHours = function(todayStr){
		var hours={};
		var today = _today(todayStr);
		for (i=0;i<24;i++){
			var z = "",zz="";
			if (i<10){z="0";}
			if (i<9){zz="0";}
			hours[i]=ows.getValues('Calendar','Start Time',['Start Time','<=',today+z+i+":00:00",'End Time','>=',today+zz+(i+1)+":00:00"]);
		}
		return hours;
	};

	var kv = getKv();
	var todayStr = kv.CalendarDate;
	if (kv.CalendarPeriod=='day'){
		owsapi({ctx:"Calendar",list:owsCalList(),view:owsCalView()}, function(o){
			var hours = getHours(todayStr);
			var td = $("#insertHours tr:first td");
			$.each(hours,function(i,e){
				$(td[i]).text(e.length);
			});
		});
	}else{
		$("#insertHours").hide();
	}
});
</script>
<style type="text/css">
	#insertHours, #insertHours td {
		border-color: #000;
		border-style:solid;
		border-width:1px;
		text-align:center;
		padding:5px;
	}
</style>
<div>
	<table id="insertHours">
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
		</tr>
		<tr>
			<td>12:00<br>AM</td>
			<td>1:00</td>
			<td>2:00</td>
			<td>3:00</td>
			<td>4:00</td>
			<td>5:00</td>
			<td>6:00</td>
			<td>7:00<br>AM</td>
			<td>8:00</td>
			<td>9:00</td>
			<td>10:00</td>
			<td>11:00</td>
			<td>12:00<br>PM</td>
			<td>1:00</td>
			<td>2:00</td>
			<td>3:00</td>
			<td>4:00</td>
			<td>5:00</td>
			<td>6:00</td>
			<td>7:00<br>PM</td>
			<td>8:00</td>
			<td>9:00</td>
			<td>10:00</td>
			<td>11:00<br>PM</td>
		</tr>
	</table>
</div>

Paul Grenier

View all entries in this series: PaulGrenier-JQuery for Everyone»
Entries in this series:
  1. JQuery for Everyone: Accordion Left Nav
  2. JQuery for Everyone: Print (Any) Web Part
  3. JQuery for Everyone: HTML Calculated Column
  4. JQuery for Everyone: Dressing-up Links Pt1
  5. JQuery for Everyone: Dressing-up Links Pt2
  6. JQuery for Everyone: Dressing-up Links Pt3
  7. JQuery for Everyone: Cleaning Windows Pt1
  8. JQuery for Everyone: Cleaning Windows Pt2
  9. JQuery for Everyone: Fixing the Gantt View
  10. JQuery for Everyone: Dynamically Sizing Excel Web Parts
  11. JQuery for Everyone: Manually Resizing Web Parts
  12. JQuery for Everyone: Total Calculated Columns
  13. JQuery for Everyone: Total of Time Differences
  14. JQuery for Everyone: Fixing Configured Web Part Height
  15. JQuery for Everyone: Expand/Collapse All Groups
  16. JQuery for Everyone: Preview Pane for Multiple Lists
  17. JQuery for Everyone: Preview Pane for Calendar View
  18. JQuery for Everyone: Degrading Dynamic Script Loader
  19. JQuery for Everyone: Force Checkout
  20. JQuery for Everyone: Replacing [Today]
  21. JQuery for Everyone: Whether They Want It Or Not
  22. JQuery for Everyone: Linking the Attachment Icon
  23. JQuery for Everyone: Aspect-Oriented Programming with jQuery
  24. JQuery for Everyone: AOP in Action - loadTip Gone Wild
  25. JQuery for Everyone: Wiki Outbound Links
  26. JQuery for Everyone: Collapse Text in List View
  27. JQuery for Everyone: AOP in Action - Clone List Header
  28. JQuery for Everyone: $.grep and calcHTML Revisited
  29. JQuery for Everyone: Evolution of the Preview
  30. JQuery for Everyone: Create a Client-Side Object Model
  31. JQuery for Everyone: Print (Any) Web Part(s) Plugin
  32. JQuery for Everyone: Minimal AOP and Elegant Modularity
  33. JQuery for Everyone: Cookies and Plugins
  34. JQuery for Everyone: Live Events vs. AOP
  35. JQuery for Everyone: Live Preview Pane
  36. JQuery for Everyone: Pre-populate Form Fields
  37. JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
  38. Use Firebug in IE
  39. JQuery for Everyone: Extending OWS API for Calculated Columns
  40. JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
  41. JQuery for Everyone: Email a List of People with OWS
  42. JQuery for Everyone: Faster than Document.Ready
  43. jQuery for Everyone: Collapse or Prepopulate Form Fields
  44. jQuery for Everyone: Hourly Summary Web Part
  45. jQuery for Everyone: "Read More..." On a Blog Site
  46. jQuery for Everyone: Slick Speed Test
  47. jQuery for Everyone: The SharePoint Game Changer
  48. JQuery For Everyone: Live LoadTip
 

Please Join the Discussion

3 Responses to “jQuery for Everyone: Hourly Summary Web Part”
  1. Christophe says:

    Will this work with recurring events? I’d say no, as the “Start time” field in recurring events is not the actual start time.

  2. AutoSponge says:

    @Christophe,

    Actually, it does seem to support recurring events to a point (as long as it repeats the same time every day).

    Bmix did not seem to need recurring event support. And you probably well know, expanding recurring events is a major pain in the rear.

  3. AutoSponge says:

    If it’s a problem for bmix, I should be able to update the filter to ignore data with a recurrence node.

    UPDATE:

    I just tested that. By changing the getHours line to filter Recurrence, it ignores those troublesome events.

    hours[i]=ows.getValues('Calendar','Start Time',['Recurrence','==',0,'Start Time','< =',today+z+i+":00:00",'End Time','>=',today+zz+(i+1)+":00:00"]);
    

Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!