Having a text input that is dependent on choosing a particular item from a list

As someone who is new to VueJs and JS, I have a question regarding my page setup. I want a text input box to only be visible when the user selects the document type 'Write Individual'. However, my current syntax seems to be incorrect as it's not displaying the expected behavior. Any advice on how to proceed would be greatly appreciated!

Vue

    <template v-if="attachmentAccepted">
  <b-field
    :label="t('document_type')"
  >
    <b-select
      v-model="documentType"
      icon="file-alt"
      :disabled="waiting.updateAttachment"
    >
      <optgroup
        v-for="group in allDocumentTypeGroups"
        :key="group.id"
        :label="t(group.name)"
      >
        <option
          v-for="type in group.documentTypes"
          :key="type.id"
          :value="type.id"
        >
          {{(type.name)}}
        </option>
      </optgroup>
    </b-select>

  </b-field>

My main struggle lies in accessing the JSON data containing document types. I need to view this parsed data so I can effectively use it in my v-if statement. Unfortunately, being new to Vue, I am unsure of how to achieve this. Using console.log(this.documentTypes) hasn't provided me with the desired outcome.

.then(({data}) => {
        this.participants = data.participants;
        this.installmentData = data.installments;
        this.installmentData = data.installments;
        this.installmentData.paymentDateStart = moment(data.installments.paymentDateStart).format('YYYY-MM-DD');
        this.installmentData.paymentDateEnd = moment(data.installments.paymentDateEnd).format('YYYY-MM-DD');
        this.documentTypes = JSON.parse(data.documentTypes);
        this.formattedDocumentTypes = JSON.parse(data.formattedDocumentTypes);
        this.loading = false;
      })

Edit: Props + Data

export default {
  name: 'AttachmentEditForm',
  mixins: [AttachmentTypeMixin],
  components: { DocumentCheckbox },
  props: {
    attachment: Object,
    slideIndex: Number,
  },
  data() {
    return {
      waiting: {},
    }
  },

Answer №1

Begin by initializing your data within the data function:

    data() {
    return {
      waiting: {},
      participants: null,
      installmentData: {
        paymentDateStart: null,
        paymentDateEnd: null,
      },
      documentTypes: null,
      formattedDocumentTypes: null,
      loading: false
    }
  },

Answer №2

Why not try logging the data.documentTypes using console.log() within the .then() function to troubleshoot your documentTypes first?

When it comes to displaying the text field conditionally, you are correct in using v-if. You just need to determine which specific documentType value should make the field visible and ensure that it is properly checked. This documentType value is typically set by selecting an item from a list, so make sure it is included in your data{} section (if it's missing). Here's an example:

<template v-if="attachmentAccepted">
  <b-field
    v-if="documentType == 'some_value_to_make_this_true'"
    :label="t('document_type')"
  >
    <b-select
      v-model="documentType"
      icon="file-alt"
      :disabled="waiting.updateAttachment"
    >
      <optgroup
        v-for="group in allDocumentTypeGroups"
...

export default {
  name: 'AttachmentEditForm',
  mixins: [AttachmentTypeMixin],
  components: { DocumentCheckbox },
  props: {
    attachment: Object,
    slideIndex: Number,
  },
  data() {
    return {
      documentType: null,
      waiting: {},
    }
  },

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

Struggling with undefined values in scope within angular-ui modal

I am having difficulties with referencing the scope in an angular-ui modal. Whenever I try to access the value of the scope, I receive an undefined message. My goal is to retrieve the data entered into ng-model="input.cost_center". Despite trying examples ...

What is the best way to upload a file in Node.js using Express and Multer?

When attempting to send a file from the front end to my node js server, I encountered an issue with receiving the file on the back end. Here is the code snippet: <form id="file-upload-form" class="uploader" action="/uploa ...

Choosing a category depending on the division being used in Jquery

<div class = ui-dialog-abc ui-dialog-xyz> <div id = "sheet1abc"> </div> </div> <div class = ui-dialog-abc ui-dialog-xyz> <div id = "sheet1xyz"> </div> </div> <div class = ui-dialog-abc ui-dialog-xyz> ...

What is the best way to show a block of text when hovering over an image with a smooth easing effect

How can I make a block of text appear with ease when hovering over an image of a tree? The current setup works, but the transition effect is not as smooth as I want it to be: HTML: <p id="hover-text1" class="hover-text1"> Accomplished! </p> & ...

What causes inline CSS and internal CSS to exhibit different behaviors?

It’s kind of strange that I have to click twice to see Foo’s content, but only once to see Bar’s content. The main difference seems to be that Foo’s content has internal style while Bar has inline style. <html> <head> <style> ...

Spin the sphere texture in Three.js

Currently, I am working on rendering a sphere through three.js. While applying a texture to the sphere works well, I am having some difficulties rotating the texture to align it with marker positions. The issue arises when trying to place markers over Kag ...

Finding the position of a specific element in an array by searching through multiple object keys

Assuming I have a single key (for example, if I have the object.name = 'Sam') and I want to find the index using: var index = array.map(function(el) {return el.name}).indexOf('Sam'); I can retrieve the index of the array element with ...

There seems to be an issue with byRole as it is failing to return

Currently in the process of migrating my unit test cases from Jest and Enzyme to React Testing Library. I am working with Material UI's Select component and need to trigger the mouseDown event on the corresponding div to open the dropdown. In my previ ...

Obtaining td values from a dynamically generated table can be achieved by utilizing JavaScript

I have searched through many questions, but none of them directly addresses my issue. The scenario in question involves allowing the user to open a jQuery Dialog where a table is generated with data pulled from a database. When the user makes a selection, ...

Transferring Django ORM data to Javascript for assessment

I have a situation where an ORM call retrieves data from a database. This data is then passed to an HTML template, but I also need it for a JavaScript function call. The process works as follows: The form loads and triggers the ORM call to fetch values f ...

Creating a connection between a class and a boolean evaluation in Vue.js

I'm working with a set of buttons (using vue-mdl) that function as a tab switcher, meaning there is always one 'current' tab in view. I want to apply a class to the active button without using a computed property. The computed property I hav ...

What is the best method for inserting a line break into a string?

Can you please explain how I can insert a line break into a string with JavaScript? I have tried using the <br/> tag, but it's not working. What is the correct way to achieve this? JavaScript var str="Click Here" +"<br>"+ "To Set" +"< ...

Wrap it in a ReactJS container tag

Today I encountered a frustrating issue while diving into ReactJS. I'm excited to learn by getting my hands dirty, but unfortunately, I keep running into this error: Adjacent JSX elements must be wrapped in an enclosing tag (47:14) And here's t ...

Error TS7053 indicates that an element is implicitly assigned the 'any' type when trying to use a 'string' type to index a 'User_Economy' type

Struggling with a particular question, but can't seem to find a solution. Error: TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'User_Economy'. No ind ...

What happens when you click and trigger mouseleave?

window.onload = function() { $(".compartir").hover(function() { console.log('hover'); var self = this; setTimeout($(self).addClass('ready'), 500); }, function() { var self = this; console.log('leave'); ...

Exiting the modal/popup box is currently disabled

I am struggling to figure out why the modal box/pop up is not closing when I click 'x'. It seems completely unresponsive. Check out the JavaScript code below: var kite = document.getElementById("poptext"); var kitetwo = document.getElementById( ...

Modifying the route category through parsing information from a JSON file

As a disclaimer, I must admit that I am venturing into unfamiliar territory here. Due to the ongoing restructuring at my workplace, I am required to take on additional tasks, such as creating visualizations. Here's the issue I'm facing: I have a ...

Interactive search tool with multiple fields using HTML and JavaScript

I need help developing a search box for structured data, where I want to implement two types of typeahead searches: one for fields and another for values within those fields. The image below illustrates what I am aiming for. https://i.sstatic.net/MRsJ2.png ...

ways to display view without page refresh in mvc3

@using (Html.BeginForm("Index", "HRBankInfo", FormMethod.Get)) { <div align="center" class="display-label"> @ViewBag.message <br /><input type="submit" value="Ok" /> </div> } This particular partial view is display ...

Having trouble compiling Vue.js for Internet Explorer 11

After seeking help on Stack Overflow and conducting extensive research, I have finally configured my babel.rc file as follows: { "presets": [["env", { "modules": false, "uglify": true, "targets": { "browsers": ["> 1%", "last 2 versi ...