Free Code Samples
- Center an item on a page
- Sort an ordered list by any element
-
-
- var viewportwidth;
- var viewportheight;
- // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
- if (typeof window.innerWidth != "undefined") {
- viewportwidth = window.innerWidth,
- viewportheight = window.innerHeight
- }
- // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
- else if (typeof document.documentElement != "undefined:
- && typeof document.documentElement.clientWidth !=
- "undefined" && document.documentElement.clientWidth != 0) {
- viewportwidth = document.documentElement.clientWidth,
- viewportheight = document.documentElement.clientHeight
- }
- // older versions of IE
- else {
- viewportwidth = document.getElementsByTagName("body")<0>.clientWidth,
- viewportheight = document.getElementsByTagName("body")<0>.clientHeight
- }
- viewportheight = viewportheight - 330;
- viewportwidth = viewportwidth - 330;
- var _top = viewportheight / 2;
- var _wid = viewportwidth / 2;
Now let's use it in the code:
$("#centeredDiv:).animate({top : _top + "px", left : _wid + "px", position : "absolute"}, 500); -
How many times have you created a table, and then seen it grow and needed to be able to sort through it? Rather than reload the page and do the work in the back end, I decided that I needed a fast way to do it with jQuery. While the building blocks were there, the examples all seemed to show how to sort one column. I've NEVER created a one column table, so I used an ordered list and came up with a table that I could sort by any column I wanted
First, we need a list:
//We'll make a header seperate from the actual list - I'm not going to show the whole HTML here, you'll get the idea- < ul class="_header">
- < li class="firstName w150">
- First Name
- </li>
- < li class="lastName w150">
- Last Name
- </li>
- < li class="phone w150">
- Phone
- </li>
- < li class="state w50">
- State
- </li>
- </ul>
- < ol id="tblContacts">
- < li>
- < ul>
- < li class="w150">
- Steve
- </li>
- < li class="w150">
- Johnson
- </li>
- < li class="w150">
- (407)555-1212
- </li>
- < li>
- Florida
- </li>
- </ul>
- </li>
- </ul>
//What we would like to do is click the header and sort on that column. Fortunately, jQuery has done most of the work for us, they just didn't do a real good job of documenting it:
- $(document).ready(function(){
- $('.firstName').click(function(){
- $('#tblContacts').html(
- $('#tblContacts').children('li').sort(function(a,b) {
- return a.children<0>.children<0>.innerHTML.trim().toUpperCase().localeCompare(
- b.children<0>.children<0>.innerHTML.trim().toUpperCase()
- );
- })
- );
- });
-
- $('.lastName').click(function () {
- $('#tblContacts').html(
- $('#tblContacts').children('li').sort(function (a, b) {
- return a.children<0>.children<1>.innerHTML.trim().toUpperCase().localeCompare(
- b.children<0>.children<1>.innerHTML.trim().toUpperCase()
- );
- })
- );
- });
-
- $('.phone').click(function () {
- $('#tblContacts').html(
- $('#tblContacts').children('li').sort(function (a, b) {
- return a.children<0>.children<2>.innerHTML.trim().toUpperCase().localeCompare(
- b.children<0>.children<2>.innerHTML.trim().toUpperCase()
- );
- })
- );
- });
-
- $('.state').click(function () {
- $('#tblContacts').html(
- $('#tblContacts').children('li').sort(function (a, b) {
- return a.children<0>.children<3>.innerHTML.trim().toUpperCase().localeCompare(
- b.children<0>.children<3>.innerHTML.trim().toUpperCase()
- );
- })
- );
- });
- });
// And let's make it LOOK like a table, with a little CSS
- #tblContacts
- {
- width:750px;
- }
- #tblContacts li
- {
- list-style-type: none;
- padding:0;
- }
- #tblContacts li ul li
- {
- list-style:none;
- display:inline-block;
- padding:0;
- }
- ._header
- {
- width:750px;
- background-color:#e4dede;
- }
- ._header li
- {
- display:inline-block;
- }
- .w150
- {
- min-width:150px;
- }
- .w50
- {
- min-width:150px;
- }
Obviously, there is a lot of room for compression here, but let's look at what we're doing:
We've created a header, and we've sectioned out the list items by 150 pixels. Right under that list, we've created another one, sectioned out the same way
We've named each header item using a class (named after the contents, for ease of use), and we've written a function for each item in jQuery.
When you click the header item, it sorts each list item in the ordered list. Using the .sort(function(a, b) tells jQuery to do a comparator of the contents of each selected list item against the whole
That works great with a list of one item, but who is ever going to have that? Instead, we'll tell jQuery to compare the first child of a (a.children<0>, or in this case, the < ul >), then grab the list item of THAT child that we want (0 for First Name, 1 for Last Name, etc), trim it, convert it to UpperCase, and then do the same for the rest of the matching list items in the whole list, using localeCompare.
Doing the whole thing within the .html() element for the ordered list maintains the list when we sort the columns!
For a finished product of: (click the headers to reorder the list)
- First Name
- Last Name
- Phone
- State
-
- Steve
- Johnson
- (407)555-1212
- Florida
-
- Dave
- Smith
- (969)555-1212
- California
-
- Roscoe
- Edderman
- (865)555-1212
- Tennessee
-
- Chauncy
- Ghellar
- (203)555-1212
- Connecticut
-