Jump to content

Search with custom date ranges


Recommended Posts

I would like to share an example of the solution to create a search with custom date ranges.

Use case: need to filter records based on different conditions, for example, this week, previous week, this month, previous month.

For example, there is the field in the table with the Date/Time data type named 'Date_Time' and the search page should look like this:
6iavESH.png

Link to comment
Share on other sites

The first option is to apply JavaScript.

1) Add a Virtual field that can be Dropdown or Listbox with the list os options for search

TFhnCrS.png

 

2) Add Criteria for the Date/Time field. They should be combined by the 'AND' logical operator. 

5uV92Uk.png

 

This article can be helpful https://howto.caspio.com/datapages/reports/how-to-add-today-to-after-now-or-next-x-days-criteria/

Criteria 1 should be Hidden, have the 'Greater than or equal' comparison type, and the 'Date' precision  

bBMyHCg.png

 

Criteria 2 should be Hidden, have the 'Less than or equal' comparison type, and the 'Date' precision  

9pqytZU.png

 

3) The idea is to populate these Criteria with corresponding dates depending on the selected date range in the Virtual field.

For that, the easiest solution is to use the 'moment.js' library. It is available via the link https://momentjs.com/

- Copy the script from the 'moment.js' library https://momentjs.com/downloads/moment.min.js

ir5skPP.png

- Add the App parameter in the application 

This article can be helpful https://howto.caspio.com/apps/app-parameters/app-parameters/

App parameter type should be 'Long text', the value is the script copied form the library.

8jDQzcn.png

 

4) Add the JS code in the Footer of the Search page. Disable the HTML editor on the Advanced tab before pasting.

In this script Monday is the 1st day of the week.

<script>
//moment.js library, add parameter with correct name
[@app:moment_js]

document.addEventListener('DataPageReady', function (event) {

    const listboxField = document.querySelector('#cbParamVirtual1'); // virtual field, change the name if needed
    const dateFrom = document.querySelector('#Value2_1'); // Criteria 1 field, its ID should be changed if needed
    const dateTo = document.querySelector('#Value2_2'); //Criteria 2 field, its ID should be changed if needed

    const mondayThisWeek = moment().startOf('isoWeek').format('L');
    const sundayThisWeek = moment().endOf('isoWeek').format('L');

    const mondayPreviousWeek = moment().subtract(1, 'weeks').startOf('isoWeek').format('L');
    const sundayPreviousWeek = moment().subtract(1, 'weeks').endOf('isoWeek').format('L');

    const firstDayThisMonth = moment().startOf('month').format('L');
    const lastDayThisMonth = moment().endOf('month').format('L');

    const firstDayPreviousMonth = moment().subtract(1,'months').startOf('month').format('L');
    const lastDayPreviousMonth = moment().subtract(1,'months').endOf('month').format('L');

    listboxField.addEventListener('change', (event) =>{
        const options = Array.from(event.target.selectedOptions);
        let selectedValue = options[0].value;
        
        switch (selectedValue) {
        case 'This week':
            dateFrom.value = mondayThisWeek;
            dateTo.value = sundayThisWeek;
            break;
        case 'Previous week':
            dateFrom.value = mondayPreviousWeek;
            dateTo.value = sundayPreviousWeek;
            break;
        case 'This month':
            dateFrom.value = firstDayThisMonth;
            dateTo.value = lastDayThisMonth;
            break;
        case 'Previous month':
            dateFrom.value = firstDayPreviousMonth ;
            dateTo.value = lastDayPreviousMonth ;
            break;
        case '':
            dateFrom.value = '';
            dateTo.value = '';
            break;
        }
    })
});
</script>
Link to comment
Share on other sites

The second option is to use a Lookup table updated by Task. 

This is the easier solution if Tasks are included in the plan.

1) Create a Lookup table with the following table  design

NSMsDq5.png

2) Populate the 'Date_name' field

FOS5w1Z.png

3) Create a Task that runs daily to update the dates

i6Cnqij.png

gJOarGo.png

4) On the Search page use the 'Range' Comparison type with the following logic:

yagEMvB.png

 

Result:

sHdqBt4.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...