Home > Mobile >  Change v-text-field text position when a certain condition is met
Change v-text-field text position when a certain condition is met

Time:10-24

I am using vue js with vuetify. Came across an interesting scenario . So i have three options (radio-buttons)

  • Left
  • Center
  • Right

And i have a v-text-field with a certain text as value and readonly . Now i want to change the position of that text inside v-text-field when option is changed/selected.

So for instance, upon option 1(left) text should be at moved at left inside v-text-field. Upon option 2(centre) text should be moved at center. And goes on.

Any suggestions for that. Also guide if you have a better approach

CodePudding user response:

You can use class or style bindings from vue

data() {
  return {
    activeAlignment: 'center'
  }
}

<div :style="{text-align : activeAlignment} ></div>

And just bind activeAlignment to your radio buttons model

CodePudding user response:

You can create classes and bind them:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    text: 'some text',
    align: ''
  })
})
.left .v-text-field__slot input {
  text-align: left;
}
.center .v-text-field__slot input {
  text-align: center;
}
.right .v-text-field__slot input {
  text-align: right;
}
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div id="app">
  <v-app>
    <v-main>
      <v-container>
        <v-btn-toggle
          v-model="align"
          tile
          color="deep-purple accent-3"
          group
        >
          <v-btn value="left">Left</v-btn>
          <v-btn value="center">Center</v-btn>
          <v-btn value="right">Right</v-btn>
        </v-btn-toggle>
        <v-text-field
          :
          v-model="text"
          label="text"
        ></v-text-field>
        <v-text-field
          v-model="text"
          label="text"
        ></v-text-field>
      </v-container>
    </v-main>
  </v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>

  • Related