Creating the necessary user interface using JSON data.Here are some steps to follow

I am struggling to create a dynamic UI based on JSON data. The issue I'm facing is with looping through the data to generate the required elements.

My Goal: Achieving a Dynamic UI from Static Code

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
  float: right;

// More CSS code here...

</code></pre>

// HTML and JavaScript code snippets here... 

</div>
</div>
</p>

<p><strong>Current Progress: Working on Dynamic Implementation</strong></p>

<p><div>
<div>
<pre class="lang-js"><code>// Some JavaScript code working on generating dynamic content based on JSON

var Data = {
  "Employ A": ["EmployA1.jpg", "EmployA2.jpg"],
  "Employ B": ["EmployB1.jpg"],
  "Employ C": ["EmployC1.jpg"]
}

// More JavaScript code here...

</code></pre>
<pre class="snippet-code-css lang-css"><code>.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
  float: right;

// More CSS code here...

</code></pre>
<pre><code><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

// More HTML code snippets here...

</button>
</div>

Currently, I am experiencing difficulties as only one counter is being displayed instead of all in the .card-header div element. It shows as 'objext-object' instead of the actual values when populating the checkboxes.

Answer №1

Your current nested loop is not functioning correctly. It's iterating over the same list of keys as seen in for key in Data. To access the array elements, you should iterate over Data[key].

Make sure to generate a new card-header and list-group for each iteration of the outer loop, where the employee name is placed within the card header. The inner loop then populates the list group with items from the arrays. Lastly, append all of this content to the .card DIV.

var Data = {
  "Employ A": ["EmployA1.jpg", "EmployA2.jpg"],
  "Employ B": ["EmployB1.jpg"],
  "Employ C": ["EmployC1.jpg"]
}

var counters = Object.keys(Data)

console.log(counters.length);
for (var key in Data) {
  var newCard = $(`<div>
      <div class="card-header"></div>
      <ul class="list-group list-group-flush">
      </ul>
    </div>`);
  var ul_innerhtml = "";
  console.log(key)
  $(".card-header", newCard).text(key);
  Data[key].forEach(d =>
    ul_innerhtml += '<li class="list-group-item">' + d + '<label class="switch "><input name="type" type="checkbox" class="success" value="' + d + '"><span class="slider round"> </span></label></li>'
  )
  $(".list-group", newCard).append(ul_innerhtml);
  $(".card").append(newCard.html());
}
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
  float: right;
}


/* Hide default HTML checkbox */

.switch input {
  display: none;
}


/* The slider */

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input.success:checked+.slider {
  background-color: #8bc34a;
}

input:checked+.slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}


/* Rounded sliders */

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div class="container">
  <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
    <div class="card" style="margin: 10px 0">
    </div>
  </div>
  <button id="btn-search" class="btn btn-default commonButton" type="submit">
<i class="fa fa-search"></i>&nbsp;Go
  </button>
</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

Retrieve the duplicated items from an array by comparing two specific properties in JavaScript

I need assistance in identifying and retrieving duplicate objects within an array that share similarities in 2 specific properties. Consider the object structure below: let arry = [ {Level: "A-1", Status: "approved"}, {Level: &q ...

Modify the contents of an array within a string using JavaScript

let message = "hello https://ggogle.com parul https://yahoo.com and http://web.com"; let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"]; I'm trying to replace all URLs in the 'message' array with "***" from the 'ur ...

Convert JSON object to a string representation

I am attempting to print a JSON object in string format. (I actually retrieved these data values from Ruby code var data = '<%=[@pro {|c| {x:c.x, y:c.y}}].to_json%>') When printing the data variable, I noticed that the values contain & ...

Is it possible to remove strings within HTML elements?

{% for term in terms %} <div class="term term-count-{{loop.index}}"> <b>{{ term }}</b>: &nbsp;&nbsp; {{ terms[term] }} &nbsp;&nbsp; </div> {% endfor %} 'terms' is a dictionary w ...

Is it possible to implement addEventListener.delay() functionality?

Hello, I am working on a Qualtrics code that utilizes jQuery. I am looking to incorporate a delay function for event listening in my code, which currently allows key presses as inputs. Specifically, I need to disable the keys for the initial 2000ms before ...

Exploring the Method of Accessing data-* Attributes in Vue.js

I have a button that, when clicked, triggers a modal to open. The content displayed in the modal is determined by the data attributes passed to the button. Here is my button: <button class="btn btn-info" data-toggle="modal" data-t ...

Creating a feature that uses a button press to initiate an Ajax request based on a specific identifier

I'm looking for the most efficient way to structure a page that involves making Ajax calls. My main issue lies in setting up the event binding for when a user clicks a button. I'm struggling to find a method to pass the ID to the function that t ...

What is the best way to integrate the express-session logic with Prisma for optimal performance?

Hi there, I recently started using Prisma and want to integrate it with PostgreSQL. My main goal is to implement authentication in my backend, but I encountered issues while trying to create a session table. When working with raw SQL, I managed to add the ...

Tips for concealing the image title when hovering over it

In this section, I will be including a title for an image that I will also need for another purpose. However, I do not want this title to be shown when the image is hovered over. Your prompt response would be greatly appreciated. Thank you in advance. ...

Interacting with a PHP script utilizing jQuery

After creating a jQuery function to submit data from a textarea, an unexpected error regarding 'recievedData' being undefined occurred. Despite using firebug to debug the script, a response was visible. Below is the jQuery function in question. ...

Issue with React-select: custom Control prevents select components from rendering

Implementing react-select@next and referring to the guide here for custom Control components is not yielding the desired outcome. import TextField from "@material-ui/core/TextField"; import Select from "react-select"; const InputComponent = (props) => ...

Switch between using a dropdownlist and textbox depending on the selection in another dropdownlist

In the process of developing an application, I have implemented a country dropdown list and a state dropdown list. Here's the scenario: when a user selects a country, the states for that country will populate in the state dropdown list. However, the ...

Is it feasible to use HTML to improve IE's tolerance for errors in markup, such as unnecessary tags?

I created a jQuery tabs element in the following way: <div id="tabs"> <ul> <li><a href="#tabs-1">Nunc tincidunt</a></li> <li><a href="#tabs-2">Proin dolor</a></li> < ...

For an optimal printing experience with Jqprint, it is recommended to utilize color output

Currently, I am exploring the jqprint plugin and am quite impressed with its functionality, except for one issue. I am trying to add color to the object that I want to print, but my attempts to colorize the object before sending it to Jqprint have been uns ...

Utilizing jQuery's Ajax functionality to extract filtered data from mySQL

I've been working on sending query strings fetched by clicking radio buttons to the server in order to receive a response in XML format. Although I'm close to finding a solution, I'm struggling to debug why it's not functioning as expec ...

Tips on retrieving the ul list value dynamically using jQuery when it changes

I have a list of countries with their dial codes and country codes. I need to find out which country is currently selected and retrieve its data-country-code using jQuery. Can anyone provide guidance on how to accomplish this? <ul class="country-list ...

Making a jQuery post request that returns JSON data

Is it possible to use the jQuery $.ajax method for making a POST request and sending data (e.g. page.aspx?var1=value) while also specifying that the expected response from the service will be in JSON format? var data = {name: _name, ...}; var request = $ ...

Is there a way to prevent the background color from filling the entire container?

In the visual representation provided below, there is a header element consisting of a back arrow and a name. The arrow container has been assigned flex: 1, while the arrow and name containers have been set to flex-start and flex-end respectively. This co ...

Audio in A-Frame does not function properly when in VR mode

My friend and I are collaborating on a small project involving a VR simulation that requires audio instructions. While everything functions smoothly in the web version and even on mobile devices, we encountered an issue when switching to VR mode on mobile ...

Is it possible to submit a form using AJAX in WordPress without relying on WordPress functions?

As I work on constructing a website using Wordpress, I am encountering an issue with submitting a form via ajax. I'm wondering if it's possible to achieve this without relying on Wordpress functions. Currently, my code runs without errors and dis ...