Check List via a Table?

Viewing 3 reply threads
  • Author
    Posts
  • August 27, 2024 at 2:01 AM #51063

    David Oxtoby
    Participant

    I’m wanting to create a table as a list of jobs to do, and have table fields that include things like ‘Task’ ‘Priority’, ‘Done’. (where Done is a check-mark).

    I’d wondered if I could add a script field into the table that somehow runs whenever the check-mark is checked, and it’ll run a script like something below, to delete the row that I’ve just added the check-mark against, sort of acting like a to-do list that deletes completed tasks.

    the below runs but it doesn’t work, and I wonder if I’m missing anything obvious.

    function deleteCheckedRows() {
    var tableFieldID = ‘fld-e6d2b5eaf2fb4e78b708ff36d05f31a5’;
    var checkmarkFieldID = ‘fld-475a8bbcba634befbca61e39a61fef8c’;

    // Get the rows in the table field
    var tableRows = record.getFieldValue(tableFieldID);

    // Array to hold the rows that should remain
    var rowsToKeep = [];

    // Loop through each row in the table field
    for (var i = 0; i < tableRows.length; i++) {
    var row = tableRows;

    // Check if the checkmark field is selected (true)
    if (!row.getFieldValue(checkmarkFieldID)) {
    // If not selected, keep this row
    rowsToKeep.push(row);
    }
    }

    // Set the filtered rows back to the table field
    record.setFieldValue(tableFieldID, rowsToKeep);

    // Save changes
    document.saveAllChanges();

    // Optional: Provide feedback
    Utils.alertWithMessage(“Checked rows deleted”, “All checked rows have been deleted.”, “info”);
    }

    // Run the script
    deleteCheckedRows();

    August 27, 2024 at 2:28 AM #51064

    David Oxtoby
    Participant

    update: the following code does what i want if I put into a script outside of the table (i.e. not a script field in the table), but how do I trigger the form script whenever I tick the check-mark field inside the table?

    function deleteCheckedRows() {
    // Replace ‘tableFieldID’ with the ID of your table field
    var tableFieldID = ‘fld-e6d2b5eaf2fb4e78b708ff36d05f31a5’; // Replace with your actual table field ID

    // Replace ‘checkmarkFieldID’ with the ID of your checkmark field in the table
    var checkmarkFieldID = ‘fld-475a8bbcba634befbca61e39a61fef8c’; // Replace with your actual checkmark field ID

    // Get the rows in the table field
    var tableRows = record.getFieldValue(tableFieldID);

    // Check if tableRows is not null or undefined
    if (tableRows && tableRows.length > 0) {
    console.log(“Number of rows: ” + tableRows.length);

    // Loop through each row in the table field
    for (var i = tableRows.length – 1; i >= 0; i–) {
    var row = tableRows;

    // Check if the checkmark field is selected (true)
    if (row.getFieldValue(checkmarkFieldID)) {
    // If selected, remove this row from the table field
    record.removeRecordFromField(row, tableFieldID);
    console.log(“Found a checkmark and deleted a row.”);
    }
    }

    // Save changes
    document.saveAllChanges();

    // Optional: Provide feedback
    Utils.alertWithMessage(“Checked rows deleted”, “All checked rows have been deleted.”, “info”);
    } else {
    console.log(“No rows found in the table field.”);
    }
    }

    // Run the script
    deleteCheckedRows();

    August 27, 2024 at 2:32 AM #51065

    David Oxtoby
    Participant

    update: ahhh, just need to put the script into a field that sits in the form, sorted.

    August 27, 2024 at 1:10 PM #51074

    JScottA
    Participant

    Using your initial query and script problem, I ran it through the TF5 assistant to see if it was helpful:

    The script you provided is almost correct but seems to be missing a couple of critical aspects, particularly regarding how Tap Forms triggers scripts and how it handles table fields.

    In Tap Forms, scripts are generally not automatically triggered by field changes such as checking a checkbox. Instead, scripts are usually run manually or via a button field or script field that the user has to click to execute.

    Here’s a refined version of your script with some improvements:

    `javascript
    function deleteCheckedRows() {
    var tableFieldID = ‘fld-e6d2b5eaf2fb4e78b708ff36d05f31a5’;
    var checkmarkFieldID = ‘fld-475a8bbcba634befbca61e39a61fef8c’;

    // Get the rows in the table field
    var tableRows = record.getFieldValue(tableFieldID);

    // Array to hold the rows that should remain
    var rowsToKeep = [];

    // Loop through each row in the table field
    for (var i = 0; i < tableRows.length; i++) {
    var row = tableRows;

    // Check if the checkmark field is selected (true)
    if (!row.getFieldValue(checkmarkFieldID)) {
    // If not selected, keep this row
    rowsToKeep.push(row);
    }
    }

    // Set the filtered rows back to the table field
    record.setFieldValue(tableFieldID, rowsToKeep);

    // Save changes
    document.saveAllChanges();

    // Optional: Provide feedback
    Utils.alertWithMessage(“Checked rows deleted”, “All checked rows have been deleted.”, “info”);
    }

    // Run the script
    deleteCheckedRows();
    `

    ### Key Points:
    1. **Manual Execution:** As per Tap Forms’ typical functionality, you’ll need to trigger this script manually, possibly by adding a script field or button field to run it.

    2. **Correcting Array Access:** In the loop, var row = tableRows; was adjusted to properly access each row in the table.

    3. **Triggering:** If you’re expecting this to run automatically when a checkbox is ticked, Tap Forms does not support that natively. You may need to manually run the script after checking boxes, unless you incorporate it into a workflow where a button click triggers the script.

    ### Execution:
    You might need to manually run the script by selecting it from the Scripts section or by creating a button in your form that users can press after they have checked off the tasks they want to delete.

    For more detailed scripting references, you may need to refer to the Tap Forms scripting documentation or related resources [oai_citation:1,1328_16794_en_1538683502.pdf](file-service://file-15C3MsaZPdU2JvfF2hiej31w).

Viewing 3 reply threads

You must be logged in to reply to this topic.