【问题标题】:DataList: How to hide the lookup value?DataList:如何隐藏查找值?
【发布时间】:2020-05-17 03:44:53
【问题描述】:

编辑此问题是因为我发现与 VueJS 无关,而是与 HTML Datalist 相关。我还将这个问题作为Setting hidden datalist option values 的副本关闭。

世界上的任何选择器都有一个显示属性作为标签和一个查找值以将其与对象列表链接。到目前为止还可以。我需要隐藏对我的用户毫无意义的查找 ID。

我的选择列表中的每个选项条目都会显示这些行。第一行是不应该呈现给用户的查找 ID。第二行是预期的显示值(标签)。

我需要一个选项来隐藏查找值! 我什至找不到选择列表的 HTML,我假设它是由浏览器生成的,而不是添加到 DOM 中的。

HLEP !!!

<div id="app">
    <datalist id="options1">  
    <option v-for="option in this.options" v-bind:value="option.id" >{{ option.name }}</option>
    </datalist>

    <datalist id="options2">  
    <option v-for="option in this.options" v-bind:value="option" >{{ option.name }}</option>
    </datalist>

    <datalist id="options3">  
    <option v-for="option in this.options" v-bind:value="option.name" >{{ option.name }}</option>
    </datalist>

    I need to hide the IDs. <br>The display value should be displayed in the box after selection and the lookup value should be sent to the model (selectedId).<br>

    <input list="options1" v-model="selectedId" ><br>
    Selected ID is: {{ selectedId }}<br><br>

    LookupResult: {{ computedLookupById | json }}

    <hr><br><br>
    I was expecting that I can send the entire object to the model. But is actually a string.<br>
    <input list="options2" v-model="selectedOption" ><br>
    Selected ID is: {{ selectedOption | json }}<br><br>

    <hr><br><br>
    If I send the display value to the model, I have to lookup for the object. Will take computation time and will only work when display values are unique:<br>
    <input list="options3" v-model="selectedName" ><br>
    Selected ID is: {{ selectedName | json }}<br><br>

    LookupResult: {{ computedLookupByName | json }}
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>


<script>

new Vue({
  el: "#app",
  data: {
    options: [
      { id: 0, name: "one" }, // So, my display value is not unique. I can't use it for luckup.
      { id: 1, name: "one" },
      { id: 2, name: "two" },
      { id: 3, name: "tree" }
    ],
    selectedOption: null,
    selectedId: null,
    selectedName: null
  },
  computed: {
    computedLookupById() {
        return this.options.find(p => {
        return p.id == this.selectedId;
      });
    },
    computedLookupByName() {
        return this.options.find(p => {
        return p.name == this.selectedName;
      });
    }
  },
  methods: {
    toggle: function(todo){
        todo.done = !todo.done
    }
  }
})

</script>

【问题讨论】:

  • 你能展示你的代码示例吗?
  • 好的,我现在看到你的代码了。你到底想隐藏什么价值?
  • 另外,很高兴看到渲染组件的整个图像以及要隐藏的区域。
  • 我已经添加了一个答案。可以查一下吗?

标签: select options datalist


【解决方案1】:

看看你的计算结果:

computedLookupById() {
    return this.options.find(p => {
    return p.id == this.selectedId;
  });
},

computedLookupById给你返回一个option对象,比如id=1,那么返回值是这样的:

{ id: 1, name: "one" },

在您的模板中,您有这一行:

LookupResult: {{ computedLookupById | json }}

如果你想隐藏id,那么你应该这样做:

LookupResult: {{ computedLookupById.name }}

根据这个https://vuejs.org/v2/guide/migration.html#Built-In-Text-Filters-removed,看起来json过滤器已从vue中删除。

【讨论】:

  • 嗨@webprogrammer,您的分析是正确的,但不能解决我的需求。我不关心显示值,我应另一个用户的要求制作了这个例子。我要强调一个事实,即选择控件应该 1) 返回所选对象(理想情况下),但这是不可能的,因为该值已序列化为字符串,或者 2) 返回 Id 以便我可以自己查找基于对象在它的唯一ID上。第二种情况是可能的,但会带来烦人的副作用。 ID 在选项模板中显示为第一行,没有隐藏它的选项。
  • 关闭了我自己的问题(发现与 VueJS 无关)重复:stackoverflow.com/questions/39086427/…
猜你喜欢
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
相关资源
最近更新 更多