Easily showcase a limitless number of items within a 10-column grid using Bootstrap!

This is the code snippet:

<div *ngFor="let minute of state.minutes.specificMinutes.selectedMinutes | keyvalue" class="col-sm-1 checkbox-container">
   <div class="custom-control custom-checkbox">
     <input type="checkbox" (click)="state.minutes.subTab='specificMinutes'"
            class="custom-control-input" id="minute-{{minute.key}}-checkbox"">
     <label class="custom-control-label" for="minute-{{minute.key}}-checkbox">{{minute.key}}</label>
   </div>
</div>

I want to fit only 10 custom-controls in a row, but since I am using col-sm-1, I end up with 12. Is there a way to ensure exactly 10 custom-controls per row without having a decimal value like col-sm-1.2? How can this be accomplished?

The number of items in the array exceeds 10, and the exact count is not known beforehand.

Answer №1

To evenly distribute space among 10 columns, just add the class="col"

Answer №2

When dealing with an unknown number of items, one approach is to divide your array or list into smaller chunks containing 10 items each in your component class. This allows you to iterate through them effectively.

For instance, you can create a function like this in your component class:

chunkArray() {
  let minutes = this.state.minutes.specificMinutes.selectedMinutes;
  let chunks = [];
  let chunkSize = 10;
  for (let i = 0, j = minutes.length; i < j; i += chunkSize) {
    chunks.push(minutes.slice(i, i + chunkSize));
  }

  return chunks;
};

Subsequently, in your template, utilize nested *ngFor loops as demonstrated below:

<div *ngFor="let minutes of getChunks()" class="row">
  <div *ngFor="let minute of minutes | keyvalue" class="col checkbox-container">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" (click)="state.minutes.subTab='specificMinutes'" class="custom-control-input"
        id="minute-{{minute.key}}-checkbox"">
       <label class=" custom-control-label" for="minute-{{minute.key}}-checkbox">{{minute.key}}</label>
    </div>
  </div>
</div>

The first loop will render a row for each chunk, while the inner loop will display the individual elements within each chunk.

If needed, adjusting the value of chunkSize will change the number of items per row dynamically.

Update

In scenarios where the total item count is not fixed and might not be evenly divisible by 10, the last chunk could have fewer than 10 items. To handle this situation, consider modifying your markup as follows:

<div *ngFor="let minutes of getChunks()" class="row">
  <div class="offset-sm-1></div>
  <div *ngFor="let minute of minutes | keyvalue" class="col-sm-1 checkbox-container">
  ...
  </div>
  <div class="offset-sm-1></div>
</div>

Alternatively, adjust the final array in the getChunks() method to pad it with placeholder elements until there are 10 items. This way, your template will continue to display items uniformly.

Answer №3

To create space on both sides, simply insert an empty column.

.block {
  width: 20px;
  height: 20px;
  border-radius: 10px;
  padding: 10px;
  margin: 5px;
  background-color: skyblue;
  color: black;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<div class="row">
  <div class="col-md-1">
    <!-- EMPTY -->
  </div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1">
    <!-- EMPTY -->
  </div>
</div>

Answer №4

If you're looking to make some layout adjustments, try utilizing an offset.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />

<div class="row">
  <div class="col-sm-1 offset-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1"><!-- Last Div Remains Empty --></div>
</div>

Alternatively, you can opt for a custom design like the one below:

/*  SECTIONS  */

.section {
  clear: both;
  padding: 0px;
  margin: 0px;
}


/*  COLUMN SETUP  */

.col {
  display: block;
  float: left;
  margin: 1% 0 1% 0%;
}

.col:first-child {
  margin-left: 0;
}


/*  GROUPING  */

.group:before,
.group:after {
  content: "";
  display: table;
}

.group:after {
  clear: both;
}

.group {
  zoom: 1;
  /* For IE 6/7 */
}


/*  GRID OF TEN  */

.span_10_of_10 {
  width: 100%;
}

.span_9_of_10 {
  width: 90%;
}</answer4>
<exanswer4><div class="answer" i="58997289" l="4.0" c="1574425722" m="1574438530" a="YnJvb2tzcmVseXQ=" ai="4279496">
<p>If you need to adjust your layout, try incorporating an offset.</p>

<p><div>
<div>
<pre><code><link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />

<div class="row">
  <div class="col-sm-1 offset-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1"><!-- Last Div Remains Empty --></div>
</div>

Another option would be creating a personalized design similar to this:

/*  SECTIONS  */

.section {
  clear: both;
  padding: 0px;
  margin: 0px;
}


/*  COLUMN SETUP  */

.col {
  display: block;
  float: left;
  margin: 1% 0 1% 0%;
}

.col:first-child {
  margin-left: 0;
}


/*  GROUPING  */

.group:before,
.group:after {
  content: "";
  display: table;
}

.group:after {
  clear: both;
}

.group {
  zoom: 1;
  /* For IE 6/7 */
}


/*  GRID OF TEN  */

.span_10_of_10 {
  width: 100%;
}

.span_9_of_10 {
  width: 90%;
}

.span_8_of_10 {
  width: 80%;
}

.span_7_of_10 {
  width: 70%;
}

.span_6_of_10 {
  width: 60%;
}

.span_5_of_10 {
  width: 50%;
}

.span_4_of_10 {
  width: 40%;
}

.span_3_of_10 {
  width: 30%;
}

.span_2_of_10 {
  width: 20%;
}

.span_1_of_10 {
  width: 10%;
}


/*  GO FULL WIDTH BELOW 480 PIXELS */

@media only screen and (max-width: 480px) {
  .col {
    margin: 1% 0 1% 0%;
  }
  .span_1_of_10,
  .span_2_of_10,
  .span_3_of_10,
  .span_4_of_10,
  .span_5_of_10,
  .span_6_of_10,
  .span_7_of_10,
  .span_8_of_10,
  .span_9_of_10,
  .span_10_of_10 {
    width: 100%;
  }
}
<div class="section group">
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble retrieving alert message after submitting form using jquery

Trying to submit a form using jQuery, but when clicking on the submit button it displays a blank page. I understand that a blank page typically means the form has been submitted, but I want to show an alert() for testing purposes instead. However, it only ...

The NodeJS backend is receiving an empty request body from the Ionic frontend application

In my Ionic app (Angular2) with a Node backend, I have implemented a feature called Level, where each Level has its own title. Users can view a list of levels and add new ones through a modal. However, I encountered an issue where the titles are not being ...

Exploring the potential of Django to efficiently implement multiple listviews on a single webpage

Recently started working with Python and Django, but I may have bitten off more than I can chew. If anyone has the time to help educate me, I would greatly appreciate it. My main model deals with "work orders," each of which is linked to subsets of data. I ...

Stop the form submission from redirecting or refreshing by utilizing JavaScript

I'm facing an issue with a page on my website that includes textboxes generated from php, along with two buttons - GetData and SaveData. My goal is to enable users to press the enter key while editing a textbox and trigger the same action as clicking ...

Encountering a problem while attempting to save a scss file in Visual Studio Code

As a beginner in using sass, I decided to utilize the node-sass NPM package for compiling scss files into CSS format. Upon saving the file, I encountered the following error: { "status": 3, "message": "Internal Error: File to ...

Implementing NgRx state management to track and synchronize array updates

If you have multiple objects to add in ngrx state, how can you ensure they are all captured and kept in sync? For example, what if one user is associated with more than one task? Currently, when all tasks are returned, the store is updated twice. However, ...

Error: An unhandled exception occurred during component destruction: TypeError: Cannot access property 'unsubscribe' of undefined

I encountered an issue while trying to unsubscribe during the ngOnDestroy method in my shopping-edit.component.ts file, specifically when clicking on the link to navigate to my recipes page. Here is a screenshot demonstrating the error: error on link clic ...

Tips for positioning HTML elements at the exact mouse location as it moves, without any delay?

Currently, I am in the process of developing a web-based drawing application that does not rely on using a canvas. My decision to avoid using a canvas is because I plan to incorporate CSS Keyframes into the HTML elements upon placement. This approach allow ...

Ways to make text within a container smoothly slide down

Hello, I have a question about jQuery as I am relatively new to it. I am currently working on creating a team members section where clicking on a team member will slide down information about that person underneath. I have managed to set up the parent co ...

The issue with hiding and showing elements using JavaScript during drag and drop functionality

In my code, I have two boxes with IDs box1 and box2, These boxes can be dragged and dropped into the boxleft element, Upon dropping them, the background image is removed and only the name appears in the box, My issue is that when loading values into box ...

Guide on utilizing the express server in developer mode within the Angular 17 application development tool

The guidelines provided in the documentation () explain that: Angular CLI will generate an initial server setup specifically for rendering your Angular application on the server side. This server can be customized to handle additional functionalities lik ...

Having trouble with links, imported templates, and selections not functioning properly post file reorganization?

After setting up my jQuery to import universal template files into various parts of my website, everything was running smoothly. However, once I restructured my filing system, things took a turn for the worse. Now, not only is my SELECT dropdown malfunctio ...

Interactive Geography Selector

When updating your personal details on , you are required to choose your country first. Once the country is selected, the system automatically adjusts the city and/or state options based on that specific country's requirements. Can someone provide exa ...

How can I vertically align text in React-bootstrap Navbar Nav-items?

I have been struggling to find a solution to my problem without resorting to custom CSS classes. I'm wondering if it's possible to achieve what I need using only flex classes. Here is the code for a simple navbar component: <Navbar bg ...

Displaying an image on a jsp page

In my current JSP, within the HTML section, I have the following: <select> <%if(size == 1)%> <option>None selected</option> <%if(size > 1)%> <option>1</option> </select> Additionally, I have this ima ...

The text in the <p> tag will remain the same color

I am encountering an issue with changing the color in the <p> tags within the code snippet below. Specifically, I am trying to change the TITLE text to gray but have been unsuccessful so far. If anyone could provide assistance on this matter, it wou ...

Struggling to create responsive embedded videos?

I have successfully created a responsive video, but there seems to be an issue with the width to height ratio when the browser is fully stretched. The iframe appears narrow and tall in this situation. However, when the browser is resized and the video move ...

What is the reason for the manual update of a view when copying an object's attributes into another object, as opposed to using Object.assign()?

In my application, I have a parent component called 'EmployeeComponent' that is responsible for displaying a list of employees. Additionally, there is a child component named 'EmployeeDetailComponent' which displays the details of the s ...

What is the best method to adjust the width of the PrimeNG ConfirmDialog widget from a logic perspective

Currently utilizing "primeng": "^11.2.0" and implementing the ConfirmDialog code below this.confirm.confirm({ header: 'Announcement', message: this.userCompany.announcement.promptMsg, acceptLabel: this.userCompany.announcement ...

Tips for efficiently printing invoices on specific paper: Print a maximum of 20 items per sheet, and if it exceeds this limit, continue onto the next page. Ensure the total amount is

$(document).ready(function(){ var j = 23; for (var i = 0; i < j+11; i++) { if (i != 0 && i % 11 == 0) { $("#printSection div").append("<?php echo '<tr><td>fff</td></tr>'; ?>"); ...