Dynamically Delete Rows from Tables
Overview
This article introduces how to delete rows from a table using JavaScript. A Delete button is placed inside the record. Clicking the button deletes the row whose row number matches the specified value.
Initial Setup
This section describes building a Kintone App before deleting rows from a table.
Create a sample App
First, create a new Kintone App.
Alternatively, the sample App made in the Dynamically Add Rows to Tables article can be reused. In this case, update the Blank space field and add the Number fields listed below.
Set the fields in the sample App as follows.
| Field Type | Field Name | Field Code |
|---|---|---|
| Text | Company Name | company_name |
| Number | Row to Delete | row_to_delete |
| Blank space | Delete Contact | delete_contact |
| Table | Contact List | contact_list |
Set the fields inside the Table field as follows.
| Field Type | Field Name | Field Code | Notes |
|---|---|---|---|
| Number | Row Number | row_number | |
| Text | Contact Name | contact_name | |
| Link | Email Address | email_address | Select E-mail address as the type |
| Check box | Mailing List | mailing_list | Add two options: "Newsletter" and "Product Updates". |
Create sample data
Next, add a record, and enter three rows of sample data into the table as shown below:
| Row Number | Contact Name | Email Address | Mailing List |
|---|---|---|---|
| 1 | Emma Rivera | emma.rivera@example.com | Newsletter |
| 2 | Noah Chen | noah.chen@example.com | Product Updates |
| 3 | Olivia Park | olivia.park@example.com | Newsletter |
Sample Code
Prepare the following JavaScript code in a text editor, and navigate to the Kintone App's settings. Upload the file into the Upload JavaScript for PC option of the
JavaScript and CSS Customization settings
.
|
|
Save the settings and click Update App. Open a record and click the Edit record icon. A Delete button appears on the Space field.
Enter 2 in the Row to Delete field, and click the Delete button. The second row of the "Contact List" field is deleted, and the remaining rows are renumbered.
Code Explanation
This section explains the sample code above. The code consists of two parts: placing a button in the Space field and deleting a row from the table.
Create a Button in the Space Field
The app.record.create.show and app.record.edit.show events run when the Add record screen or Edit record screen is displayed.
kintone.app.record.getSpaceElement('delete_contact') retrieves the Space element by its element ID, delete_contact. A button labeled Delete is created and appended to that element. The deleteRow function is assigned to the button's onclick event.
|
|
Delete a Row from the Table
The Table field is represented as an array. To delete a row, an element is removed from the array using splice().
Code to delete a row from a table
splice() takes two arguments:
- First argument: The index of the element to start from.
- Second argument: The number of elements to delete.
The following code deletes one row, starting from the second row.
|
|
Structure of the deleted row
The following diagram shows the structure of the record data. The row at index 1 is the row to be deleted.
|
|
How the sample code deletes a row
The code loops through the rows with forEach() and checks each row.
The matching row is then deleted with splice().
Get the record and the row number to delete
kintone.app.record.get() retrieves the record data. The value of the Row to Delete field is stored in targetRowNo.
|
|
Loop with forEach() and delete the matching row with splice()
The row whose Row Number value matches the Row to Delete value is deleted.
|
|
Update the row numbers
|
|
After a row is deleted, the row numbers are no longer sequential.
The resetRowNo function loops through the remaining rows and sets each Row Number value to its index plus one.
|
|
Reflect the changes in the record
Finally, kintone.app.record.set() reflects the updated record data on the screen.
|
|
Notes
To Delete Rows When the Record Is Saved
The row is deleted when the Delete button is clicked.
To delete a row when the record is saved instead, edit the table data in event.record during the app.record.create.submit or app.record.edit.submit event, and return the event object. The edited table data is then saved with the record.
To Delete All Rows in the Table
The Table field is an array, so emptying the array deletes all rows at once.
|
|