Show different options like selection menus, checkboxes, date pickers, and more based on the content of a JSON

Looking for Help with Dynamic Input Fields and Additional Elements

I have successfully implemented dynamic input fields based on a JSON file, but now I need to include selections, checkboxes, and a datepicker as well according to their respective groups.

I'm facing challenges in pushing these elements into computedJSON. Simply writing options: item.selection for selections does not seem to work as expected.

template:

<table>
  <tbody>
    <tr v-for="(group, key) in getComputedJson" :key="key">
      <div v-for="(item, indexer) in group" :key="indexer">
        <b-form-input v-if="item.type" :type="item.type"></b-form-input>
        <b-form-select v-if="item.selection" :options="item.selection"></b-form-select>
        <b-form-checkbox-group v-if="item.checkbox" :options="item.checkbox"></b-form-checkbox-group>
        <b-form-datepicker v-if="item.date"></b-form-datepicker>

     </div>
    </tr>
  </tbody>
</table>

script:

<script>
export default {
 computed: {
  getComputedJson() {
   const computedJson = {};
   this.json.forEach(item => {
    if(!computedJson[item.group]) {
     computedJson[item.group] = [];
     computedJson[item.group].push({label: item.label, type: item.type}); //Include selections, checkboxes, and datepickers here 
    } else {
    computedJson[item.group].push({label: item.label, type: item.type}); //And here too 
   }
  }
return computedJson;
}
</script>

updated json:

[
    {
        "label": "Input 1",
        "type": "text",
        "group": "Test1"
    },
    {
        "label": "Input 2",
        "type": "text",
        "group": "Test2"
    },
    {
        "label": "Input 3",
        "type": "text",
        "group": "Test3"
    },
    {
        "label": "Input 4",
        "type": "number",
        "group": "Test1"
    },
    {
        "label": "Selection",
        "selection": [
                { "text": "Selection 1" },
                { "text": "Selection 2" },
                { "text": "Selection 3" }
              ],
        "group": "Test2"
    },
    {
        "label": "Checkbox"",
        "selection":[
          {"text": "Checkbox 1"},
          {"text":"Checkbox 2"},
          {"text": "Checkbox 3"}],
        "group" &quo; Test1"
    }
                   ,
{
    & "label" : "Date",
    "date": "Yes",
         "gruppe": "Test3"
Although the original text involved questions regarding implementing dynamic input fields based on a JSON file, this modified version delves deeper into incorporating selections, checkboxes, and a datepicker element within the existing structure.
]

Answer №1

If you want to update your JSON, consider adjusting the conditions within the v-if and options properties as shown below:

<table>
  <tbody>
    <tr v-for="(group, key) in getComputedJson" :key="key">
      <div v-for="(item, indexer) in group" :key="indexer">
        <b-form-input v-if="item.type" :type="item.type"></b-form-input>
        <b-form-select v-if="item.label === 'Selection'" :options="item.selection"></b-form-select> // updated condition
        <b-form-checkbox-group v-if="item.label === 'Checkbox'" :options="item.selection"></b-form-checkbox-group> // updated condition
        <b-form-datepicker v-if="item.date"></b-form-datepicker>
     </div>
    </tr>
  </tbody>
</table>

Changes made to the computed property:

<script>
export default {
 computed: {
  getComputedJson() {
   const computedJson = {};
   this.json.forEach(item => {
    if(!computedJson[item.group]) {
     computedJson[item.group] = [];
     if(item.label === 'Checkbox' || item.label === 'Selection') {
      computedJson[item.group].push({label: item.label, selection: item.selection, type: item.type}); // Make sure options are included in item.selection
     } else {
      computedJson[item.group].push({label: item.label, type: item.type});
    } else {
     if(item.label === 'Checkbox' || item.label === 'Selection') {
      computedJson[item.group].push({label: item.label, selection: item.selection, type: item.type}); // Make sure options are included in item.selection
     } else {
    computedJson[item.group].push({label: item.label, type: item.type});
    }
   }
  }
return computedJson;
}
</script>

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

Is there a way to retrieve the HTML raw code using backticks for string interpolation in JavaScript?

I am working on a resume builder project where the HTML template file is stored in Firebase storage and retrieved using a URL. The template has been modified to include string interpolation, such as <h1>${name}</h1>. However, when I fetch the d ...

Retrieve the inner content of parentheses within a string, utilizing recursion for nested parentheses

I am currently working on a function that will extract words enclosed in parentheses and store them in their own array, accounting for nested parentheses recursively. For example, when given the string "((a b) ugh (one two)) pi", I would like it to be tra ...

Here is a unique version: "A guide on centering a carousel item in jquery upon being clicked."

Does anyone know how to center the item I click in a carousel? I've searched high and low for a solution but couldn't find a clear answer. Can someone please assist me with this? This is what I have tried so far: http://jsfiddle.net/sp9Jv/ Here ...

Unusual quirks in javascript variables when used with arrays

Unsure if this question has been asked previously. Nevertheless, I couldn't find it anywhere. I've observed a peculiar behavior that seems to occur only with arrays. Here is the typical behavior I anticipate from variables: var k = 10, m = ...

Tips for organizing an AngularJS bootstrap Modal for a search feature

I need help with integrating AngularJs, bootstrap, and an API to populate a bootstrap modal popover with JSON data upon clicking a search function button. While I have successfully implemented the data flow, I am struggling to trigger the modal using the b ...

AngularJS - Viewless and Issue-Free

I'm currently working on a project that involves using AngularJS and PHP. I made some changes, but now it's not functioning properly... The issue is that there are no errors in the console, Angular works (I can retrieve its version), but my vi ...

Refreshing data in Vuex store using Vue.js

Hello there, I am facing an issue with Vuex, where I retrieve data from Laravel in Json format. Specifically, when trying to fetch the logged-in user's data using their token, I store this information in the blind. Now, my dilemma lies in whether I ...

Trigger callback function when user selects a date on the calendar using Material UI X Date Picker

I am currently utilizing the DateTimePicker component in material ui, version 5. I am looking for a way to intercept the callback triggered when a user clicks on a day in the calendar selector: https://i.stack.imgur.com/0Tlogm.png Although the DateTimePi ...

Tips for enforcing validation rules at the class level using Angular's version of jQuery Validate

After utilizing jQuery Validate's convenient addClassRules function to impose a rule on all elements of a specific class, rather than relying on the attributes of their name, I encountered a roadblock when trying to do the same with the Angular wrappe ...

Tips for keeping a Youtube video playing even after the page is refreshed

Is it possible to save the current position of a Youtube video and have it resume from that point when the page is refreshed, instead of starting from the beginning? I am considering using cookies to store the last position or utilizing GET. Although my w ...

Vuetify text appearing to the right of the v-navigation-drawer

I am a beginner programmer looking to create a fixed side navigation bar, similar to an admin page. However, the router-view is not displaying correctly on the right side of the page. I realize that I have not included href links for each navigation list i ...

No response data being displayed after Angular post request

After sending a POST request via Postman, I received the expected response with a status of 400. However, when I tried sending the same request using Angular's http.post in the browser, I only received a 400 error without any response data. https://i ...

Converting October website pages' formatting into JSON for a Vue.js application

In order to create a Vue menu in OctoberCMS, I have developed a plugin that extracts the page structure from October pages into JSON data while maintaining the indentation of pages and subpages. Referring to this post: How to get static page dropdown in O ...

Implementing multiple dropdown menus with Material UI in a navigation bar

I am currently working on designing a navigation bar that consists of multiple material UI dropdown menus. However, I have encountered an issue that has proven to be quite challenging for me to resolve. Whenever I click on a navigation bar item, all the dr ...

Organizing a drop down list in alphabetical order with Angular.js is not supported

I have encountered an issue with ordering the drop down list alphabetically using Angular.js. Below is the code I am using: <div class="input-group bmargindiv1 col-md-12"> <span class="input-group-addon ndrftextwidth text-right" style="width:180p ...

Incorporation of a dynamic jQuery animation

I'm a beginner in jquery and I'm attempting to achieve the following: I want each menu to collapse separately when the mouse hovers over it. The issue is that both menus collapse simultaneously! I know it's probably something simple, but I ...

The pagination feature for array field type is malfunctioning on Mongoose, yet it functions properly on the Mongo

I am facing an issue with pagination on the rating field of my product collection. After executing a query in the mongo shell, db.products.find({_id: ObjectId('610bd9233fdc66100f703dd4')}, {ratings: {$slice: [1,1]}}).pretty(); I received the ...

Can someone guide me on how to retrieve data from a MUI table within a React project

Currently, I am retrieving data from a server in JSON format and looping through this data to display specific information. Everything is functioning as expected, but I'm encountering an issue with a Popover element that contains items with onclick ev ...

I am eager to display this JSON data using AngularJS tables

I have JSON file with content about categories, departments, and digital marketing: { "categories":[ { "dept_id":"123", "category_name":"database", "category_discription":"list of database", "current time ...

What is the best way to maintain the previous input value on page refresh in Vuejs?

In my development project, I am utilizing the Laravel and Vue.js technologies. I am facing an issue where I need to set the input value to its previous old value when either refreshing the page or navigating back to it in a Vue dynamic component. Here is ...