DEV Community

Explorer
Explorer

Posted on

๐Ÿ—‘๏ธ Add Delete Button and Add Row Feature in Joget Spreadsheet using JS

๐Ÿงฉ Overview

In Joget, the Spreadsheet form element (powered by Handsontable) is perfect for managing tabular data inside forms.
However, it doesnโ€™t natively provide built-in row-level delete buttons or a convenient add row feature.

In this guide, weโ€™ll learn how to:
โœ… Add a delete button to each row in a Joget Spreadsheet.
โœ… Create a separate โ€œAdd Rowโ€ button that dynamically inserts new rows.
โœ… Keep the entire implementation simple, secure, and fully functional.


โš™๏ธ How It Works

๐Ÿ’ก The approach uses a custom column renderer and a jQuery button event:

  • The custom renderer injects a Delete button in a column cell.
  • The button calls hot.alter("remove_row", row) to delete that row.
  • A separate Add Row button triggers hot.alter("insert_row_below") to insert a new row at the bottom.
  • Both actions are performed dynamically without reloading the form.

๐Ÿง  Important:

  • Set the spreadsheet column format type to Custom.
  • Paste the renderer script directly in the custom field configuration.
  • Avoid adding any extra comments inside the renderer function (to prevent Joget parser issues).

๐Ÿ’ป Full Code

<!-- ๐Ÿ”น Place this inside your Spreadsheet column (Format Type: Custom) -->
{ { 
  renderer: function (instance, td, row, col, prop, value, cellProperties) { 
    td.innerHTML = "<button type='button' class='eaction'>#i18n.ocs_delete_btn#</button>"; 
    td.style.textAlign = "center"; 
    $(document).ready(function () { 
      let hot = FormUtil.getField("appointments_spreadSheet").data("hot"); 
      let btn = td.querySelector(".eaction"); 
      btn.onclick = function (e) { 
        e.preventDefault(); 
        e.stopPropagation(); 
        hot.alter("remove_row", row); 
      }; 
    }); 
  } 
} }
Enter fullscreen mode Exit fullscreen mode
// ๐Ÿ”น Add this script in a Custom HTML element below your Spreadsheet

$("#addRowBtn").on("click", function(e) {
    e.preventDefault();
    let hot = FormUtil.getField("appointments_spreadSheet").data("hot");
    console.log({hot: hot});
    if (!hot) {
        console.error("Handsontable not initialized");
        return;
    }

    try {
        let lastRow = hot.countRows();
        hot.alter("insert_row_below", lastRow);
    } catch (err) {
        console.error("Failed to add row", err);
    }
});
Enter fullscreen mode Exit fullscreen mode
<!-- ๐Ÿ”น Add Row Button -->
<button id="addRowBtn" class="btn btn-primary">
  โž• Add Row
</button>
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Example Use Cases

โœ… Employee Shift Planner โ€“ Quickly add or remove shift rows.
โœ… Project Task Tracker โ€“ Manage task entries dynamically.
โœ… Material Request Form โ€“ Add/remove item rows on demand.
โœ… Appointment List โ€“ Delete canceled appointments instantly.


๐Ÿ› ๏ธ Customization Tips

๐Ÿ’ก To change the delete button label:
Replace

#i18n.ocs_delete_btn#
Enter fullscreen mode Exit fullscreen mode

with plain text (e.g., "Delete" or "Remove").

โš™๏ธ To use a custom Spreadsheet ID:
Update this line:

FormUtil.getField("appointments_spreadSheet")
Enter fullscreen mode Exit fullscreen mode

to match your spreadsheetโ€™s field ID.

๐ŸŽจ To style buttons:
Apply CSS in a Custom HTML element:

<style>
  .eaction {
    background-color: #e74c3c;
    color: #fff;
    border: none;
    padding: 5px 10px;
    border-radius: 6px;
  }
  #addRowBtn {
    margin-top: 10px;
  }
</style>
Enter fullscreen mode Exit fullscreen mode

๐ŸŒŸ Key Benefits

โœ… User-friendly: Adds clear visual controls to manage rows.
โš™๏ธ Lightweight: Uses only jQuery and built-in Joget APIs.
๐Ÿš€ Instant updates: No form reload needed.
๐Ÿงฉ Reusable: Works in any spreadsheet-based form.
๐Ÿ”ง Customizable: Easily adjust button text, color, or logic.


๐Ÿ”’ Security Note

โš ๏ธ Ensure that delete actions are contextually safe โ€”

  • Only use this feature for client-side data entry (not for record deletion in the database).
  • If connected to backend records, validate all deletion logic on the server side before applying changes.

๐Ÿงญ Final Thoughts

By adding these small enhancements, your Joget Spreadsheet becomes much more interactive and user-friendly.

Top comments (0)