Home > Software design >  display selection, checkboxes, datepicker, etc. dynamically based on json file
display selection, checkboxes, datepicker, etc. dynamically based on json file

Time:10-08

Additional question to a question of me before

I'm displaying input fields dynamically based on my json file - now I want to display selections, checkboxes and a datepicker as well based on their groups.

How I'm able to solve this - I need to push these elements into computedJSON, but writting for example to selection options: item.selection is not working.

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}); //Need to input here my selection, checkbox and datepicker 
    } else {
    computedJson[item.group].push({label: item.label, type: item.type}); //Here too 
   }
  }
return computedJson;
}
</script>

new 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": "Test1"
    },
    {
        "label": "Date",
        "date": "yes",
        "gruppe": "Test3"
    }
]

CodePudding user response:

As per your new json try changing the condition in v-if and options prop as 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> // change added
        <b-form-checkbox-group v-if="item.label === 'Checkbox'" :options="item.selection"></b-form-checkbox-group> // change added
        <b-form-datepicker v-if="item.date"></b-form-datepicker>
     </div>
    </tr>
  </tbody>
</table>

Modified 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}); // Provided you should have the options 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}); // Provided you should have the options in item.selection
     } else {
    computedJson[item.group].push({label: item.label, type: item.type});
    }
   }
  }
return computedJson;
}
</script>
  • Related