简体   繁体   English

在 v-for 循环中使用带有加载器的 Vuetify v-btn

[英]Use Vuetify v-btn with loader inside a v-for loop

I am trying to figure out how to have my dynamically generated buttons inside a for loop each have a separate loader.我想弄清楚如何让我在 for 循环中动态生成的按钮每个都有一个单独的加载程序。 Vuetify has buttons with Loaders. Vuetify 有带有 Loader 的按钮。

The problem I am having is, When these buttons are in a for loop and one is clicked they all show the loading indicator.我遇到的问题是,当这些按钮处于 for 循环中并且单击其中一个时,它们都会显示加载指示器。 I would like only the one that is clicked to show the loading indicator.我只想要单击以显示加载指示器的那个。

HTML: HTML:

<div v-for="(item, i) in items" :key='i'>

<v-btn 
dark 
color="pink"
:loading="loading"
@click="loader = 'loading'"
>
  <v-icon>location_on</v-icon> Lookup
  <span slot="loader">locating...</span>
  <span slot="loader" class="custom-loader">
      <v-icon dark>cached</v-icon>
  </span>
</v-btn>

</div>

SCRIPT脚本

data () {
      return {
        loader: null,
        loading: false
      }
    },

Let's say I have 3 items.假设我有 3 个项目。 The code above will generate three buttons but they all will share the same loading param.上面的代码将生成三个按钮,但它们都将共享相同的加载参数。 How can I have each button use its only loading param?我怎样才能让每个按钮使用它唯一的加载参数? As always any and all help is much appreciated.一如既往,我们非常感谢您的帮助。

You're using the same data property for all buttons, so these buttons share the same loading state which affects the at one time, to make difference try to add a data property called index which represents the current clicked button index :您对所有按钮使用相同的数据属性,因此这些按钮共享相同的loading状态,这会影响一次,为了有所不同,请尝试添加一个名为index的数据属性,它表示当前单击的按钮索引:

data () {
      return {
        index:-1,
        loader: null,
        loading: false
      }
    },

and bind loading prop to the condition loading && i==index and update the current index on click event @click="loader = 'loading';index=i" :并将 loading 道具绑定到条件loading && i==index并在点击事件@click="loader = 'loading';index=i"更新当前索引:

<div v-for="(item, i) in items" :key='i'>

<v-btn 
dark 
color="pink"
:loading="loading && i==index"
@click="loader = 'loading';index=i"
>
  <v-icon>location_on</v-icon> Lookup
  <span slot="loader">locating...</span>
  <span slot="loader" class="custom-loader">
      <v-icon dark>cached</v-icon>
  </span>
</v-btn>

</div>

Its actually a lot easier than you think:它实际上比你想象的要容易得多:

<div v-for="(item, i) in items" :key='i'>
<v-btn 
dark 
color="pink"
:loading="loading[index]"
@click="loading[index] = true"
>
  <v-icon>location_on</v-icon> Lookup
  <span slot="loader">locating...</span>
  <span slot="loader" class="custom-loader">
      <v-icon dark>cached</v-icon>
  </span>
</v-btn>

</div>
data () {
      return {
        loading: {},
      }
    },

In the beginning loading[index] will be undefined, so it will be evaluated as false, once you establish its value in the click event it will be evaluated as true, it worked for me, hope it helps.在开始时loading[index]将是未定义的,所以它会被评估为 false,一旦你在 click 事件中建立它的值,它就会被评估为 true,它对我有用,希望它有帮助。

you can use reactive array to prevent changing the index like this.您可以使用反应数组来防止像这样更改索引。

<div v-for="(item, i) in items" :key='i'>
<v-btn @click.prevent="testload(i)" :loading="loading[i]"></v-btn>
</div>

data () {
  return {
    loading: [],
  }
},
methods: {
    testload: function (index) {
        // reactive array
        this.$set(this.loading, index, true);
        console.log(index)
        console.log(this.loading[index])
        // stop loading after x miliseconds
        setTimeout(() => (this.$set(this.loading, index, false)), 3000)
    },

dont forget to input false according the length of items, this is the example:不要忘记根据项目的长度输入false,这是示例:

getDataAll: function () {
                var i = 0
                for (i = 0; i < items.length; i++) {
                    this.loading.push(false);
                }
             }

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

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