118 lines
4.1 KiB
HTML
118 lines
4.1 KiB
HTML
<!doctype html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Übung 3: Profilcard mit destructuring</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<div class="container py-5">
|
|
<div class="alert alert-primary">
|
|
<h2>Übung 3: Profilcard mit destructuring</h2>
|
|
<p>
|
|
Erstelle eine Funktion namens <code>createProfileCard</code>, die die Informationen einer Person
|
|
entgegennimmt und daraus eine Bootstrap Card im HTML-Format generiert. Nutze dabei die generatePersonData
|
|
Funktion, um die Personendaten zu formatieren. Die Funktion soll so angepasst werden, dass sie Destructuring
|
|
für die übergebenen Parameter verwendet.
|
|
</p>
|
|
</div>
|
|
|
|
<div class="alert alert-secondary my-3">
|
|
<!-- template: Beispiel card -->
|
|
<div class="card" style="width: 18rem">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Card title</h5>
|
|
<p class="card-text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Consequatur, obcaecati.</p>
|
|
</div>
|
|
<ul class="list-group list-group-flush">
|
|
<li class="list-group-item">Hobby 1</li>
|
|
<li class="list-group-item">Hobby 2</li>
|
|
<li class="list-group-item">Hobby 3</li>
|
|
</ul>
|
|
</div>
|
|
<!-- ▲ /card ▲ -->
|
|
</div>
|
|
|
|
<div class="output alert alert-secondary my-3">
|
|
<div class="row"></div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
<script>
|
|
'use strict';
|
|
|
|
const outputEl = document.querySelector('.output');
|
|
|
|
const person1 = { firstName: 'John', lastName: 'Wick', age: 51, hobbies: ['guns', 'dogs', 'cars'] };
|
|
const person2 = { firstName: 'Jane', lastName: 'Doe', age: 23 };
|
|
const person3 = { firstName: 'Joe', lastName: 'Doe' };
|
|
|
|
const generatePersonData = ({ firstName = '[FIRSTNAME]', lastName = '[LASTNAME]', hobbies = [], age }) => {
|
|
let html = '<div class="card mb-3">';
|
|
html += '<div class="card-body">';
|
|
html += `<h5 class="card-title">${firstName} ${lastName}</h5>`;
|
|
|
|
if (!!age) {
|
|
html += `<p class="card-text">Person is ${age} years old.</p>`;
|
|
}
|
|
|
|
if (hobbies.length > 0) {
|
|
html += `<p class="card-text">Person has the following hobbies:</p>`;
|
|
html += '</div>';
|
|
html += '<ul class="list-group list-group-flush">';
|
|
|
|
hobbies.forEach((hobby) => {
|
|
html += `<li class="list-group-item">${hobby}</li>`;
|
|
});
|
|
|
|
html += '</ul>';
|
|
} else {
|
|
html += '</div>';
|
|
}
|
|
|
|
html += '</div>';
|
|
|
|
return html;
|
|
};
|
|
|
|
// const generatePersonData2 = ({ firstName = '[FIRSTNAME]', lastName = '[LASTNAME]', hobbies = [], age }) => {
|
|
// let html = `<div class="card mb-3">
|
|
// <div class="card-body">
|
|
// <h5 class="card-title">${firstName} ${lastName}</h5>
|
|
// ${!!age ? `<p class="card-text">Person is ${age} years old.</p>`: ''}
|
|
// ${(hobbies.length > 0) ? `<p class="card-text">Person has the following hobbies:</p></div>`
|
|
// html += '<ul class="list-group list-group-flush">';
|
|
|
|
// hobbies.forEach((hobby) => {
|
|
// html += `<li class="list-group-item">${hobby}</li>`;
|
|
// });
|
|
|
|
// html += '</ul>';
|
|
// } else {
|
|
// html += '</div>';
|
|
// }
|
|
|
|
// html += '</div>';
|
|
|
|
// return html;
|
|
// };
|
|
|
|
const createProfileCard = (personObj) => {
|
|
const cardHtml = generatePersonData(personObj);
|
|
|
|
const cardContainer = document.createElement('div');
|
|
cardContainer.classList.add('col-12', 'col-md-4');
|
|
|
|
cardContainer.insertAdjacentHTML('beforeend', cardHtml);
|
|
outputEl.querySelector('.row').appendChild(cardContainer);
|
|
};
|
|
|
|
createProfileCard(person1);
|
|
createProfileCard(person2);
|
|
createProfileCard(person3);
|
|
</script>
|
|
</body>
|
|
</html>
|