0
\$\begingroup\$

I'm creating an option checkbox for an Organization data only if they have Organization data to submit from their CSV. I am creating a <div> and my <select> and <option> are wrapped inside of the <div>.

Do I need to create an input ? I'm in Vue.js and don't see any current methods or v-if nested in the right way. I want this selection of this checkbox to be done upon upload of the CSV and prior to submit.

HTML

<div class="large-12 medium-12 small-12 cell">
  <input type="checkbox" id="organization" ref="Organization" v-model="checkbox" :v-if="field in requiredOrganizationFields"
  <h1 class="mb-7 mt-5 ml-6 font-bold text-2xl"> Organization Fields</h1>
  <div v-for="field in requiredOrganizationFields" class="p-8 -mr-6 -mb-6 flex flex-wrap" style="width:150px">
    <label :for="field" type="select" name="field">{{field}}</label>
    <select :id="field" :name="field" v-model="mapping[field]" required>
      <option value="">Please Select</option>
      <option v-for="columnName in columnNames" :name="columnName" :value="columnName"  value="">{{columnName}}</option>
    </select>
  </div>
</div>

JavaScript

    data: null,
          columnNames: [],




  file: null,
  requiredContactFields: [ 
    'contact_account_name',
    'contact_first_name',
    'contact_last_name'
      ],
  optionalContactFields: [
    'contact_email',
    'contact_phone',
    'contact_address',
    'contact_city',
    'contact_region',
    'contact_postal_code'
      ],
  requiredOrganizationFields: [
      'organization_account_name',
      'organization_name'
      ],
  optionalOrganizationFields: [
      'organization_email',
      'organization_phone',
      'organization_address',
      'organization_city',
      'organization_region',
      'organization_postal_code'

      ]    

}





getColumnNames() {
  let formData = new FormData();
  formData.append('file', this.file);
  let self = this;
  axios
  .post('http://localhost:8080/api/contacts/csv-column-names',
  formData,
  {
    headers: {
      'Content-Type': 'multipart/form-data'

    }
  }

  )
  .then(function(response){
    console.log('SUCCESS!!');
    console.log(response);
    //Need array of column names you get from the back-end
    self.columnNames = response.data.column_names;


  })
  .catch(function(response){
    console.log('FAILURE!!');
    console.log(response);

  })


},
\$\endgroup\$
3
  • 1
    \$\begingroup\$ @tieskedh That open tag in my Javascript is meant to be empty to allow user data to be passed through prior to submission. It is a problem that can be improved through nesting the v-if in the correct tag. Also the way someone simulates a shouting experience is by ALL-CAPS, not bolded letters \$\endgroup\$
    – Vick7
    Mar 11, 2020 at 17:04
  • 2
    \$\begingroup\$ Do you want a review of the code provided or are you more interested in the addition of a specific feature? Please clarify after looking at our help center. \$\endgroup\$
    – Mast
    Mar 12, 2020 at 10:45
  • \$\begingroup\$ Addition on specific features. There are no clear examples a single checkbox for this use case \$\endgroup\$
    – Vick7
    Mar 12, 2020 at 18:13

1 Answer 1

2
\$\begingroup\$

If you want to allow a user to be able to input an optional field after checking that check box it will go like this

You bind the field you want to make optional in your v-model. Upon the click event that optional field should dropdown

HTML

<label></label>
  <input type="checkbox" name="show_organization_fields" v-model="showOrganizationFields">
  <div v-if="showOrganizationFields">
   <h1 class="mb-7 mt-5 ml-6 font-bold text-2xl">Organization Fields</h1> 
   <div v-for="field in requiredOrganizationFields" class="p-8 -mr-6 -mb-6 flex flex-wrap" style="width:150px">
    <label :for="field" type="select" name="field">{{field}}</label>
    <select :id="field" :name="field" v-model="mapping[field]" required>
      <option value="">Please Select</option>
      <option v-for="columnName in columnNames" :name="columnName" :value="columnName"  value="">{{columnName}}</option>
    </select>
  </div>

Javascript

data: null,
      columnNames: [],



      showOrganizationFields: false,


      file: null,
      requiredContactFields: [ 
        'contact_account_name',
        'contact_first_name',
        'contact_last_name'
          ],
      optionalContactFields: [
        'contact_email',
        'contact_phone',
        'contact_address',
        'contact_city',
        'contact_region',
        'contact_postal_code'
          ],
      requiredOrganizationFields: [
          'organization_account_name',
          'organization_name'
          ],
      optionalOrganizationFields: [
          'organization_email',
          'organization_phone',
          'organization_address',
          'organization_city',
          'organization_region',
          'organization_postal_code'

          ]    
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.