Working tweaking and upgrating the code a little bit, et voila…
Your ‘Mini-Repeater’ already is capable to recognize …
-STRING-values
-BOOLEAN-values
-& NUMBERS
Everything is all about your imagination and willingness to create somethign
Additional…
This way you can improve your own REPATER…
Here in my last EXAMPLE-VERSION, our new created ‘Mini-Repeater’ is able to …
-recognize IDs as ID (now it doesn’t matter if the ID is a STRING or a NUMBER)
-also we see a new generated ID-element on our screen.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
background-color: #ffffff; /* Set a background color for better visibility */
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
}
.mainDiv {
width: calc(100% - 40px); /* 20px margin on each side */
background-color: blue;
border-radius: 10px; /* Optional: Add rounded corners */
padding: 20px;
box-sizing: border-box;
margin-bottom: 20px; /* Add margin between the two mainDivs */
}
.itemDiv {
margin: 5px;
padding: 10px;
border: 1px solid #ccc;
position: relative;
}
.title {
font-weight: bold;
color: white;
}
.idString {
position: absolute;
top: 0;
right: 0;
background-color: #f0f0f0;
padding: 5px;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<div class="mainDiv" id="mainDiv"></div>
</div>
<script>
const dataArray = [
{ _id: '123', name: 'Anna', age: 30, city: 'ExampleCity', isActive: true },
{ _id: 456, name: 'John', age: 25, city: 'SampleVille', isActive: false },
{ _id: 789, name: 'Alice', age: 35, city: 'Testburg', isActive: true },
{ _id: 101, name: 'Bob', age: 28, city: 'DemoCity', isActive: false },
{ _id: '202', name: 'Eve', age: 40, city: 'PrototypeTown', isActive: true },
{ _id: 303, name: 'Charlie', age: 32, city: 'ModelCity', isActive: false },
{ _id: 404, name: 'Mia', age: 27, city: 'ExampleVillage', isActive: true },
{ _id: 505, name: 'David', age: 45, city: 'TrialCity', isActive: false },
{ _id: 606, name: 'Sophie', age: 31, city: 'Sampleton', isActive: true },
{ _id: 707, name: 'Oliver', age: 29, city: 'Testington', isActive: false }
];
const createDivsInsideDiv = (parentDivId, dataArray) => {
const parentDiv = document.getElementById(parentDivId);
// Check if the parentDiv exists before manipulating it
if (parentDiv) {
// Loop through the array and create a div for each item
dataArray.forEach((item, index) => {
const newItemDiv = document.createElement('div');
newItemDiv.classList.add('itemDiv');
// Analyze each item and place titles for all value types
for (const key in item) {
const title = document.createElement('div');
title.classList.add('title');
if (typeof item[key] === 'string' && key !== '_id') {
title.textContent = `${key}: ${item[key]}`;
newItemDiv.appendChild(title);
}
if (typeof item[key] === 'number' && key !== '_id') {
title.textContent = `${key}: ${item[key]}`;
newItemDiv.appendChild(title);
}
if (typeof item[key] === 'boolean') {
title.textContent = `${key}: ${item[key]}`;
newItemDiv.appendChild(title);
}
}
// Convert _id to string and place it in the top right corner
const idStringDiv = document.createElement('div');
idStringDiv.classList.add('idString');
idStringDiv.textContent = `ID: ${String(item._id)}`;
newItemDiv.appendChild(idStringDiv);
// Place the item's content
parentDiv.appendChild(newItemDiv);
});
}
};
// Example usage
createDivsInsideDiv('mainDiv', dataArray);
</script>
</body>
</html>
Forgot to implement the looping-counter to find every x-element on the ‘Mini-Repeater’,
here it is…
…ok last ugrades… (let us add some interactivity to our ‘Mini-Rpeater’)…
Click onto one of the items…
…and what about some more visuality …???
What kind of functionality do → YOU ← want to add to this ‘Mini-Repeater’ ???
…CONTINUE…
A little bit simplified code for your purposes…(starting point for your project)