简体   繁体   中英

If statement inside a Template Literal function

I have looked through a lot of other posts regarding this issue but I cant seem to find an answer that fits my particular problem. Basically I have an "app" page that displays cards. Each card is an individual app. These cards are generated from a JavaScript object using template literals inside a function. I have three separate objects, one for Engineering , Office and In House .

What do I need to do?

Right now, when you load the site, you are met with only three cards that represent categories. I want to generate items from a different object depending on what category the user wants. For example: when the user clicks the button on the Engineering card, only items inside the approved_Engineering_Software object will be generated.

The problem I am having is that I am having trouble using if statements inside my template literal functions. I want to be able to do something like this:

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>
  `;
 }
}

I have checked here here and here but none of the answers given in those links have successfully worked.

Here is my code if you need more context:

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>
  `;
}

a template literal will be translated as a string. you have:

if (`${card.description} == "Engineering"`)

that will be translated as:

if('Engineering == "Engineering"')

Non-empty strings are truthy; what you want to do is probably:

if (card.description === "Engineering")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM