0

I am trying to change the row of a table to #0093ff; if the Holidays.HolidayDate is the next upcoming holiday.

NEW CODE:

<table ng-if="model.Holidays" class="table">
                        <tr>
                            <th>Holiday</th>
                            <th>Date</th>
                            <th>Branch</th>
                            <th>Hours</th>
                        </tr>
                        <tr ng-repeat="Holidays in model.Holidays" ng-style="isUpcomingHoliday(Holidays.HolidayDate) ? 'color:#0093ff' : '' ">
                            <td>{{Holidays.HolidayName}}</td>
                            <td>{{Holidays.HolidayDate | fulldaydate}}</td>
                            <td>{{Holidays.Branch ? Holidays.Branch : 'All'}}</td>
                            <td>{{Holidays.Hours ? Holidays.Hours : 'Closed'}}</td>
                        </tr>
                    </table>

JS

.filter('isUpcomingHoliday', ['$filter',
    function ($filter) {
        return function (input) {
            const sortedHolidays = this.model.Holidays.sort((a, b) => {
                return moment(b.date).isAfter(moment(a.date))
            });

            const upcomingHoliday = sortedHolidays[0];

            return moment(holidayDate).isSame(upcomingHoliday);
        };
    }])
1
  • you may need ng-style="nextHol(Holidays.HolidayDate) ? 'background-color:#0093ff' : '' " on your row (tr), with a respective method for checking the next holiday Commented Mar 27, 2018 at 13:31

2 Answers 2

1

You can achieve it by sorting holidays and compare with the first element. I think it can look like this:

isUpcomingHoliday(holidayDate) {
  const sortedHolidays = this.model.Holidays.sort((a, b) => {
                           return new Date(b.HolidayDate) - new Date(a.HolidayDate)  
                         };

  return holidayDate.getTime() === sortedHolidays[0].getTime();
}

<section class="module module-divider-bottom" data-app="global">
            <div class="container" ng-controller="ContactController">
                <div class="container">
                    <table ng-if="model.Holidays" class="table">
                        <tr>
                            <th>Holiday</th>
                            <th>Date</th>
                            <th>Branch</th>
                            <th>Hours</th>
                        </tr>
                        <tr ng-repeat="Holidays in model.Holidays" ng-style="isUpcomingHoliday(Holidays.HolidayDate) ? 'background-color:#0093ff' : '' ">
                            <td>{{Holidays.HolidayName}}</td>
                            <td>{{Holidays.HolidayDate | fulldaydate}}</td>
                            <td>{{Holidays.Branch ? Holidays.Branch : 'All'}}</td>
                            <td>{{Holidays.Hours ? Holidays.Hours : 'Closed'}}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </section>

It could be better and more readable with moment.js:

isUpcomingHoliday(holidayDate) {
  const sortedHolidays = this.model.Holidays.sort((a, b) => {
                           return moment(b.date).isAfter(moment(a.date)) 
                         };
  const upcomingHoliday = sortedHolidays[0];  

  return moment(holidayDate).isSame(upcomingHoliday);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Agreed. moment.js is really helpful when comparing dates. Especially if they aren't of the same format.
@Patryk I have edited my question and tried to implement your suggestion however it is still now highlighting the row in the table of the next upcoming holiday
@Charles I'm not really confident about Angular 1.x but I will try help. Here I got working demo of that mechanism, maybe you can base on it: stackblitz.com/edit/angularjs-cexvck
0

Something like this should do the trick. I didn't know if you already had an api that tells you the next holiday, If you are using moment to check if the date is the same (or if you already have something to compare dates). I chose the class vs ngStyle because you might want to style other things related to that holiday. Hope that helps.

isNextHoliday (holidayDate): boolean {
 // return some function that compares the two dates
}
.some-color {
  background: red;
}
<section class="module module-divider-bottom" data-app="global">
  <div class="container" ng-controller="ContactController">
    <div class="container">
      <table ng-if="model.Holidays" class="table">
        <tr>
          <th>Holiday</th>
          <th>Date</th>
          <th>Branch</th>
          <th>Hours</th>
        </tr>
        <tr ng-repeat="Holidays in model.Holidays">
          <td>{{Holidays.HolidayName}}</td>
          <td [class.some-color]="isNextHoliday(Holidays.HolidayDate)">{{Holidays.HolidayDate | fulldaydate}}</td>
          <td>{{Holidays.Branch ? Holidays.Branch : 'All'}}</td>
          <td>{{Holidays.Hours ? Holidays.Hours : 'Closed'}}</td>
        </tr>
      </table>
    </div>
  </div>
</section>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.