Created
January 12, 2013 19:16
-
-
Save purtuga/4520029 to your computer and use it in GitHub Desktop.
A jQuery addon function for displaying the ID of a List Item in SharePoint on the EDIT (EditForm.aspx) or DISPLAY (DispForm.aspx) pages. See this blog post for more information: http://wp.me/p2kCXW-2u
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @fileOverview A jQuery addon function for displaying the ID of a | |
* List Item in SharePoint on the EDIT (EditForm.aspx) or DISPLAY (DispForm.aspx) pages. | |
* | |
* @author Paul Tavares, www.purtuga.com, paultavares.wordpress.com | |
* @see http://wp.me/p2kCXW-2u | |
* @license Dual License support under MIT or GPL. User can pick whichever one applies | |
* best for their project and does'nt not have to contact me. | |
* | |
*/ | |
;(function($){ | |
"use strict"; | |
/** | |
* Finds the form displayed on the EDIT and DISP pages and displays | |
* the associated ID of the item at the top of that form. | |
* | |
* @param {Object} [options] | |
* @param {String} [options.field="Title"] | |
* The field to use in finding the table. | |
* @param {selector,HTMLElement} [options.field=null] | |
* The table where the ID | |
* should be inserted. If defined on input, field input | |
* param is ignored. | |
* @param {String} [options.itemId=itemId] | |
* The item ID that will be displayed on the table. | |
* Defaults to the ID parameter in the URL | |
* @param {String} [options.idWrapper='<div style="font-weight:bold;font-size:2em;"></div>'] | |
* The HTML that will be used to wrap the ID when it is | |
* inserted into the table/page. | |
* | |
* @return {Object} jQuery | |
* | |
* @example | |
* | |
* $().SPAddIdToFormTable(); | |
* $.SPAddIdToFormTable(); | |
* $().SPAddIdToFormTable({ field: "Name" }); | |
* | |
*/ | |
$.SPAddIdToFormTable = $.fn.SPAddIdToFormTable = function(options){ | |
var opt = $.extend({}, { | |
field: "Title", | |
table: null, | |
itemId: GetUrlKeyValue("ID", false, location.href), | |
idWrapper: '<div style="font-weight:bold;font-size:2em;"></div>' | |
}, | |
options); | |
if (!opt.field && !opt.table) { | |
return; | |
} | |
if (!opt.table) { | |
opt._field = opt.field.toString(); | |
opt.field = $("[title='" + opt._field + "']").closest('tr'); | |
if (!opt.field.length){ | |
opt.field = $("[name='SPBookmark_" + opt._field + "']").closest('tr'); | |
} | |
if (!opt.field.length) { | |
try { | |
console.log("$.fn.SPAddIdToFormTable(ERROR) Unable to locate " + | |
opt._field + " on the page!"); | |
} catch(e) {} | |
return; | |
} | |
opt.table = opt.field.closest("table"); | |
} else if (!opt.field) { | |
opt.field = opt.table.find("tr:first"); | |
} | |
opt.table.find("tr:first").before( | |
opt.field.clone() | |
.css("display", "") | |
.find("td") | |
.eq(0) | |
.find("h3") | |
.empty() | |
.append("ID") | |
.end() | |
.end() | |
.eq(1) | |
.empty() | |
.append( | |
$(opt.idWrapper).wrapInner(opt.itemId) | |
) | |
.end() | |
.end() | |
); | |
return this; | |
}; | |
})(jQuery); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment