Today I have released onto codeplex a new library, the jQuery UI server controls:
http://jqueryuiserverctls.codeplex.com/
This library is intended to make sure that when you use jQuery controls such as tabs you don't end up with the page rendering without the jQuery styling applied. This happens because the scripts to create things such as tabs doesn't execute until after the page is loaded. This is jarring to the user because they see the tabs appearing like this:
Before the script gets applied.
And then it appears like this:
Whilst you may not notice this when running on localhost, a site like this demonstrates it adequately:
http://en.webdiyer.com/MvcPager/Demo/MsAjaxOrders
If you use this library to create your tabs all that styling is applied on the server and thus your page always appears the way it should when the client first sees it!
How to use it
Normally, if you were using jQuery UI to create the tabs above , here is how it would look:
<script type="text/javascript">
$(function () { $("#nonservertabs").tabs(); });
</script>
<div id="nonservertabs">
<ul>
<li><a href="#nonservertabs-1">Gallery</a></li>
<li><a href="#nonservertabs-2">List</a></li>
</ul>
<div id="nonservertabs-1">
This is the gallery tab
</div>
<div id="nonservertabs-2">
<p>This is the list tab at @DateTime.Now.ToString()</p>
</div>
</div>
Here is how the same code can be created using the jQuery UI server controls:
@Html.JQueryUiTabs(new JQueryUiTabsSettings
{
Items = new List<JQueryUiTab>
{new JQueryUiTab{Title="Gallery"}.SetContent(@<text>This is the gallery tab</text>),
new JQueryUiTab{Title="List"}.SetContent(@<p>This is the list tab at @DateTime.Now.ToString()</p>)}
}).Render()
Which results in this output:
<script type="text/javascript">$(function(){$('#tabs').tabs({selected:0,});});
</script>
<div class="ui-tabs ui-widget ui-widget-content ui-corner-all" id="tabs">
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
<li class="ui-state-default ui-corner-topui-tabs-selected ui-state-active"><a href="#tabs-1">
<span>Gallery</span></a></li><li class="ui-state-default ui-corner-top"><a href="#tabs-2">
<span>List</span></a></li></ul>
<div class="ui-tabs-panel ui-widget-content ui-corner-bottom" id="tabs-1">
This is the gallery tab
</div>
<div class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide" id="tabs-2">
<p>This is the list tab at 25/04/2011 5:28:51 p.m.</p>
</div>
</diV>
As you can see, all the styles required by jQuery are automatically inserted for you, which ensures that the client sees the tabs as you intended without any potential delay caused by script execution.
There are other options you can apply, such as cookies that I will talk about the future blog posts.
Download the library for yourself today and try it out! Would love to get your feedback on how it can be improved.