Search code examples
google-app-maker

How to display data for a specific month in a table?


I want to know how to display data for a specific month in a table.

There is a table with the column [EmployeeId,Date,StartTime,EndTime]. I want to filter by the month of Date, but I don't know how.

  var AttendanceResult = [];

  // Select employee number and current month data
   var query = app.models.AttendanceMaster.newQuery(); 
   query.filters.EmployeeId._equals = userNumber; // Match employee number
   query.filters.Date._contains = roadMonth;       // Including month

   var records = query.run();  

   for (var i = 0; i < records.length; i++) {
      AttendanceResult.push(records[i]);
   }   

return AttendanceResult;

Since the "query.filters.Date._contains = roadMonth;" filter in the above code is not working, I think there is an error here, Please tell me.

What are some examples of filters?


Solution

  • You can certainly use appmaker's scripting to achieve this. Building from your example, I see you are using server scripting. The following snippet should help you understand how to achieve it and should also yield the result you desire:

    function filterByMonth(month){
    
      //get the selected month digit
      month = month.toLowerCase();
      var allMonths = {
        "january": 0,
        "february": 1,
        "march": 2,
        "april": 3,
        "may": 4,
        "june": 5,
        "july": 6,
        "august": 7,
        "september": 8,
        "october": 9,
        "november": 10,
        "december": 11
      };
      var monthDigit = allMonths[month];
    
      // Select employee number and current month data
      var selectedMonthStart = new Date();
      selectedMonthStart.setMonth(monthDigit);
      selectedMonthStart.setDate(1);
      selectedMonthStart.setHours(0,0,-1,0);
      var selectedMonthEnd = new Date();
      selectedMonthEnd.setMonth(monthDigit+ 1);
      selectedMonthEnd.setDate(1);
      selectedMonthEnd.setHours(0,0,0,0);
      var query = app.models.AttendanceMaster.newQuery(); 
      query.filters.EmployeeId._equals = userNumber; // Match employee number
      query.filters.Date._greaterThan = selectedMonthStart; // Including month start time
      query.filters.Date._lessThan = selectedMonthEnd; // Including month end time
      var AttendanceResult = query.run();
    
      return AttendanceResult; 
    }