简体   繁体   English

如何动态添加 vue bootstrap 下拉列表项?

[英]How to dynamically add vue bootstrap dropwdown list item?

I'm trying to dynamically add drowdown list items using bootstrap-vue, and I'm having trouble getting the dropdown list items to render to the browser.我正在尝试使用 bootstrap-vue 动态添加下拉列表项,但无法将下拉列表项呈现给浏览器。 Here's what I have so far, any input would be much appreciated.这是我到目前为止所拥有的,任何输入将不胜感激。 I want it to be dynamics so that if the original JSON ever changes the dropwdown list items will change with it without having to go back and change the code.我希望它是动态的,因此如果原始 JSON 发生更改,下拉列表项将随之更改,而无需返回 go 并更改代码。

NOTE: the reportData is a list of objects, which is why I'm setting the first element of the reportData to the newData that i want the dropdown list items to be rendered from.注意:reportData 是一个对象列表,这就是为什么我将 reportData 的第一个元素设置为我希望从中呈现下拉列表项的 newData。

Dropdown List Component:下拉列表组件:

  <section class="drop-down">
    <div>
      <b-dropdown id="dropdown-list" text="Error Reports" class="m-md-2">
       <b-dropdown-item >ITEM</b-dropdown-item>
        <b-dropdown-divider></b-dropdown-divider>
      </b-dropdown>
    </div>

    <button @click="changeReport">Click to change</button>
  </section>
</template>

<script lang="js">

  export default  {
    name: 'drop-down',
    props:['reportData'],
    data () {
      return {
        newData: this.reportData[0]
      }
    },
    methods: {
      changeReport(){
        this.newData = this.reporData[1]
      }
    },
    watch: {
      newData : function(val, OldVal){
        var dropdownList = document.getElementById("dropdown-list")
          for (var key in val){
            console.log(key)
            var dropdownItem = document.createElement('b-dropdown-item')
            dropdownItem.value = key
            dropdownList.add(drowdownItem, 0)
          }
      }
    }
}


</script>

<style>

</style>

Try to avoid directly manipulating the DOM when you can while using Vue.在使用 Vue 时尽量避免直接操作 DOM。 In this case, your best option would be to set the currently active data to whatever list you want to display.在这种情况下,您最好的选择是将当前活动数据设置为您想要显示的任何列表。 Then, in the template, loop though each of those active data items.然后,在模板中,遍历每个活动数据项。

 new Vue({ el: "#app", data: { reportData: [{ text: 'Item 1 Text', value: 'Item 1 Value' }, { text: 'Item 2 Text', value: 'Item 2 Value' }, { text: 'Item 3 Text', value: 'Item 3 Value' }], } })
 <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css" /> <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" /> <script src="https://unpkg.com/vue"></script> <script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script> <div id="app"> <section class="drop-down"> <b-dropdown text="Error Reports" class="m-md-2"> <b-dropdown-item v-for="(item, key) in reportData":key="key"> {{ item.text }}: {{ item.value }} </b-dropdown-item> </b-dropdown> </section> </div>

Feel free to let me know if you have any questions.如果您有任何问题,请随时告诉我。


Edit:编辑:

The following example may help clarify what you need to do if you have nested arrays of objects that you know the structure of ahead of time.以下示例可能有助于阐明如果您嵌套了您提前知道其结构的对象的 arrays,您需要做什么。

However, if you don't know the depth of your tree, you may have to consider using recursive components .但是,如果您不知道树的深度,则可能不得不考虑使用递归组件

 new Vue({ el: "#app", data: { // This is your input data dataStore: [{ name: 'Item 1', value: [{ text: 'SubItem 1 Text', value: 'SubItem 1 Value' }, { text: 'SubItem 2 Text', value: 'SubItem 2 Value' }, { text: 'SubItem 3 Text', value: 'SubItem 3 Value' }] }, { name: 'Item 2', value: [{ text: 'SubItem 1 Text', value: 'SubItem 1 Value' }, { text: 'SubItem 2 Text', value: 'SubItem 2 Value' }, { text: 'SubItem 3 Text', value: 'SubItem 3 Value' }, { text: 'SubItem 4 Text', value: 'SubItem 4 Value' }, { text: 'SubItem 5 Text', value: 'SubItem 5 Value' }, ] }], // This is the data we will display activeData: null }, created() { // The value that will be selected upon creation this.activeData = this.dataStore[0] }, methods: { changeData() { this.activeData = this.dataStore[1] } } })
 <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css" /> <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" /> <script src="https://unpkg.com/vue"></script> <script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script> <div id="app"> <section v-if="activeData" class="drop-down"> <h2>{{ activeData.name }}</h2> <b-dropdown text="Error Reports" class="m-md-2"> <b-dropdown-item v-for="(item, key) in activeData.value":key="key"> {{ item.text }}: {{ item.value }} </b-dropdown-item> </b-dropdown> </section> <button @click="changeData">Change Data Item</button> </div>

try this尝试这个

      <b-dropdown id="dropdown-list" text="Error Reports" class="m-md-2">
       <b-dropdown-item v-for="item in listItems">@{{ item }}</b-dropdown-item>
        <b-dropdown-divider></b-dropdown-divider>
      </b-dropdown>

      <button @changeReport>Click To Change</button>

In your JS....在你的 JS....

    data() {
      items: []
    },

    computed: {
      listItems() {
        return this.items;
      }
    },
    methods: {
      changeReport(){
        this.items = this.reporData[1]
      }

Something like that.类似的东西。 I don't know exactly what your data looks like, but if you fill this.list with your new data, the computed property will notice that change and re-render your dropdown.我不确切知道您的数据是什么样的,但是如果您用新数据填充this.list ,计算属性会注意到更改并重新呈现您的下拉列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM