Lesson 3 - Now Code It
This tutorial take you through the process of writing JavaScript code in Appsmith. In the earlier lessons, you have written JS code inside the mustache syntax {{}}
which is great for single line coding. If you want to write more complex code, you have to use JS Objects.
Write JS functions
Select the Explorer tab on the Entity Explorer to the screen's left.
Click the + icon next to Queries/JS.
Select New JS Object from the list of options.
Delete everything within
export default {}
. Instead, paste the following function inside the braces.getBackground: (gender) => {
if (gender == 'male') return "#42f587";
else if (gender == 'female') return "#f5e942";
else return "#f57b42";
}This function returns a different hex color code based on gender.
Go back to the canvas by clicking the page name or the Widget tab.
Select the
usersTable
Table.Click the gear icon ⚙️ next to the gender column. Click on the Style tab.
In the Cell Background property, click the JS button and paste the following code snippet to call the function to return the color code based on the gender of the user in the current table row.
{{JSObject1.getBackground(currentRow.gender)}}
You will see the cell color change based on the value in the gender column.
Use built-in objects and functions
Appsmith provides global objects and functions within its framework to help build your apps.
Let's show an alert message after the update query runs. Click the Update button in the Form widget. In the onClick event, click the JS button to the right. Modify the code as shown below.
{{
updateUsers.run().then(() => {
showAlert('Record updated');
getUsers.run();
});
}}Test by editing any record and click the Update button to see the alert message pop up on the top of the screen.
Create a new blank page by clicking on the + next to Pages on the Entity Explorer to the left of the screen. Rename the page to
Account Details
.
Let's display the name of the currently logged in user. Drop a Text widget at the top of this page. You will see your name or email id by default. Take a look at the Text property on the right. It contains the following code snippet.
Hello {{appsmith.user.name || appsmith.user.email}}
Here you are using the
appsmith
global object to extract information about the currently logged in user and append it to the textHello
. For more information, refer to appsmith global object.
Navigate to another page
You can also navigate from one page to another. Go back to the User Information page. Drop a button widget anywhere on the canvas. Change its Label property to
Next Page
.In the onClick property, click the JS button.
Between the curly braces
{{}}
enter the following code snippetnavigateTo('Account Details')
Click the Next Page button to test if it redirects you to the Account Details page.
For more information on all the global functions available in the Appsmith framework, see Global Functions.
- Click the Deploy button on the top right of the screen to deploy the application and test it in the View mode.
🚩 Congratulations! You have built your first app that can display data from the database and save the updated data using a form.
In this tutorial, you explored a few different widgets and created a simple database GUI to view, query, and update data on a sample PostgreSQL database. You can use these skills to build your own app.
Happy App Building!