Tips for integrating leaflet maps according to the number of elements in your dataset

I'm looking to incorporate leaflet maps onto my page, under the following conditions:

If there is only one map, it should span the entire width using bootstrap grid columns. If there are two maps, they should be placed side by side with equal column widths (e.g., col-6). For three maps, two should be on the first row and one on the second row spanning the full width. And for five maps, they should be arranged in two rows with two maps in each row and one map in the third row, all stretching the entire width. (note: the number of elements varies dynamically from the backend)

.component.ts

  
      for (var j = 0; j < this.sensorsarray.length; j++){

    var v = "map" + i + "";
    var div = document.createElement("div");
    div.id = v;
      div.style = "height:380px;";
                  console.log(v);
                
                  document.getElementById('mapper').appendChild(div);


// some code 
}

.component.html

<div class="row no-gutters">
      
     <div class="col-6" [ngClass]="{ 'col-12' : array.length %2 ==1 }">
        <div id="mapper" style="height: 385px;border: 1px solid gray;"></div>  
   
        </div>
       
        
  </div>

Can someone assist me with the above requirements?

Answer №1

Here is the code snippet to tackle your problem:

In your .ts file, you have defined an array like this:

array = [1,2,3,4,5,6,7]

In your HTML file:

<div class="row no-gutters">
    <div *ngFor="let item of array; let i=index;" 
        [ngClass]="i===(array.length-1) && (array.length % 2!==0) ? 'col-12' : 'col-6'">
        <div id="mapper" style="height: 385px;border: 1px solid gray;"></div>
    </div>
</div>

Based on your updated query, I believe this could be the solution:

for (var i = 0; i < this.sensorsarray.length; i++) {
      var v = "map" + i + "";
      var div = document.createElement("div");
      div.id = v;
      document.getElementById("mapper").appendChild(div);
      var map = document.getElementById("map" + i);
      map.style.height = "380px;";
      if (
        i === this.sensorsarray.length - 1 &&
        this.sensorsarray.length % 2 !== 0
      ) {
        map.classList.add("col-12");
      } else {
        map.classList.add("col-6");
      }
      // some code
    }

<div class="row no-gutters">
    <div class="w-100">
        <div id="mapper" style="height: 385px;border: 1px solid gray;" class="row"></div>
    </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

Creating a Typescript React functional component that accepts multiple props of various types

Currently, I am utilizing a React function component to pass parameters in the following manner: interface RoleProps { roles: IRoleState dispatch: Dispatch roleListLoading: boolean } const EditPermission: React.FC<RoleProps> = ({roles, dispatc ...

No slides will be displayed in Ionic 2 once the workout is completed

Here is the result of the JSONOBJ:https://i.sstatic.net/8vyQd.png In my home.html file, I have ion-card containing a method called navigate(), which is structured as follows: navigate(event, exercise, exercise2, exercise3, exercise4){ this. ...

Mastering Angular2: Leveraging TypeScript's Power to Unleash JavaScript ES6 Syntax with

I am having trouble implementing a translation feature using the ng2-translate pipe in my Angular2/Ionic2 app, which is entirely written in JavaScript ES6. However, I have encountered an issue during the setup phase. The code snippets I have found on the ...

show additional worth on the console

Just starting out with JavaScript. Trying to display additional values in the console. Uncertain about how to access add-ons. Can anyone help me troubleshoot? Here is my code snippet below: https://jsfiddle.net/6f8upe80/ private sports: any = { ...

What is the reason behind Angular triggering @HostListener as soon as the element begins to appear in the DOM?

I encountered an issue while writing a directive to close an element when a user clicks outside of the host element. The problem seems to be that the @HostListener is triggered immediately once the element is displayed in the DOM. It might not actually be ...

Using string interpolation within the innerHTML property in Angular 2

How do I render an expression using the [innerHTML] directive? public stringInterpolation: string = 'title'; public data: any = '<a>{{stringInterpolation}}</a>'; <div [innerHTML]="data"></div> The issue is th ...

Angular 8 carousel featuring a dynamic bootstrap 4 design with multiple images and cards displayed on a single slide

I am currently working on a dynamic carousel that should display multiple cards or images in one row. Initially, I faced an issue with the next and previous buttons not functioning properly when trying to display multiple cards in one row. After some onlin ...

Angular version 10 does not allow for intentionally causing errors using the HttpClient delete() method

Currently, I'm working through an Angular tutorial and facing a challenge in the error-handling section. Despite my efforts to trigger an error with incorrect data in a HttpClient.delete()-request, I am unable to force one. To provide some context, I ...

What is the reason behind having to coerce the enum value before the object type can be properly recognized?

My object is discriminated based on the type property, which can be any value from a specified enum. I encounter an issue in TypeScript when passing a valid object to a function; it complains about mismatched types. However, coercing the enum value resolve ...

Tips for displaying an array and iterating through its children in Angular 7

I am currently working on extracting the parent and its children to an array in order to display them using ngFor. However, I am encountering an issue where the children are not being displayed during the ngFor. I have a service that retrieves data from a ...

When Angular renders for the first time, the route query parameters are found to be empty

In my current situation, I am determining actions based on the query parameters of the Angular URL. For instance, if the URL is localhost:4200/token=abc, certain tasks need to be performed, and if the URL is just localhost:4200, a different set of actions ...

Angular directive for converting fields to title case

I have created a custom directive to transform all table column data into titlecase. The directive I implemented is designed to achieve this transformation, keeping in mind that pipes are not being counted: @Directive({ selector: '[appTitleCase]' ...

Attempting to grasp the concept of Typescript generics

I have a function defined as follows: interface ExtraModels extends Model { unknown: string } const write = async (data: ExtraModels[]) => { console.log(data[0].unknown) } This function is currently working. However, I want to modify it to: cons ...

Initiate the input change event manually

Struggling with creating a custom counter input component where the input value is controlled by custom increment/decrement buttons. Desired output: https://i.sstatic.net/oYl1g.png Content projection will be used to expose the input for form usage and a ...

Tips for transferring observable to parent component in angular 2?

I have two components, SearchComponent and RuleListComponent. The Search component is a child of RuleList. https://i.stack.imgur.com/rFlM2.png I need the SearchComponent to retrieve data using APIService. This data should then be passed to RuleList as an ...

Require assistance with accurately inputting a function parameter

I developed this function specifically for embedding SVGs export function svgLoader( path: string, targetObj: ElementRef ){ let graphic = new XMLHttpRequest; graphic.open('GET', path, !0), graphic.send(), graphic.onload = (a)=> ...

Utilizing a library that solely enhances the functionality of the Array object

I have a library with type definitions structured like this: declare global { interface Array<T> { addRange<T>(elements: T[]): void; aggregate<U>(accumulator: (accum: U, value?: T, index?: number, list?: T[]) => an ...

Error TS2307: Module 'passport' is nowhere to be found

Recently, I ventured into a new project using the TypeScript Basic Node.js Express 4 Application template in VS 2017. To enhance my project, I utilized npm to add passport. The addition of passport is reflected under the npm node in Solution Explorer, wit ...

Choosing a particular checkbox with changing identifiers in Cypress: A guide

Having trouble selecting a specific checkbox without a distinct identifier? The ID keeps changing, so using it to target the checkbox directly isn't an option. Check out this image of the checkbox: https://i.stack.imgur.com/kwzAS.png This particular ...

Why is Vite's hot reloading feature displaying unpredictable outcomes?

I have a unique setup consisting of Vite, Typescript, and Vue 3 SPA utilizing "script setup". This app is equipped with Urql to query data from a GraphQL endpoint. An interesting occurrence happens where the query results are only displayed after the comp ...