1

I have one Leave List table with json data.

var app=angular.module('myApp',[])
app.controller('myController', ['$scope', '$http', function ($scope, $http) {
    $scope.init = function () {

$scope.holidayList=[
             {
                'month':'JAN',
                'date':01,
                'day': 'Mon',
                'occasion':'New Year'
            },
             {
                'month':'FEB',
                'date': 03,
                'day': 'Fri',
                'occasion':'Maha Shivaratri'
            }

        ];
}
}])

https://jsfiddle.net/856fysen/2/

Here I need to display multiple leaves in single month,for that I have changed my json like this

$scope.holidayList=[
             {
                'month':'JAN',
                'date':[01,07,20],
                'day': ['Mon','Fri','Sun'],
                'occasion':['New Year','pongal','bhogi']
            },
             {
                'month':'FEB',
                'date': 03,
                'day': 'Fri',
                'occasion':'Maha Shivaratri'
            }

        ];

Is it correct?

krish
  • 1,077
  • 6
  • 24
  • 49
  • 1
    It's correct, but in my opinion it's also hard to display correctly. I would use like this: `[{ 'JAN': [{ 'date': '01', ...}, { 'date': '07', ...}]}]` – peetya Nov 05 '17 at 08:24
  • @peetya no need to specify `month`? – krish Nov 05 '17 at 08:29

3 Answers3

1

I would suggest you to use following format, it will be more convenient.

   [{
      'month': 'JAN',
      'Leaves': [{
          'date': '2017-11-05T08:22:06.750Z',
          'day': 'Mon',
          'occassion': 'some_occasion'
          },{
           'date': '2017-11-05T08:22:06.750Z',
           'day': 'Mon',
           'occasion': 'some_occasion'
         }
       ]
     },{
       'month': 'FEB',
       'Leaves': [{
         'date': '2017-11-05T08:22:06.750Z',
         'day': 'Mon',
         'occasion': 'some_occasion'         
          }, {
         'date': '2017-11-05T08:22:06.750Z',
         'day': 'Mon',
         'occasion': 'some_occasion'
    }
}]

You can also use timestamp in date field and write a filter to separate date & day, timestamp is always recommended in such scenario.

Prabhu Tiwari
  • 109
  • 1
  • 7
1

Your approach is corect.

But consider that the model has to be independent of the implementation and is a good practice to keep it clean and easy to read.

Here is my advice:

1) Do not add model directly to scope. Give it it's own variable so that you can refer to it easier.

2) Do not split the date into it's day/month. Even if they are recursive events, you can add years to your existent fixed dates to find the day-of-week. You can later format your date for display: How to format date in angularjs

'd': Day in month (1-31)

'EEE': Day in Week, (Sun-Sat)

3) Avoid paralel arrays that will complicate your code like:

'date':[01, 07, 20],
'day': ['Mon','Fri','Sun']

4) Do not include presentation instructions in model like levels. The view is responsable to organize the data into levels.

Here is my approach:

var holidayList=[
    {
        date: ['01-01-2016', '02-01-2016'],
        event: 'New Year'
    },
        date: ['13-01-2016', '14-01-2016'],
        month: 'Bhogi'
    },
    {
        date: ['14-01-2016', '17-01-2016'],
        event: 'Thai Pongal'
    },
    {
        date: ['13-02-2016', '14-02-2016'],
        event: 'Maha Shivaratri'
    }
];
$scope.holidayList = holidayList;

The "right" JSON date format

profimedica
  • 2,716
  • 31
  • 41
0

yes, it is correct and well formatted.

Hammed Oyedele
  • 1,030
  • 8
  • 10