Wei WeiDeng
Wei WeiDeng

Reputation: 31

Vue3 "conditional" rendering with <select> changed

I am pretty new to the wonderful world of Vue3(or Vue), and I encountered a problem with "conditional" rendering with select changed. What I want to achieve is "different value of select will show different div for argument setting". However, my code do nothing when I changed the value of select, can anybody give me a hint? The following is my .vue file code:

  <div class="">

    <div class="">
      <p>Data integration type: 
        <select name="" id="" v-on:change="onchange" v-model="integration_type">
          <option value="">--Please choose one--</option>
          <option value="1+2">1+2</option>
          <option value="1+1">1+1</option>
        </select>
      </p>
    </div>

    <div class="" v-bind:style="display1">
      <p>Arguments for 1+2: 
        <input type="text">
      </p>
      <br>
      <button v-on:click="onclick">start integration</button>
    </div>

    <div class="" v-bind:style="display2">
          <p>Arguments for 1+1:
        <input type="text">
      </p>
      <br>
      <button v-on:click="onclick">start integration</button>
    </div>

  </div>
</template>

<script setup lang="ts">
let integration_type = ""
let display1 = "display:none;"
let display2 = "display:none;"

const onchange = function(){
  if(integration_type == "1+2"){
    display1 = "display:inline;"
    display2 = "display:none;"
  }else{
    display1 = "display:none;"
    display2 = "display:inline;"
  }
}

const onclick = function(){

}

</script>

<style scoped>

</style>

Upvotes: 0

Views: 534

Answers (1)

Nikola Pavicevic
Nikola Pavicevic

Reputation: 23490

Try like following snippet:

const { ref } = Vue
const app = Vue.createApp({
  setup() {
    let integration_type = ref("")
    let display1 = ref("display:none;")
    let display2 = ref("display:none;")

    const onchange = function(){
      if (integration_type.value == "1+2"){
        display1.value = "display:inline;"
        display2.value = "display:none;"
      } else {
        display1.value = "display:none;"
        display2.value = "display:inline;"
      }
    }

    const onclick = function(){

    }
    
    return {
      display1, display2, integration_type, onchange
    }
  }
})
app.mount('#demo')
<script src="https://unpkg.com/[email protected]/dist/vue.global.prod.js"></script>
<div id="demo" class="">

    <div class="">
      <p>Data integration type: 
        <select name="" id="" @change="onchange" v-model="integration_type">
          <option value="">--Please choose one--</option>
          <option value="1+2">1+2</option>
          <option value="1+1">1+1</option>
        </select>
      </p>
    </div>

    <div class="" :style="display1">
      <p>Arguments for 1+2: 
        <input type="text">
      </p>
      <br>
      <button @click="onclick">start integration</button>
    </div>

    <div class="" :style="display2">
          <p>Arguments for 1+1:
        <input type="text">
      </p>
      <br>
      <button @click="onclick">start integration</button>
    </div>

  </div>
</div>

Upvotes: 1

Related Questions