A Deep Dive into CSS Grid

As the title says A Deep Dive into CSS Grid, I’m not gonna explain the basic of CSS grid rather in this post I’ll try to explore almost all features and properties of CSS grid.

Recap: CSS grid allows us to divide a webpage into rows and columns with simple properties to build complex layouts as well as small user interfaces using CSS only, without any change to the HTML.

Let’s dive in detail..

Property: display
To make an element a grid, set its display property to grid.

.to-be-grid {
display: grid;
}

Doing this makes .to-be-grid a grid container and its children grid items.

Property: grid-template-columns

We can create columns by using the grid-template-columns property. To define columns set this grid-template-columns property to column sizes in the order that you want them in the grid to appear. Let’s have a look:

.grid {
display: grid;
grid-template-columns: 100px 100px 100px;
}

This defines 3 100px width columns. All grid items will be arranged in order, in these columns. And the row height will be equal to the height of the tallest element in row but this also can be changed with grid-template-rows.

Property: grid-template-rows

It is used to define the number & the size of rows in a grid. It is similar in syntax to grid-template-rows.

.grid {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
}

Only the grid-template-rows property without grid-template-columns would result in column width being same as the width of the widest element in that row.

Property: grid-template

Grid is shorthand for three properties: grid-template-rows, grid-template-columns, and grid-template-areas.

You can use it is like this:

.grid {
grid-template:
“header header header” 80px
“nav article article” 600px
/ 100px 1fr;
}

You define the template areas such as you normally would, place the width of every row on its side then finally place all the column widths after a forward slash. Like before, you’ll place all of it on one line.

Data Type:

The unit “fr” is a newly created unit for CSS Grid Layout. The fr unit helps to create flexible grids without calculating percentages. 1 fr represents one fraction of available space. The available space is divided into total number of fractions defined. So, 3fr 4fr 3fr divides the space into 4+3+4=11 parts and allocates 4, 3 and 4 parts of the available space for the three rows/columns respectively. For example:

.grid {
display: grid;
grid-template-columns: 3fr 4fr 3fr;
}

If you combine fixed units with flexible units, the available space for the fractions is calculated after subtracting the fixed space. Let’s check out another example:

.grid {
display: grid;
grid-template-columns: 3fr 200px 3fr;
}

The width of a single fraction is calculated like: (width of .grid – 200px) / (3 + 3). The space of the gutters, if any would have also been subtracted initially from the width of .grid. This is the difference between frs and %s that percentages don’t include the gutter that you define with grid-gap.

Here 3fr 200px 3fr is essentially equal to 1fr 200px 1fr.

Guess, that’s a lot for today :pĀ  Will continue remaining in part two.

Show data from two JPA entity tables on same page using Spring Boot and Thymeleaf

spring boot thymeleaf bootstrap 4

Let’s say you want to display multiple table contents in Spring Boot project using Thymeleaf, JPA Entity and Model.

In my project I have mapped a relationship between the two entities I need to retrieve data from and am wondering how to display data from the two tables on list page. I’ve ‘category’ table and ‘product’ table like below:

Spring Boot, Thymeleaf Show data from two JPA entity tables on same page using Spring Boot and Thymeleaf

And in my controller I’ve list of both category and product in respective model. Here is the snippet:

model.addAttribute("products", productService.findAll());
model.addAttribute("categories", categoryService.findAll());

So, I’ve all the required data in the model and I can access them from Thymeleaf. Let’s copy the table code from below (N.B: I’ve used bootstrap table):

<table class="table table-striped table-bordered table-responsive" role="grid">
   <thead>
      <th>Category</th>
      <th>Product</th>
   </thead>
   <tbody>
      <tr role="row" class="odd" th:each="product:${products}">
         <td>
            <p th:each="category : ${categories}" th:if="(${category.id} == ${product.id})" th:text="${category.name}"></p>
         </td>
         <td th:text="${product.name}">
      </tr>
   </tbody>
</table>

It’ll produce something like this:

Spring Boot, Thymeleaf: Display data from two JPA entity tables on same page

That’s all! We’re done!
If you’ve any confusion please let me know šŸ™‚

Thymeleaf select required not working?

thymeleaf

Since last few days trying to add select “required” attribute in my Thymeleaf project.  Neither of these works in Thymeleaf:

  • <select required=”required”>
  • <select required=’required’>
  • <select required=required>
  • <select required=””>
  • <select required=”>
  • <select required>

In this tutorial I’m gonna show how to achieve this.

Let’s copy the code from below:

<select th:name="someName" class="form-control" id="someID" th:required="true">
    <option value="">Select Category</option>
    <option th:each="model: ${someModel}" th:value="${model.id}"
            th:text="${model.Title}"></option>
</select>

Please note that, first/default option value must be set to “” if you put “0” it won’t work!

That’s all! We’re done!
If you’ve any confusion please let me know šŸ™‚

Bootstrap Dynamic Modal with Spring Boot and Thymeleaf

spring boot thymeleaf bootstrap 4

Let’s say you want to dynamically create modal popup in your Spring Boot project using Twitter Bootstrap 4 to show the user a ‘warning popup’ before deleting a data or edit the contents on the fly in same page. Not to mention I’ve used Thymeleaf as my template engine.

In this tutorial I’m gonna show how to achieve this.

I assume you have already included required Bootstrap resources and necessary jQuery library in your project and are ready to implement modal popup. Let’s copy the modal code from below:

<div class="modal modal-warning fade in" id="myModal">
 <div class="modal-dialog">
 <div class="modal-content">
 <div class="modal-header">
 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
 <span aria-hidden="true">Ɨ</span></button>
 <h5 class="modal-title">Delete User</h5>
 </div>
 <div class="modal-body">
 <h3>Are you sure want to delete this user?</h3>
 </div>
 <div class="modal-footer">
 <button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
 <a type="button" class="btn btn-danger"><i class="fa fa-check"></i>&nbsp;Yes</a>
 </div>
 </div>
 </div>
 </div>

It’ll produce something like this:

Bootstrap Dynamic Modal with Spring Boot and Thymeleaf

Now, the next step actually the final step. Make the modal dynamically generate for all the data in table.

I’ve some data in the table like below now I want to delete a user but before that I want that modal to generate dynamically for all data.

Bootstrap Table in Spring boot

On the ‘Red’ delete button the popup will generate.

<a data-toggle="modal" data-target="#modal-warning" th:attr="data-target='#modal-warning'+${user.id }"><span class="glyphicon glyphicon-trash"></span></a>

Here “${user.id}” is my thymeleaf object.

Now, let’s make the modal dynamic with the id:

<div class="modal modal-warning fade in" th:id="modal-warning+${user.id }" >
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">Ɨ</span></button>
                <h5 class="modal-title">Delete User</h5>
            </div>
            <div class="modal-body">
                <h3>Are you sure want to delete this user?</h3>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
                <a type="button" class="btn btn-outline" th:href="@{/user/delete/{id}(id=${user.id})}"><i class="fa fa-check"></i>&nbsp;Yes</a>
            </div>
        </div>
    </div>
</div>

Here “th:href=”@{/user/delete/{id}(id=${user.id})}” is your POST URL.
You can also edit any data inside the modal by adding a form.

That’s all! We’re done!
If you’ve any confusion please let me know šŸ™‚