[英]If statement inside a Template Literal function
我瀏覽了很多關於這個問題的其他帖子,但我似乎找不到適合我特定問題的答案。 基本上我有一個顯示卡片的“應用程序”頁面。 每張卡片都是一個單獨的應用程序。 這些卡是使用 function 中的模板文字從 JavaScript object 生成的。 我有三個獨立的對象,一個用於Engineering
、 Office
和In House
。
我需要做什么?
現在,當您加載網站時,您只會看到三張代表類別的卡片。 我想根據用戶想要的類別從不同的 object 生成項目。 例如:當用戶點擊Engineering
卡上的按鈕時,只會生成approved_Engineering_Software
object里面的項目。
我遇到的問題是在模板文字函數中使用if statements
時遇到問題。 我希望能夠做這樣的事情:
function categoryTemplate(card) {
if (`${card.title}` == "Engineering"){
return `
<card class="nested">
<img class="image" src="${card.icon}">
<div class="text">
<h3>${card.title}</h3>
<p>${card.description}</p>
<button class="request-button" onclick="show_Engineering_items()">Request</button>
</div>
</card>
`;
}
}
如果您需要更多上下文,這是我的代碼:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Software Request</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body class="main-page">
<div id="category-view"></div>
<div id="app"></div>
<script src="script.js"></script>
</body>
</html>
JavaScript
const main_Category_View = [
{
title: "Engineering",
description: "From Autocad to something else that I will write in later, all your Enginerring Software is found here",
icon: "Assets/docker1.png"
},
{
title: "Office",
description: "General purpose office applications e.g Microsoft Office, Excel, Powerpoint, Visio etc",
icon: "Assets/docker1.png"
},
{
title: "In House",
description: "Apps build by In House specifically for In House",
icon: "Assets/docker1.png"
}
];
const approved_Engineering_Software = [
{
title: "Microsoft Azure",
description: "Microsoft Azure is a cloud computing service created by Microsoft for building, testing, deploying, and managing\
applications and services through Microsoft-managed data centers.",
icon: "Assets/azure.png"
},
{
title: "Visual Studio Code",
description: "Visual Studio Code is a source-code editor developed by Microsoft for Windows, Linux and macOS. It includes support \
for debugging, embedded Git control and GitHub, and code refactoring.",
icon: "Assets/vscode.png"
},
];
const approved_Office_Software = [
{
title: "Microsoft Word",
description: "Microsoft Word is a word processor developed by Microsoft. It was first released on October 25, 1983 under the name Multi-Tool Word for Xenix systems.",
icon: "Assets/word.png"
}
];
//sorts everything perfectly
var sortByProperty = function (property) {
return function (x, y) {
//basically returns nothing if they are equal,
//or returns the item that is greater than the lesser
return ((x[property] === y[property]) ? 0 : ((x[property] > y[property]) ? 1 : -1));
};
};
// completes the rest of the structure of the cards
function softTemplate(item) {
return `
<card class="nested">
<img class="image" src="${item.icon}">
<div class="text">
<!--<button class="request-button" onclick="getDescription('${item.description}')">Request</button> -->
<h3>${item.title}</h3>
<p>${item.description}</p>
<button class="request-button" onclick="getDesc()">Request</button>
</div>
</card>
`;
}
function categoryTemplate(card) {
return `
<card class="nested">
<img class="image" src="${card.icon}">
<div class="text">
<!--<button class="request-button" onclick="getDescription('${card.description}')">Request</button> -->
<h3>${card.title}</h3>
<p>${card.description}</p>
<button class="request-button" onclick="show_items()">Request</button>
</div>
</card>
`;
}
document.getElementById("category-view").innerHTML = `
<div class="stack">
<main class="grid">
${main_Category_View.map(categoryTemplate).join("")}
</main>
</div>
`;
let sorted_Office_List = approved_Office_Software.sort(sortByProperty('title'));
let sorted_Engineering_List = approved_Engineering_Software.sort(sortByProperty('title'));
// Sets up the structure of the page and sends our Template Literal to two functions for work.
function show_items() {
document.getElementById("app").innerHTML = `
<div class="stack">
<main class="grid">
${sorted_Office_List.map(getDesc).join("")}
${sorted_Office_List.map(softTemplate).join("")}
</main>
</div>
`;
}
function show_Engineering_items() {
document.getElementById("app").innerHTML = `
<div class="stack">
<main class="grid">
${sorted_Office_List.map(softTemplate).join("")}
</main>
</div>
`;
}
模板文字將被翻譯為字符串。 你有:
if (`${card.description} == "Engineering"`)
這將被翻譯為:
if('Engineering == "Engineering"')
非空字符串是真實的; 你想要做的可能是:
if (card.description === "Engineering")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.