1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| *****************************************************
* List View Managed Metadata Priority Color Sample *
* Authors: Rob Lempens + Wouter Laureys (Spikes NV) *
*****************************************************/
(function () {
(window.jQuery || document.write('<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-1.10.0.min.js"><\/script>'));
// Create object that has the context information about the field that we want to change it's output render
var priorityFilledContext = {};
priorityFilledContext.Templates = {};
priorityFilledContext.Templates.Fields = {
// Apply the new rendering for Priority field on List View
"ColoredPriority": { "View": priorityFilledTemplate}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(priorityFilledContext);
})();
var handledPriorities = [];
function priorityFilledTemplate(ctx) {
var priority = ctx.CurrentItem[ctx.CurrentFieldSchema.Name].Label;
var className = "priorityClass" + priority;
// The color needs only to be set once for each term (class)
if (handledPriorities.indexOf(priority) == -1) {
// Getting the color from the custom taxonomy property and set it for the class.
setTaxonomyColor(ctx.CurrentItem[ctx.CurrentFieldSchema.Name], className);
handledPriorities.push(priority);
}
return "<span class='"+className+"'>" + priority + "</span>";
}
function setTaxonomyColor(valueObject, className) {
if (valueObject && valueObject.__type == "TaxonomyFieldValue:#Microsoft.SharePoint.Taxonomy") {
var context = SP.ClientContext.get_current();
var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStore = session.getDefaultSiteCollectionTermStore();
var term = termStore.getTerm(valueObject.TermID);
context.load(session);
context.load(termStore);
context.load(term, 'IsRoot', 'Id', 'Name', 'Parent', 'CustomProperties');
context.executeQueryAsync(success, fail);
function success() {
var color = term.get_objectData().get_properties()["CustomProperties"]["Color"] != undefined ? term.get_objectData().get_properties()["CustomProperties"]["Color"] : "";
$("." + className).css('color', color);
}
function fail() {
alert("Oops...");
}
}
} |
Partager