Join the Team Forms community

reference a data source in a text field

Updated 5 months ago

i have a table show below and i would want the sum of 'days requested' column based on the 'type of leave' to get the total days requested example: the calculated value of the number field = 33 where the 'type of leave' = 'sickleave'.

I tried accessing the data source to query it but have no idea where to start. any help will be appreciated

Marked as solution

Hi Alex,

Data sources can be accessed directly in JavaScript, allowing you to set the value of any component based on the data source. The steps below should help you set started.

  1. Navigate to the data sources screen and click the "Data Source Id" menu option. This will reveal a dialogue showing the id of the data source along with some example code.

    GIF on getting data source Id in Team Forms
  2. Open the form builder screen and drag-n-drop a number field (or text field) into your form. Since days are typically a number, I think a number field make more sense.

  3. In the calculated value enter the following JavaScript. You will need to modify the JavaScript to match your specific data source ids, field names and column names.

    async function calculateLeave(){
        const dataSoruceRows =  await tf.getDataSource('enter your data source id here')
        const totalHours = _(dataSoruceRows)
            .filter(row => row?.leaveType === 'sickLeave')
            .sumBy('daysRequested')
        return totalHours
    }
    
    value = calculateLeave() 

You should find that the total of the data source will appear in the number field / text field.

View full solution
E
1 comment

Hi Alex,

Data sources can be accessed directly in JavaScript, allowing you to set the value of any component based on the data source. The steps below should help you set started.

  1. Navigate to the data sources screen and click the "Data Source Id" menu option. This will reveal a dialogue showing the id of the data source along with some example code.

    GIF on getting data source Id in Team Forms
  2. Open the form builder screen and drag-n-drop a number field (or text field) into your form. Since days are typically a number, I think a number field make more sense.

  3. In the calculated value enter the following JavaScript. You will need to modify the JavaScript to match your specific data source ids, field names and column names.

    async function calculateLeave(){
        const dataSoruceRows =  await tf.getDataSource('enter your data source id here')
        const totalHours = _(dataSoruceRows)
            .filter(row => row?.leaveType === 'sickLeave')
            .sumBy('daysRequested')
        return totalHours
    }
    
    value = calculateLeave() 

You should find that the total of the data source will appear in the number field / text field.