ToolJet/docs/versioned_docs/version-2.33.0/how-to/delete-multiple-rows-table.md

131 lines
5.1 KiB
Markdown
Raw Normal View History

Release: Community changes (v2.34.0) (#9226) * add custom resolvers info and editable row selection info (#9057) * fix system requirements icon * add auth info for webhooks and fix casing * add regex custom validation info (#9068) * [docs]: Marketplace 1.7 updates (#9085) * [docs] Amazon redshift plugin * make minor improvements * add and update docs for marketplace 1.7 * update order of plugins in overview to match sidebar * create new version --------- Co-authored-by: Shubhendra <withshubh@gmail.com> * add the latest version in the versions.json file (#9094) * [docs]: Update PDF component (#9088) * update PDF component * merged with develop and added changes to the new version * update docs favicon: (#9118) * [docs] SSO revamp (#9031) * add method to set default language * update image settings through custom css and update screenshots for getting started and tooljet concepts (#9158) * fix read documentation button * fix formatting for setup icons (#9172) * fix sidebar link for aws lambda * Update static media (#9175) * updated the screenshots * reduced the gif size * reverted the package.json file * edited the zoomed in images and replaced some gifs with screenshots * removed one gif * update static media * update file names * update toolbar * fix file names * fix: dynamodb img path * update media for org management dashboard * fix: casing and formatting * update workspace constant media * update media in workspace settings and github * update github sso * minor change to github sso docs * minor fix * update google sso * change includeCurrentVersion flag to false --------- Co-authored-by: Asjad Ahmed Khan <iitasjad2001@gmail.com> Co-authored-by: Asjad Ahmed Khan <60435499+2001asjad@users.noreply.github.com> Co-authored-by: Karan Rathod <karan.altcampus@gmail.com> * Feature: Engagespot plugin (#9012) * feat(plugins): added engagespot plugin * feat(docs): added engagespot plugin docs * chore(engagespot-plugin): revised copywritings * Feature: Databricks data source (#9174) * plugin-created * Databricks integration * icon, error handling * removed unrelated changes from marketplace and frontend package-lock.json removed runAsync and maxRows timeouts pending * timeout implementation * socket timeout and error handling * resolve comments * resolve comments2 * solved render issue test connection improvements * solved undefined error * fix TJDB not null value fail for is operation (#9055) * fix TJDB not null value fail for is operation * handling not null and null case insenstive values * Support for marketplace plugin deploy on render preview app (#9221) * Fix for marketplace error on render preview app * add marketplace build command * Adding new workflow for building marketplace plugin * removed render app creation * [:docs] Add documentation for Databricks plugin (#9224) * add docs for databricks * update databricks docs * update docs * remove ref to clusters * bump to v2.34.0 * Fixed data source cypress failure (#9227) * updated spec with required text * updated mongodb and import spec * updated import spec --------- Co-authored-by: Karan Rathod <karan.altcampus@gmail.com> Co-authored-by: Adish M <44204658+adishM98@users.noreply.github.com> Co-authored-by: Midhun G S <gsmithun4@gmail.com> Co-authored-by: Shubhendra <withshubh@gmail.com> Co-authored-by: Aman Regu <amanregu@gmail.com> Co-authored-by: Asjad Ahmed Khan <iitasjad2001@gmail.com> Co-authored-by: Asjad Ahmed Khan <60435499+2001asjad@users.noreply.github.com> Co-authored-by: Jobin Jose <129726530+jobin-logidots@users.noreply.github.com> Co-authored-by: Syed Mohammad Akhtar Rizvi <85864291+ShazanRizvi@users.noreply.github.com> Co-authored-by: blank0537 <111295371+blank0537@users.noreply.github.com> Co-authored-by: Mekhla Asopa <59684099+Mekhla-Asopa@users.noreply.github.com>
2024-03-29 13:43:26 +00:00
---
id: delete-multiple-rows
title: Delete Multiple Rows in a Table
---
This guide explains how to delete multiple rows from a table, assuming you've already connected to a data source. We'll use PostgreSQL for this example, but you can adjust the queries based on the SQL database that you are using.
## 1. Create a Query to Fetch the Data from the Database
- Create a new query and name it *getRecords*.
- Select SQL mode and enter the following query:
```sql
SELECT * FROM tooljet // replace tooljet with your table name
```
- Enable the `Run the query on application load?` option to execute the query automatically when the application starts.
<div style={{textAlign: 'left'}}>
<img className="screenshot-full" src="/img/how-to/delete-rows/getRecords.png" alt="How-to: Delete Multiple Rows in Table" />
</div>
<br/>
## 2. Populating the Table with Data
- Drag and drop a **Table** component on the canvas.
- In Table properties, go to the `Data` property and set the value to `{{queries.getRecords.data}}`.
- Now if you run the *getRecords* query, the returned data will be loaded in the Table component.
<div style={{textAlign: 'left'}}>
<img className="screenshot-full" src="/img/how-to/delete-rows/querydata.png" alt="How-to: Delete multiple rows in table" />
</div>
<br/>
## 3. Enable Bulk Row Selection on Table
- Go to the Table properties and enable the `Bulk selection` option.
- Enabling this option will allow you to select multiple rows on the table.
<div style={{textAlign: 'center'}}>
<img className="screenshot-full" src="/img/how-to/delete-rows/bulkselection.png" alt="How-to: Delete multiple rows in table" />
</div>
<br/>
## 4. Create a Custom JavaScript Query
- Create a new Run Javascript code query. It will be named *runjs1* by default.
- Enter the following code:
```js
const uniqueIdentifier = "id";
const idsToDelete = Object.values(components.table1.selectedRows).map(dataUpdate => dataUpdate[uniqueIdentifier]);
const idsString = idsToDelete.map(id => `'${id}'`).join(', ');
const SQL = `DELETE FROM tooljet WHERE ${uniqueIdentifier} IN (${idsString});`;
return SQL;
```
The above code generates a SQL query that deletes rows from the database table where the `id` field matches the selected IDs in ToolJet's Table component.
- Click on the **Preview** button to see the SQL statement generated by the query.
<div style={{textAlign: 'left'}}>
<img className="screenshot-full" src="/img/how-to/delete-rows/runjs.png" alt="How-to: Delete multiple rows in table" />
</div>
*If you're using a different column as the unique identifier, feel free to update the code accordingly. You can also update the Table name if you have renamed it, the default name is *table1*.*
- Select a few rows on the Table component and then Preview the SQL query generated by the *runjs1* query.
<div style={{textAlign: 'left'}}>
<img className="screenshot-full" src="/img/how-to/delete-rows/runjs1.png" alt="How-to: Delete multiple rows in table" />
</div>
<br/>
## 5. Create a New Query to Delete the Rows
- Create a new query, name it `delete`, and select SQL mode.
- Enter the following code:
```sql
{{queries.runjs1.data}}
```
In this query, we are dynamically loading the SQL statement generated by the JavaScript query.
<div style={{textAlign: 'left'}}>
<img className="screenshot-full" src="/img/how-to/delete-rows/delete.png" alt="How-to: Delete multiple rows in table" />
</div>
<br/>
## 6. Add a Button to Delete the Selected Rows
- Drag and drop a **Button** component on the canvas.
- Edit its properties and set the `Button text` property to "Delete selected".
- Add a new **Event** to the button.
- Select On click as the Event, Run Query as the Action, and *runjs1* as the Query.
<div style={{textAlign: 'left', marginBottom: '15px'}}>
<img className="screenshot-full" src="/img/how-to/delete-rows/button.png" alt="How-to: Delete multiple rows in table" />
</div>
- Optionally, we can add a loading state to the Button whenever the *delete* or *getRecords* query is running:
```js
{{queries.delete.isLoading || queries.getRecords.isLoading}}
```
- Add a new **Event** to the *runjs1* query.
- Select Query Success as the Event, Run Query as the Action and *delete* as the Query.
<div style={{textAlign: 'left', marginBotton:'15px'}}>
<img className="screenshot-full" src="/img/how-to/delete-rows/eventrunjs.png" alt="How-to: Delete multiple rows in table" />
</div>
Now, whenever you click on the Button component, the *runjs1* query will run and generate a delete SQL statement with selected rows on the table. Once the *runjs1* query executes, the *delete* query will execute and delete the rows from the database.
- Add a new **Event** to the *delete* query.
- Select Query Success as the Event, Run Query as the Action and *getRecords* as the Query.
<div style={{textAlign: 'left', marginBottom:'15px'}}>
<img className="screenshot-full" src="/img/how-to/delete-rows/eventdelete.png" alt="How-to: Delete multiple rows in table" />
</div>
By implementing this, we are ensuring that every time rows are deleted, the Table component will automatically refresh to display the most recent data fetched from the database.