Guide to Dynamically Append Elements in Vue.js Based on Specific Conditions

Can anyone assist me in figuring out how to change the append outer icon based on 2 conditions? I've attempted using the code below but it's not working. Also, is there a way to determine if the input is a NUMBER within the component?

<v-text-field
  flat
  solo
  maxlength=16
  :append-outer-icon="form.ktp.length !== 16 && isInteger(form.ktp) ? '' : 'mdi-checkbox-marked-circle'"
  v-model="form.ktp"
></v-text-field>

Answer №1

After trying various methods, I was able to find a solution on my own by implementing v-text-field slot append outer and restricting input to only numbers in my text field using the following code.

<v-text-field
  flat
  solo
  maxlength=16
  @keypress="isNumber($event)"
  onpaste="return false;"
  v-model="form.ktp"
>
   <template slot="append-outer">
     <v-icon v-if="form.ktp.length === 16" color="green darken-2">mdi-checkbox-marked-circle</v-icon>
   </template>
</v-text-field>

Below is the script that handles this functionality:

isNumber(evt)
  {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      evt.preventDefault();;
    } else {
      return true;
    }
  },

Answer №2

According to the documentation found at this link, it is necessary to utilize Number.isInteger when using the isInteger method.

To properly implement this, make sure to format it as shown below:

<v-text-field
  flat
  solo
  maxlength=16
  :append-outer-icon="form.ktp.length !== 16 && Number.isInteger(form.ktp) ? '' : 'mdi-checkbox-marked-circle'"
  v-model="form.ktp"
></v-text-field>

Answer №3

To ensure that your v-model is cast as a Number when the input string is valid, utilize the .numbers modifier. https://v2.vuejs.org/v2/guide/forms.html#number

Next, incorporate your icon condition within a computed property for better functionality.

<v-text-field
  flat
  solo
  maxlength=16
  :append-outer-icon="iconName"
  v-model.number="form.ktp"
/>


.....

computed: {
  iconName() {
    return !(this.form.ktp && this.form.ktp.length !== 16) && 'mdi-checkbox-marked-circle'
  }
}

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

What are the steps to utilizing the $document service in AngularJS?

I am a complete beginner when it comes to AngularJS and JavaScript in general, and I find myself a bit confused about the usage of $document. From what I understand, $document provides access to some JQuery functions. So, if I want to remove an element tha ...

AngularJS ng-model directive binds an input field with a specific value

I encounter a challenge where my inputs, containing values, do not display the value when I apply an ng-model to each input field. Below is the snippet of my code: student-list.html <!--edit form--> <div class="col s12" ng-show="dataForm"> ...

CSS responsive grid - adding a horizontal separator between rows

I currently have a responsive layout featuring a grid of content blocks. For desktop view, each row consists of 4 blocks. When viewed on a tablet, each row displays 3 blocks. On mobile devices, each row showcases 2 blocks only. I am looking to add a ho ...

Transforming dates in JavaScript

So I have a situation where I need to call php from javascript. The URL address is Jun 18 18:00:00 UTC+0200 in the year 2013, but that format is not suitable for my needs. I want to convert it to the format YYYY-MM-DD, either using JavaScript or PHP. An ...

The Bootstrap datepicker functions properly when used in plain HTML, but encounters issues when implemented within the .html()

Using HTML: <input class="m-ctrl-medium date-picker" size="16" type="text" id='lateETD1' name="dateRecv" value=""/> Not working with script: var ETD = $("#ETD"); ETD.html("<input class='m-ctrl-medium date-picker' id='la ...

Validation of the top-level URL

How can I validate or filter only the top-level URL/domain using JavaScript or PHP? This filter should also accept new domain extensions, for example: Valid URL: and so on... Invalid URL: and so on... Subdomains are also not allowed! I need this val ...

Is it possible to incorporate a React app as a component within a static HTML page?

Looking to integrate the react-csv-viewer component into a static HTML page without deploying it on a local server. I've tried following the instructions from the React tutorial, but am struggling with the build process. My goal is simple - to use th ...

Passing events between sibling components in Angular 2Incorporating event emission in

Having some trouble emitting an event from one component to another sibling component. I've attempted using @Output, but it seems to only emit the event to the parent component. Any suggestions? ...

Incorporate additional information into the current data series within Lightning Chart

In my React JS application, I have successfully used a point series to create a chart displaying data. However, I am now looking to bind real-time data to the chart. For example, when the chart initially loads with 10 points, I want to continuously receiv ...

Passing props to component data in Vuejs: Best practices

I'm experimenting with Vue 2.0 and I've encountered a bit of confusion. How can I properly pass props to a component's internal data? I've followed the documentation, but it still seems unclear. HTML <service-list :services="model"& ...

Changing a single state in React results in the modification of both states simultaneously

Whenever I attempt to modify one state, I find that another state is inexplicably changing as well. I've scoured my code for the issue but can't seem to pinpoint it. What steps should I take next? Here's the snippet of code in question: impo ...

What are the pros and cons of passing an imported object from a parent component to its children as props versus directly importing that object within the children components?

My current project involves a helper object known as TimeHelper, which is used for time-related tasks. This object is required in multiple components within the top-level parent component. I am contemplating whether it would be advantageous to import Time ...

What is the best way to design a drop-down menu that resembles the Facebook notifications drop-down?

I'm looking to implement a dropdown menu on my website that is similar to the DB dropdown for notifications. Any suggestions for JS libraries that could help with this would be greatly appreciated. ...

Is it possible to share data from one controller in a JS file to another JS file? (using AngularJS)

I need to create a table in JavaScript. In one of my JS files, I have a controller with JSON data containing properties like width, height, color, etc. In another JS file, I am building the actual table structure. Here is an example of my AngularJS file: ...

"Retrieve the x-coordinate position of a box within a specific div using

https://i.sstatic.net/68yeF.jpg https://i.sstatic.net/ZCrxZ.jpg Is there a way for me to move the box within the gray area? <div id="timeline"> <div cdkDragBoundary="#timeline" ...

When providing the index.html file using express.js, remember to include the user-agent header

When using my Express.js app to render my index.html page, I want to customize the http 'User-Agent' header. </p> <p>I've tried this method without success:</p> <pre><code>req.headers['user-agent'] = ...

What is the reason that JavaScript does not function properly in dark mode?

I would like the dark mode button to switch to CSS when clicked, so that in waiting mode the button appears dark and in dark mode the button is light! var toggleButton = document.getElementById('mode-toggle') var isDarkMode = false; functio ...

Running into a problem with NPM installation that's causing it to stall and throw an error message (specifically: connect ENET

Has anyone else encountered an issue where running NPM install gets stuck on idealTree buildDeps and displays the following error? npm ERR! code ENETUNREACH npm ERR! syscall connect npm ERR! errno ENETUNREACH npm ERR! request to https ...

Tips for managing the background color of a box when the function is constantly refreshing in Internet Explorer

function process(Objects) { var objectId = Objects; displayContent(objectId); } function displayContent(objectId) { var boxId = objectId.id; var color = ret ...

The wrapping of cells in React Material UI GridList is not functioning properly

While working with Material-UI GridList, I've encountered an issue where the cells don't wrap correctly within the grid. Upon inspecting the CSS, I noticed that it utilizes flex-wrap, which is intended to flex rows. However, I believe there must ...