質問

I have the following JS script to color code our custom list items inside out list view, by checking if the item is status does not equal Delivered and its EndDate has been bypassed:-

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
       function init() {
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
          Templates: {},
          OnPostRender: function(ctx) {
           var rows = ctx.ListData.Row;
           for (var i=0;i<rows.length;i++)
           {
              var currentDate = new Date();
              var enddate = rows[i]["EnDDate"];
              var status = rows[i]["OrderStatus"];
              var rowId = GenerateIIDForListItem(ctx, rows[i]);
              var row = document.getElementById(rowId); 
              if(row != null) {
                  if(status != 'Delivered' && enddate  && enddate < currentDate
                  )
                  {
                 row.style.backgroundColor = '#FA8072';
                 row.style.fontWeight = "bold";
                  }

now the above will not color code anything. the problem i think is that our EndDate field value will be return as follow 28/09/2018 while the currentDate will be retruned in the follow format Tue Oct 09 2018 15:09:11 GMT+0100 (GMT Daylight Time) .. so i think this will make my comparison enddate < currentDate return unexpected results! so can anyone advice on this please?

役に立ちましたか?

解決

All you need to do is make your enddate variable a Date object that is based on the field value, like this:

var enddate = new Date(rows[i]["EnDDate"]); // is that really two capital Ds, or is that a typo?

I also see though that you are checking that enddate has a value, which means you are expecting that it might be blank? In which case you might want to do something like:

var enddate = rows[i]["EnDDate"];
if (enddate) {
    enddate = new Date(enddate);
}

// and then later keep the same validation check:
if (status != 'Delivered' && enddate && enddate < currentDate)

If you are using a European format (day/month/year) you will need to swap the day and month, and if you are moving things around you might as well call the Date constructor with year, month, date, but if you do that you also have to keep in mind that in Javascript, months are zero-based. So, if you are absolutely certain that the separators will be slashes, you could do:

var enddate = rows[i]["EnDDate"];
if (enddate) {
    var dateParts = enddate.split('/');
    enddate = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]); // subtract one from the month because in code it is zero-based
}

If you are not absolutely certain that your separators will be slashes, you will need more complex logic to separate the parts and swap things around.

ライセンス: CC-BY-SA帰属
所属していません sharepoint.stackexchange
scroll top