【问题标题】:Vuejs Hide a div when v-model is emptyVuejs在v-model为空时隐藏一个div
【发布时间】:2020-05-13 20:00:34
【问题描述】:

我有一个这样的选择下拉菜单。

<select v-model="selectedInjection">
    <option v-for="(match,i) in haufigkeitMatches"
        :key="i"
        :value="match.value" >{{ match.name }}
    </option>
</select>

我是这样渲染值的

<td v-if="selectedInjection">{{Math.round(selectedInjection)}</td>

{{Math.round(selectedInjection)} 的值会随着选择值的变化而变化,并且工作正常。但是当我没有选择任何值时,{{Math.round(selectedInjection)} 会显示旧的选择值,除非我选择一个新值。 选择值为空时如何隐藏{{Math.round(selectedInjection)}

这是 Jsfiddle https://jsfiddle.net/ey3scra0/

【问题讨论】:

  • 你可以试试你的 v-if 条件:v-if="selectedInjection.length>0"。
  • 我假设选项是从 API 填充的。在这种情况下,检查 hausfigkeitMatches.length > 0。否则检查 selectedInjection 的初始值。例如。如果 selectedInjection 的初始值为 ''。检查 selectedInjection !== ''
  • 谨慎使用循环中的索引作为键。如果您这样做,Vue 将失去跟踪,并可能导致错误..
  • v-if="selectedInjection.length>0" 给我错误Cannot read property 'length' of null我已经编辑了我的问题并附上了一个jsfiddle

标签: vue.js vuejs2 vuejs3


【解决方案1】:

最好在你的 div 中使用v-show

<div class="hideResult" v-show="showSelectedInjection && selectedProduct != ''">
   {{Math.round(selectedInjection)}}
</div>

然后在第二次选择时添加@change:

<select v-model="selectedInjection" @change="setShowSelectedInjection">

您还需要使用方法setShowSelectedInjection 添加额外的字段showSelectedInjection 并在setSelectsToDefault 中设置showSelectedInjection

data:{
showSelectedInjection: false,

(...)

methods:{
            setSelectsToDefault(){
                this.selectedIeProKg = 0;
                this.selectedPreisProIE = 0;
                this.showSelectedInjection = this.haufigkeitMatches.map(h => h.value).includes(this.selectedInjection);
            },
            setShowSelectedInjection(){
                this.showSelectedInjection = true;
            }

这是一个工作示例:JSFiddle

【讨论】:

  • 嗨,v-show="selectedInjection &gt; 0" 没有帮助。您可以在我的问题中检查 jsfiddle。如果我设置了this.selectedInjection = 0,那么它会重置我不应该设置的v-model="selectedInjection"。当我第一次选择然后第二次选择时,它会输出一个值。当我将第一个选择重置为空选项时,第二个第二个选择确实会自动变为空。但是{{Math.round(selectedInjection)}} 会留下来。
  • 更新了答案和 JSFiddle 链接 - 现在您的 selectedInjection 模型未重置。我添加了额外的字段 showSelectedInjection 。
  • 非常感谢。这就像 90% 一样,当我设置第一个选择和第二个选择时,它会给出一个值,然后当我将第一个选择设置为空选项时,第二个选择也会变空,这很好但仍然是值正在闲逛。
  • 更新了答案和 JSFiddle 链接 - 我在 v-show 中为该边缘情况添加了额外条件:v-show="showSelectedInjection &amp;&amp; selectedProduct != ''"。我希望它现在可以 100% 工作。
【解决方案2】:

我不知道它对您的问题有多大影响,因为您没有提供应用程序中数据如何变化的示例。如果一个变量依赖于另一个变量,你必须观察它并控制它的变化。就像我这里一样,我跟随一个变量,检查它是否为空,然后重置第二个变量。如果还是不行,输入更多的数据,一段更大的代码,那我也会调整答案。

new Vue({
  data: {
    selectedInjection: null,
    haufigkeitMatches: []
  },
  computed: {
    round() {
      return Math.round(this.selectedInjection);
    }
  },
  watch: {
    haufigkeitMatches(val) {
      if (!val.length) {
        this.selectedInjection = null;
      }
    }
  },
  methods: {
    add() {
      this.haufigkeitMatches.push({
        name: 'Fus',
        value: 1.1
      }, {
        name: 'Ro',
        value: 2.2
      }, {
        name: 'Dah',
        value: 3.3
      })
    },
    del() {
      this.haufigkeitMatches = [];
    }
  }

}).$mount('#app');
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <select v-model="selectedInjection">
    <option v-for="(match,i) in haufigkeitMatches" :key="i" :value="match.value">{{ match.name }}</option>
  </select>
  <div v-show="selectedInjection">{{round}}</div>
  <div>DIV is {{selectedInjection ? 'visible' : 'invisible'}}</div>
  <button @click="add">Add</button>
  <button @click="del">Del</button>
</div>

【讨论】:

  • 你应该提供更多的代码,最好插入一个代码sn-p,这样更容易回答问题。
  • 感谢您的努力,我已经编辑了我的问题,并在其中附加了 jsfiddle
【解决方案3】:

这对我有用。每次都在清除旧值。请试试这个。

<div id="app">
                  <select v-model="selectedProduct" @change="setSelectsToDefault">
                    <option value=""></option>
                    <option v-for="(level1,index) in products"
                            :key="index"
                            :value="level1">{{level1.name}}</option>
                </select>
                <select v-model="selectedInjection">
                    <option v-for="(match,i) in haufigkeitMatches"
                            :key="i"
                            :value="match.value" >{{ match.name }}</option>
                </select>
                <table>
                  <tr>
                  <td class="table-result-body-ipJahr"><div class="hideResult" v-if="selectedInjection> 0">{{Math.round(selectedInjection)}}</div></td>
                </tr>
                </table>
</div>


    "use strict";

    new Vue({
        el: '#app',
        data: {
            selectedPreisProIE: [],
            selectedIeProKg: [],
            selectedGewicht: [],
            selectedInjection: '',
            selectedProduct: null,
            dataJson: [],
            products: [{
                name: "ivi",
                Hint: "45-60 IE/kg alle 5 Tage\n60 IE 1x/Woche\n30-40 IE 2 x/Woche",
                frequency: [1, 2, 8]
            }, {
                name: "ynovi",
                Hint: "40-50 IE/kg\n2x/Woche im Abstand\nvon 3-4 Tagen",
                frequency: [2, 6, 7]
            }, {
                name: "octa",
                Hint: "50 (25-65) IE/kg\nalle 3-5 Tage",
                frequency: [6, 7, 8]
            }, {
                name: "eroct",
                Hint: "50 IE/kg \nalle 4 Tage",
                frequency: [7]
            }, {
                name: "ltry",
                Hint: "20-40 I.E./kg\n2-3x/Woche",
                frequency: [2, 3]
            }, {
                name: "ate",
                Hint: "20-40 I.E./kg\nAlle 2-3 Tage",
                frequency: [5, 6]
            }, {
                name: "Facto A",
                Hint: "20-40 I.E./kg\nAlle 2-3 Tage",
                frequency: [5, 6]
            }, {
                name: "Eight",
                Hint: "40-60 I.E./kg \nJeden 3.Tag oder\n2x/Woche",
                frequency: [2, 3, 5, 6]
            }, {
                name: "iq_Vima",
                Hint: "20-40 I.E./kg\nAlle 2-3 Tage",
                frequency: [5, 6]
            }, {
                name: "Afla",
                Hint: "20-50 I.E./kg\n2-3x/Woche",
                frequency: [2, 3]
            }, {
                name: "Pma",
                Hint: "20-40 I.E./kg\nAlle 2-3 Tage",
                frequency: [5, 6]
            }, {
                name: "others",
                Hint: "Individuell",
                frequency: [1, 2, 3, 4, 5, 6, 7, 8]
            }],
            haufigkeit: [{
                name: "1x / Woche",
                id: 1,
                value: 52.1428571429
            }, {
                name: "2x / Woche",
                value: 104.2857142857143,
                id: 2
            }, {
                name: "3x / Woche",
                value: 156.4285714285714,
                id: 3
            }, {
                name: "alle 1 Tage",
                value: 365,
                id: 4
            }, {
                name: "alle 2 Tage",
                value: 182.5,
                id: 5
            }, {
                name: "alle 3 Tage",
                value: 121.6666666666667,
                id: 6
            }, {
                name: "alle 4 Tage",
                value: 91.25,
                id: 7
            }, {
                name: "alle 5 Tage",
                value: 73,
                id: 8
            }]
        },
        computed: {
            haufigkeitMatches: function haufigkeitMatches() {
                var _this = this;

                if (this.selectedProduct) {
                    return this.haufigkeit.filter(function (x) {
                        return _this.selectedProduct.frequency.includes(x.id);
                    });
                }
            },
        },
        methods:{
            setSelectsToDefault(){
                this.selectedIeProKg = 0;
                this.selectedPreisProIE = 0;
                this.selectedGewicht = 0;
                this.selectedInjection='';
            }
            }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2020-06-08
    • 1970-01-01
    相关资源
    最近更新 更多