Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

JSLink Calculated Fields

$
0
0

I am trying to calculate a Days Until Expiration field in a view of a Document Library using JSLink.  I have not found any examples of how to apply JSLink the rendering of a field in a Document Library, and the examples I have used that originally were demoing how to show days remaining until DueDate of a task are not working.

This was originally rendered with a custom DVWP in the old MOSS 2007 environment, and I thought it would be easy to make it work with JS Script but it is not and I am lost.

Scenario:

Document Library with a Custom Content Type of Agreement

Has a field called Agrmt Expiration Date

Add a custom field to the Doc Library called "Remaining", calculated =0

Load JS file as follows to Master Page Gallery -

Javascript Display Template

Target: View

Standalone: Override 

List Template 101

Scope /

Publish Major Version

Script:

(function () {
  var fieldCtx = {};
 
  fieldCtx.Templates = {};
  fieldCtx.Templates.Fields = {
    "Remaining": {
                "View": RemainingFieldViewTemplate
              }
  };
 
  SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldCtx);
})();
 
 
function RemainingFieldViewTemplate(ctx) {
  var _expiryDateValue = ctx.CurrentItem.Agrmt_x0020_Expiration_x0020_Date;  // the value of the exp date field
 
  var today = new Date();  // the current date
  today.setHours(0, 0, 0, 0);  // we just need the date part, so set the time to 00:00:00
 
  var date = new Date(_expiryDateValue);  // convert the due date value to a variable of type Date
 
  var diff = date - today;  // calculate the difference
 
  var noOfDays = diff / (24 * 60 * 60 * 1000);  // normalize the difference to days
 
  return noOfDays;  // return the string that should be displayed

the ONLY thing I changed on this example was to change _dueDateValue = ctx.CurrentItem.DueDate

TO

_expiryDateValue = ctx.CurrentItem.Agrmt_x0020_Expiration_x0020_Date, and referred to the _expiryDateValue var when calculating.  Nothing else is different from the example

Create Web Part attached to a view of that Doc Library that includes the "Remaining" column.

Assign the JS file to the Web Part, and save.

Remaining still shows "0" on the Web Part view, even though Agrmt Expiration Date = 05/01/2017

This was straight out of an extremely simple example and i followed it to the letter in order to apply it to my Doc Library - what have I done wrong?

I would very much love to avoid DVWP customization on this if at all possible, but I must have a dashboard web part that displays days remaining.

Any help would be greatly appreciated.


DWM


Viewing all articles
Browse latest Browse all 7589

Trending Articles