Vue.js - How to have isolated scope for a spinner inside of `v-for`












1















HTML



<div v-for="account in accounts">
...
<button v-if="!isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
<i class="fa fa-unlink"></i>
Unlink
</button>

<button v-if="!!isUnlinking" class="btn btn-warning btn-sm" :disabled="!!isUnlinking">
<i class="fa fa-spin fa-refresh"></i>
Unlinking...
</button>
...
</div>


PROBLEM



When I click on Unlink button, the button within all repeated/iterated div elements is getting the spinner. Whereas I would like only the button that is clicked to have the spinner icon.










share|improve this question



























    1















    HTML



    <div v-for="account in accounts">
    ...
    <button v-if="!isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
    <i class="fa fa-unlink"></i>
    Unlink
    </button>

    <button v-if="!!isUnlinking" class="btn btn-warning btn-sm" :disabled="!!isUnlinking">
    <i class="fa fa-spin fa-refresh"></i>
    Unlinking...
    </button>
    ...
    </div>


    PROBLEM



    When I click on Unlink button, the button within all repeated/iterated div elements is getting the spinner. Whereas I would like only the button that is clicked to have the spinner icon.










    share|improve this question

























      1












      1








      1


      1






      HTML



      <div v-for="account in accounts">
      ...
      <button v-if="!isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
      <i class="fa fa-unlink"></i>
      Unlink
      </button>

      <button v-if="!!isUnlinking" class="btn btn-warning btn-sm" :disabled="!!isUnlinking">
      <i class="fa fa-spin fa-refresh"></i>
      Unlinking...
      </button>
      ...
      </div>


      PROBLEM



      When I click on Unlink button, the button within all repeated/iterated div elements is getting the spinner. Whereas I would like only the button that is clicked to have the spinner icon.










      share|improve this question














      HTML



      <div v-for="account in accounts">
      ...
      <button v-if="!isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
      <i class="fa fa-unlink"></i>
      Unlink
      </button>

      <button v-if="!!isUnlinking" class="btn btn-warning btn-sm" :disabled="!!isUnlinking">
      <i class="fa fa-spin fa-refresh"></i>
      Unlinking...
      </button>
      ...
      </div>


      PROBLEM



      When I click on Unlink button, the button within all repeated/iterated div elements is getting the spinner. Whereas I would like only the button that is clicked to have the spinner icon.







      vue.js vuejs2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 6 '16 at 9:14









      AkashAkash

      6,38013940




      6,38013940
























          6 Answers
          6






          active

          oldest

          votes


















          2














          In your V-FOR, each row can be assigned a key.



          Eg.



          <div v-for=" (b,key) in buttons">

          <button>
          {{b}}
          <i class="fa fa-spinner fa-spin" @click="clickButton(key)" v-if="buttonsLoading[key]">
          </button>

          </div>


          And in your VUE data section:



          data: {
          buttons:{
          button1:'Click me',
          button2:'NO click me,
          button3:'NOOO Click me',
          },
          buttonsLoading:,
          },
          methods: {
          clickButton:function(key)
          {
          Vue.set(this.buttonsLoading , key , 1);
          }
          }


          You're obviously going to know when the button needs to stop, if you're doing an axios or ajax request so when you get a success response, add Vue.set(this.buttonsLoading , 'button' + key , 0) so it stops spinning.



          Eg. in my V-FOR setup, I have a button in each row.
          When clicked, each individual button must act separately and show it's own spinner, not all of them (which is the error you are having).



          To do this, you must create an array and then you can set the key to the array when clicked.



          Did a video for you if you want, https://www.youtube.com/watch?v=Zm00rS0gX24, please excuse if it is not explained exactly, I am not the best at explaining these things :) Cheers






          share|improve this answer

































            0














            In this case every button should have id parameter. When you are 'unlinking' you have to check with v-if if id of button is the same as context element and display spinner only in this case.






            share|improve this answer
























            • won't that result in multiple ids in HTML which is not a good practice?

              – Akash
              Dec 6 '16 at 9:35



















            0














            The problem seems to me is you are using single variable isUnlinking to make all the buttons visible, so if the variable changes for one of the button, it is applied on all the buttons.



            You have to have a array of isUnlinking and change only for that button, like following:



            <div v-for="(account, index) in accounts">
            ...
            <button v-if="!isUnlinking[index]" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
            <i class="fa fa-unlink"></i>
            Unlink
            </button>

            <button v-else class="btn btn-warning btn-sm" :disabled="isUnlinking[index]">
            <i class="fa fa-spin fa-refresh"></i>
            Unlinking...
            </button>


            Also notice the use of v-else, instead of v-if="!!isUnlinking".



            ...






            share|improve this answer
























            • how will the unlinking Array know how many items are there in the v-for list? And thanks for v-else...it's cool

              – Akash
              Dec 6 '16 at 12:44






            • 1





              @Akash You will have to initialise the array using accounts array and set it false initially.

              – Saurabh
              Dec 6 '16 at 12:51











            • it feels little cumbersome to simultaneously initialize an array with false booleans. I am considering writing a component for this button which will have its own scope. Moreover, when an account is unlinked i will have to also delete that array row. it could get complicated

              – Akash
              Dec 7 '16 at 10:40





















            0














            The best way is to add this property on the account object:



            <div v-for="account in accounts">
            ...
            <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
            <i class="fa fa-unlink"></i>
            Unlink
            </button>

            <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!isUnlinking">
            <i class="fa fa-spin fa-refresh"></i>
            Unlinking...
            </button>
            ...
            </div>


            Now it's bound to the specific object.






            share|improve this answer
























            • that's a good idea, however, mutating the account object is not an option.

              – Akash
              Dec 8 '16 at 4:29



















            0














            One way I have solved similar problem was that I have passed $event.target.children[0] as a property on a click event function, than removed the class from element and appended a new class for a spinner to an element. I hope this will help.



            <div v-for="account in accounts">
            <button class="btn btn-warning btn-sm"
            @click="unlinkAccount($event.target.children[0])">
            <i class="fa fa-unlink"></i>
            Unlink
            </button>
            </div>
            <script>
            export default {
            methods: {
            unlinkAccount() {
            event.className = event.className.replace(/bfa fa-unlinkb/g, "fa fa-spin fa-refresh");
            }
            }
            }
            </script>





            share|improve this answer































              0














              This is the solution I would like to propose to my question:



              UPDATE NOV-2018:



              I would suggest creating a new vue-component and iterate over it. The account object would be passed to the child-component via props:



              <div>
              <h3> Accounts </h3>
              <AccountButton v-for="account in accounts" :account="account" />
              </div>


              Good Luck...





              PREVIOUS SUGGESTION



              <div v-for="account in accounts.map(function(item){ return {item: item, isUnlinking: false}})">
              ...
              <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(account)">
              <i class="fa fa-unlink"></i>
              Unlink
              </button>

              <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!account.isUnlinking">
              <i class="fa fa-spin fa-refresh"></i>
              Unlinking...
              </button>
              ...
              </div>


              <script>
              // somewhere inside Vue.component
              ...
              function unlinkAccount(account) {
              account.isUnlinking = true;
              // do the unlinking with account.item.provider.providerId
              }
              ...
              </script>


              Here, I've bootstrapped a new array from the existing accounts array with the map function (this mapping could be moved into the Vue component syntax in JS instead of keeping it in HTML). Now each account item has its own property isUnlinking which can be set as required. I've set the initial value to false as unlinking will start only after clicking the button.



              Thank you @Bazinga, @Saurabh, @Bernard Doci and @M U for your participation



              Good Luck...






              share|improve this answer

























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40991645%2fvue-js-how-to-have-isolated-scope-for-a-spinner-inside-of-v-for%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                6 Answers
                6






                active

                oldest

                votes








                6 Answers
                6






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                In your V-FOR, each row can be assigned a key.



                Eg.



                <div v-for=" (b,key) in buttons">

                <button>
                {{b}}
                <i class="fa fa-spinner fa-spin" @click="clickButton(key)" v-if="buttonsLoading[key]">
                </button>

                </div>


                And in your VUE data section:



                data: {
                buttons:{
                button1:'Click me',
                button2:'NO click me,
                button3:'NOOO Click me',
                },
                buttonsLoading:,
                },
                methods: {
                clickButton:function(key)
                {
                Vue.set(this.buttonsLoading , key , 1);
                }
                }


                You're obviously going to know when the button needs to stop, if you're doing an axios or ajax request so when you get a success response, add Vue.set(this.buttonsLoading , 'button' + key , 0) so it stops spinning.



                Eg. in my V-FOR setup, I have a button in each row.
                When clicked, each individual button must act separately and show it's own spinner, not all of them (which is the error you are having).



                To do this, you must create an array and then you can set the key to the array when clicked.



                Did a video for you if you want, https://www.youtube.com/watch?v=Zm00rS0gX24, please excuse if it is not explained exactly, I am not the best at explaining these things :) Cheers






                share|improve this answer






























                  2














                  In your V-FOR, each row can be assigned a key.



                  Eg.



                  <div v-for=" (b,key) in buttons">

                  <button>
                  {{b}}
                  <i class="fa fa-spinner fa-spin" @click="clickButton(key)" v-if="buttonsLoading[key]">
                  </button>

                  </div>


                  And in your VUE data section:



                  data: {
                  buttons:{
                  button1:'Click me',
                  button2:'NO click me,
                  button3:'NOOO Click me',
                  },
                  buttonsLoading:,
                  },
                  methods: {
                  clickButton:function(key)
                  {
                  Vue.set(this.buttonsLoading , key , 1);
                  }
                  }


                  You're obviously going to know when the button needs to stop, if you're doing an axios or ajax request so when you get a success response, add Vue.set(this.buttonsLoading , 'button' + key , 0) so it stops spinning.



                  Eg. in my V-FOR setup, I have a button in each row.
                  When clicked, each individual button must act separately and show it's own spinner, not all of them (which is the error you are having).



                  To do this, you must create an array and then you can set the key to the array when clicked.



                  Did a video for you if you want, https://www.youtube.com/watch?v=Zm00rS0gX24, please excuse if it is not explained exactly, I am not the best at explaining these things :) Cheers






                  share|improve this answer




























                    2












                    2








                    2







                    In your V-FOR, each row can be assigned a key.



                    Eg.



                    <div v-for=" (b,key) in buttons">

                    <button>
                    {{b}}
                    <i class="fa fa-spinner fa-spin" @click="clickButton(key)" v-if="buttonsLoading[key]">
                    </button>

                    </div>


                    And in your VUE data section:



                    data: {
                    buttons:{
                    button1:'Click me',
                    button2:'NO click me,
                    button3:'NOOO Click me',
                    },
                    buttonsLoading:,
                    },
                    methods: {
                    clickButton:function(key)
                    {
                    Vue.set(this.buttonsLoading , key , 1);
                    }
                    }


                    You're obviously going to know when the button needs to stop, if you're doing an axios or ajax request so when you get a success response, add Vue.set(this.buttonsLoading , 'button' + key , 0) so it stops spinning.



                    Eg. in my V-FOR setup, I have a button in each row.
                    When clicked, each individual button must act separately and show it's own spinner, not all of them (which is the error you are having).



                    To do this, you must create an array and then you can set the key to the array when clicked.



                    Did a video for you if you want, https://www.youtube.com/watch?v=Zm00rS0gX24, please excuse if it is not explained exactly, I am not the best at explaining these things :) Cheers






                    share|improve this answer















                    In your V-FOR, each row can be assigned a key.



                    Eg.



                    <div v-for=" (b,key) in buttons">

                    <button>
                    {{b}}
                    <i class="fa fa-spinner fa-spin" @click="clickButton(key)" v-if="buttonsLoading[key]">
                    </button>

                    </div>


                    And in your VUE data section:



                    data: {
                    buttons:{
                    button1:'Click me',
                    button2:'NO click me,
                    button3:'NOOO Click me',
                    },
                    buttonsLoading:,
                    },
                    methods: {
                    clickButton:function(key)
                    {
                    Vue.set(this.buttonsLoading , key , 1);
                    }
                    }


                    You're obviously going to know when the button needs to stop, if you're doing an axios or ajax request so when you get a success response, add Vue.set(this.buttonsLoading , 'button' + key , 0) so it stops spinning.



                    Eg. in my V-FOR setup, I have a button in each row.
                    When clicked, each individual button must act separately and show it's own spinner, not all of them (which is the error you are having).



                    To do this, you must create an array and then you can set the key to the array when clicked.



                    Did a video for you if you want, https://www.youtube.com/watch?v=Zm00rS0gX24, please excuse if it is not explained exactly, I am not the best at explaining these things :) Cheers







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jul 11 '18 at 2:44

























                    answered Jul 11 '18 at 1:54









                    sfr8assfr8as

                    213




                    213

























                        0














                        In this case every button should have id parameter. When you are 'unlinking' you have to check with v-if if id of button is the same as context element and display spinner only in this case.






                        share|improve this answer
























                        • won't that result in multiple ids in HTML which is not a good practice?

                          – Akash
                          Dec 6 '16 at 9:35
















                        0














                        In this case every button should have id parameter. When you are 'unlinking' you have to check with v-if if id of button is the same as context element and display spinner only in this case.






                        share|improve this answer
























                        • won't that result in multiple ids in HTML which is not a good practice?

                          – Akash
                          Dec 6 '16 at 9:35














                        0












                        0








                        0







                        In this case every button should have id parameter. When you are 'unlinking' you have to check with v-if if id of button is the same as context element and display spinner only in this case.






                        share|improve this answer













                        In this case every button should have id parameter. When you are 'unlinking' you have to check with v-if if id of button is the same as context element and display spinner only in this case.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Dec 6 '16 at 9:33









                        M UM U

                        4,02232645




                        4,02232645













                        • won't that result in multiple ids in HTML which is not a good practice?

                          – Akash
                          Dec 6 '16 at 9:35



















                        • won't that result in multiple ids in HTML which is not a good practice?

                          – Akash
                          Dec 6 '16 at 9:35

















                        won't that result in multiple ids in HTML which is not a good practice?

                        – Akash
                        Dec 6 '16 at 9:35





                        won't that result in multiple ids in HTML which is not a good practice?

                        – Akash
                        Dec 6 '16 at 9:35











                        0














                        The problem seems to me is you are using single variable isUnlinking to make all the buttons visible, so if the variable changes for one of the button, it is applied on all the buttons.



                        You have to have a array of isUnlinking and change only for that button, like following:



                        <div v-for="(account, index) in accounts">
                        ...
                        <button v-if="!isUnlinking[index]" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
                        <i class="fa fa-unlink"></i>
                        Unlink
                        </button>

                        <button v-else class="btn btn-warning btn-sm" :disabled="isUnlinking[index]">
                        <i class="fa fa-spin fa-refresh"></i>
                        Unlinking...
                        </button>


                        Also notice the use of v-else, instead of v-if="!!isUnlinking".



                        ...






                        share|improve this answer
























                        • how will the unlinking Array know how many items are there in the v-for list? And thanks for v-else...it's cool

                          – Akash
                          Dec 6 '16 at 12:44






                        • 1





                          @Akash You will have to initialise the array using accounts array and set it false initially.

                          – Saurabh
                          Dec 6 '16 at 12:51











                        • it feels little cumbersome to simultaneously initialize an array with false booleans. I am considering writing a component for this button which will have its own scope. Moreover, when an account is unlinked i will have to also delete that array row. it could get complicated

                          – Akash
                          Dec 7 '16 at 10:40


















                        0














                        The problem seems to me is you are using single variable isUnlinking to make all the buttons visible, so if the variable changes for one of the button, it is applied on all the buttons.



                        You have to have a array of isUnlinking and change only for that button, like following:



                        <div v-for="(account, index) in accounts">
                        ...
                        <button v-if="!isUnlinking[index]" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
                        <i class="fa fa-unlink"></i>
                        Unlink
                        </button>

                        <button v-else class="btn btn-warning btn-sm" :disabled="isUnlinking[index]">
                        <i class="fa fa-spin fa-refresh"></i>
                        Unlinking...
                        </button>


                        Also notice the use of v-else, instead of v-if="!!isUnlinking".



                        ...






                        share|improve this answer
























                        • how will the unlinking Array know how many items are there in the v-for list? And thanks for v-else...it's cool

                          – Akash
                          Dec 6 '16 at 12:44






                        • 1





                          @Akash You will have to initialise the array using accounts array and set it false initially.

                          – Saurabh
                          Dec 6 '16 at 12:51











                        • it feels little cumbersome to simultaneously initialize an array with false booleans. I am considering writing a component for this button which will have its own scope. Moreover, when an account is unlinked i will have to also delete that array row. it could get complicated

                          – Akash
                          Dec 7 '16 at 10:40
















                        0












                        0








                        0







                        The problem seems to me is you are using single variable isUnlinking to make all the buttons visible, so if the variable changes for one of the button, it is applied on all the buttons.



                        You have to have a array of isUnlinking and change only for that button, like following:



                        <div v-for="(account, index) in accounts">
                        ...
                        <button v-if="!isUnlinking[index]" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
                        <i class="fa fa-unlink"></i>
                        Unlink
                        </button>

                        <button v-else class="btn btn-warning btn-sm" :disabled="isUnlinking[index]">
                        <i class="fa fa-spin fa-refresh"></i>
                        Unlinking...
                        </button>


                        Also notice the use of v-else, instead of v-if="!!isUnlinking".



                        ...






                        share|improve this answer













                        The problem seems to me is you are using single variable isUnlinking to make all the buttons visible, so if the variable changes for one of the button, it is applied on all the buttons.



                        You have to have a array of isUnlinking and change only for that button, like following:



                        <div v-for="(account, index) in accounts">
                        ...
                        <button v-if="!isUnlinking[index]" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
                        <i class="fa fa-unlink"></i>
                        Unlink
                        </button>

                        <button v-else class="btn btn-warning btn-sm" :disabled="isUnlinking[index]">
                        <i class="fa fa-spin fa-refresh"></i>
                        Unlinking...
                        </button>


                        Also notice the use of v-else, instead of v-if="!!isUnlinking".



                        ...







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Dec 6 '16 at 9:36









                        SaurabhSaurabh

                        29.5k1799166




                        29.5k1799166













                        • how will the unlinking Array know how many items are there in the v-for list? And thanks for v-else...it's cool

                          – Akash
                          Dec 6 '16 at 12:44






                        • 1





                          @Akash You will have to initialise the array using accounts array and set it false initially.

                          – Saurabh
                          Dec 6 '16 at 12:51











                        • it feels little cumbersome to simultaneously initialize an array with false booleans. I am considering writing a component for this button which will have its own scope. Moreover, when an account is unlinked i will have to also delete that array row. it could get complicated

                          – Akash
                          Dec 7 '16 at 10:40





















                        • how will the unlinking Array know how many items are there in the v-for list? And thanks for v-else...it's cool

                          – Akash
                          Dec 6 '16 at 12:44






                        • 1





                          @Akash You will have to initialise the array using accounts array and set it false initially.

                          – Saurabh
                          Dec 6 '16 at 12:51











                        • it feels little cumbersome to simultaneously initialize an array with false booleans. I am considering writing a component for this button which will have its own scope. Moreover, when an account is unlinked i will have to also delete that array row. it could get complicated

                          – Akash
                          Dec 7 '16 at 10:40



















                        how will the unlinking Array know how many items are there in the v-for list? And thanks for v-else...it's cool

                        – Akash
                        Dec 6 '16 at 12:44





                        how will the unlinking Array know how many items are there in the v-for list? And thanks for v-else...it's cool

                        – Akash
                        Dec 6 '16 at 12:44




                        1




                        1





                        @Akash You will have to initialise the array using accounts array and set it false initially.

                        – Saurabh
                        Dec 6 '16 at 12:51





                        @Akash You will have to initialise the array using accounts array and set it false initially.

                        – Saurabh
                        Dec 6 '16 at 12:51













                        it feels little cumbersome to simultaneously initialize an array with false booleans. I am considering writing a component for this button which will have its own scope. Moreover, when an account is unlinked i will have to also delete that array row. it could get complicated

                        – Akash
                        Dec 7 '16 at 10:40







                        it feels little cumbersome to simultaneously initialize an array with false booleans. I am considering writing a component for this button which will have its own scope. Moreover, when an account is unlinked i will have to also delete that array row. it could get complicated

                        – Akash
                        Dec 7 '16 at 10:40













                        0














                        The best way is to add this property on the account object:



                        <div v-for="account in accounts">
                        ...
                        <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
                        <i class="fa fa-unlink"></i>
                        Unlink
                        </button>

                        <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!isUnlinking">
                        <i class="fa fa-spin fa-refresh"></i>
                        Unlinking...
                        </button>
                        ...
                        </div>


                        Now it's bound to the specific object.






                        share|improve this answer
























                        • that's a good idea, however, mutating the account object is not an option.

                          – Akash
                          Dec 8 '16 at 4:29
















                        0














                        The best way is to add this property on the account object:



                        <div v-for="account in accounts">
                        ...
                        <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
                        <i class="fa fa-unlink"></i>
                        Unlink
                        </button>

                        <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!isUnlinking">
                        <i class="fa fa-spin fa-refresh"></i>
                        Unlinking...
                        </button>
                        ...
                        </div>


                        Now it's bound to the specific object.






                        share|improve this answer
























                        • that's a good idea, however, mutating the account object is not an option.

                          – Akash
                          Dec 8 '16 at 4:29














                        0












                        0








                        0







                        The best way is to add this property on the account object:



                        <div v-for="account in accounts">
                        ...
                        <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
                        <i class="fa fa-unlink"></i>
                        Unlink
                        </button>

                        <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!isUnlinking">
                        <i class="fa fa-spin fa-refresh"></i>
                        Unlinking...
                        </button>
                        ...
                        </div>


                        Now it's bound to the specific object.






                        share|improve this answer













                        The best way is to add this property on the account object:



                        <div v-for="account in accounts">
                        ...
                        <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(provider.providerId)">
                        <i class="fa fa-unlink"></i>
                        Unlink
                        </button>

                        <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!isUnlinking">
                        <i class="fa fa-spin fa-refresh"></i>
                        Unlinking...
                        </button>
                        ...
                        </div>


                        Now it's bound to the specific object.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Dec 7 '16 at 10:44









                        BazingaBazinga

                        6,21832349




                        6,21832349













                        • that's a good idea, however, mutating the account object is not an option.

                          – Akash
                          Dec 8 '16 at 4:29



















                        • that's a good idea, however, mutating the account object is not an option.

                          – Akash
                          Dec 8 '16 at 4:29

















                        that's a good idea, however, mutating the account object is not an option.

                        – Akash
                        Dec 8 '16 at 4:29





                        that's a good idea, however, mutating the account object is not an option.

                        – Akash
                        Dec 8 '16 at 4:29











                        0














                        One way I have solved similar problem was that I have passed $event.target.children[0] as a property on a click event function, than removed the class from element and appended a new class for a spinner to an element. I hope this will help.



                        <div v-for="account in accounts">
                        <button class="btn btn-warning btn-sm"
                        @click="unlinkAccount($event.target.children[0])">
                        <i class="fa fa-unlink"></i>
                        Unlink
                        </button>
                        </div>
                        <script>
                        export default {
                        methods: {
                        unlinkAccount() {
                        event.className = event.className.replace(/bfa fa-unlinkb/g, "fa fa-spin fa-refresh");
                        }
                        }
                        }
                        </script>





                        share|improve this answer




























                          0














                          One way I have solved similar problem was that I have passed $event.target.children[0] as a property on a click event function, than removed the class from element and appended a new class for a spinner to an element. I hope this will help.



                          <div v-for="account in accounts">
                          <button class="btn btn-warning btn-sm"
                          @click="unlinkAccount($event.target.children[0])">
                          <i class="fa fa-unlink"></i>
                          Unlink
                          </button>
                          </div>
                          <script>
                          export default {
                          methods: {
                          unlinkAccount() {
                          event.className = event.className.replace(/bfa fa-unlinkb/g, "fa fa-spin fa-refresh");
                          }
                          }
                          }
                          </script>





                          share|improve this answer


























                            0












                            0








                            0







                            One way I have solved similar problem was that I have passed $event.target.children[0] as a property on a click event function, than removed the class from element and appended a new class for a spinner to an element. I hope this will help.



                            <div v-for="account in accounts">
                            <button class="btn btn-warning btn-sm"
                            @click="unlinkAccount($event.target.children[0])">
                            <i class="fa fa-unlink"></i>
                            Unlink
                            </button>
                            </div>
                            <script>
                            export default {
                            methods: {
                            unlinkAccount() {
                            event.className = event.className.replace(/bfa fa-unlinkb/g, "fa fa-spin fa-refresh");
                            }
                            }
                            }
                            </script>





                            share|improve this answer













                            One way I have solved similar problem was that I have passed $event.target.children[0] as a property on a click event function, than removed the class from element and appended a new class for a spinner to an element. I hope this will help.



                            <div v-for="account in accounts">
                            <button class="btn btn-warning btn-sm"
                            @click="unlinkAccount($event.target.children[0])">
                            <i class="fa fa-unlink"></i>
                            Unlink
                            </button>
                            </div>
                            <script>
                            export default {
                            methods: {
                            unlinkAccount() {
                            event.className = event.className.replace(/bfa fa-unlinkb/g, "fa fa-spin fa-refresh");
                            }
                            }
                            }
                            </script>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jun 4 '18 at 12:57









                            Bernard DociBernard Doci

                            157112




                            157112























                                0














                                This is the solution I would like to propose to my question:



                                UPDATE NOV-2018:



                                I would suggest creating a new vue-component and iterate over it. The account object would be passed to the child-component via props:



                                <div>
                                <h3> Accounts </h3>
                                <AccountButton v-for="account in accounts" :account="account" />
                                </div>


                                Good Luck...





                                PREVIOUS SUGGESTION



                                <div v-for="account in accounts.map(function(item){ return {item: item, isUnlinking: false}})">
                                ...
                                <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(account)">
                                <i class="fa fa-unlink"></i>
                                Unlink
                                </button>

                                <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!account.isUnlinking">
                                <i class="fa fa-spin fa-refresh"></i>
                                Unlinking...
                                </button>
                                ...
                                </div>


                                <script>
                                // somewhere inside Vue.component
                                ...
                                function unlinkAccount(account) {
                                account.isUnlinking = true;
                                // do the unlinking with account.item.provider.providerId
                                }
                                ...
                                </script>


                                Here, I've bootstrapped a new array from the existing accounts array with the map function (this mapping could be moved into the Vue component syntax in JS instead of keeping it in HTML). Now each account item has its own property isUnlinking which can be set as required. I've set the initial value to false as unlinking will start only after clicking the button.



                                Thank you @Bazinga, @Saurabh, @Bernard Doci and @M U for your participation



                                Good Luck...






                                share|improve this answer






























                                  0














                                  This is the solution I would like to propose to my question:



                                  UPDATE NOV-2018:



                                  I would suggest creating a new vue-component and iterate over it. The account object would be passed to the child-component via props:



                                  <div>
                                  <h3> Accounts </h3>
                                  <AccountButton v-for="account in accounts" :account="account" />
                                  </div>


                                  Good Luck...





                                  PREVIOUS SUGGESTION



                                  <div v-for="account in accounts.map(function(item){ return {item: item, isUnlinking: false}})">
                                  ...
                                  <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(account)">
                                  <i class="fa fa-unlink"></i>
                                  Unlink
                                  </button>

                                  <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!account.isUnlinking">
                                  <i class="fa fa-spin fa-refresh"></i>
                                  Unlinking...
                                  </button>
                                  ...
                                  </div>


                                  <script>
                                  // somewhere inside Vue.component
                                  ...
                                  function unlinkAccount(account) {
                                  account.isUnlinking = true;
                                  // do the unlinking with account.item.provider.providerId
                                  }
                                  ...
                                  </script>


                                  Here, I've bootstrapped a new array from the existing accounts array with the map function (this mapping could be moved into the Vue component syntax in JS instead of keeping it in HTML). Now each account item has its own property isUnlinking which can be set as required. I've set the initial value to false as unlinking will start only after clicking the button.



                                  Thank you @Bazinga, @Saurabh, @Bernard Doci and @M U for your participation



                                  Good Luck...






                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    This is the solution I would like to propose to my question:



                                    UPDATE NOV-2018:



                                    I would suggest creating a new vue-component and iterate over it. The account object would be passed to the child-component via props:



                                    <div>
                                    <h3> Accounts </h3>
                                    <AccountButton v-for="account in accounts" :account="account" />
                                    </div>


                                    Good Luck...





                                    PREVIOUS SUGGESTION



                                    <div v-for="account in accounts.map(function(item){ return {item: item, isUnlinking: false}})">
                                    ...
                                    <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(account)">
                                    <i class="fa fa-unlink"></i>
                                    Unlink
                                    </button>

                                    <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!account.isUnlinking">
                                    <i class="fa fa-spin fa-refresh"></i>
                                    Unlinking...
                                    </button>
                                    ...
                                    </div>


                                    <script>
                                    // somewhere inside Vue.component
                                    ...
                                    function unlinkAccount(account) {
                                    account.isUnlinking = true;
                                    // do the unlinking with account.item.provider.providerId
                                    }
                                    ...
                                    </script>


                                    Here, I've bootstrapped a new array from the existing accounts array with the map function (this mapping could be moved into the Vue component syntax in JS instead of keeping it in HTML). Now each account item has its own property isUnlinking which can be set as required. I've set the initial value to false as unlinking will start only after clicking the button.



                                    Thank you @Bazinga, @Saurabh, @Bernard Doci and @M U for your participation



                                    Good Luck...






                                    share|improve this answer















                                    This is the solution I would like to propose to my question:



                                    UPDATE NOV-2018:



                                    I would suggest creating a new vue-component and iterate over it. The account object would be passed to the child-component via props:



                                    <div>
                                    <h3> Accounts </h3>
                                    <AccountButton v-for="account in accounts" :account="account" />
                                    </div>


                                    Good Luck...





                                    PREVIOUS SUGGESTION



                                    <div v-for="account in accounts.map(function(item){ return {item: item, isUnlinking: false}})">
                                    ...
                                    <button v-if="!account.isUnlinking" class="btn btn-warning btn-sm" @click="unlinkAccount(account)">
                                    <i class="fa fa-unlink"></i>
                                    Unlink
                                    </button>

                                    <button v-if="!!account.isUnlinking" class="btn btn-warning btn-sm" :disabled="!!account.isUnlinking">
                                    <i class="fa fa-spin fa-refresh"></i>
                                    Unlinking...
                                    </button>
                                    ...
                                    </div>


                                    <script>
                                    // somewhere inside Vue.component
                                    ...
                                    function unlinkAccount(account) {
                                    account.isUnlinking = true;
                                    // do the unlinking with account.item.provider.providerId
                                    }
                                    ...
                                    </script>


                                    Here, I've bootstrapped a new array from the existing accounts array with the map function (this mapping could be moved into the Vue component syntax in JS instead of keeping it in HTML). Now each account item has its own property isUnlinking which can be set as required. I've set the initial value to false as unlinking will start only after clicking the button.



                                    Thank you @Bazinga, @Saurabh, @Bernard Doci and @M U for your participation



                                    Good Luck...







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 14 '18 at 5:04

























                                    answered Jun 5 '18 at 2:04









                                    AkashAkash

                                    6,38013940




                                    6,38013940






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40991645%2fvue-js-how-to-have-isolated-scope-for-a-spinner-inside-of-v-for%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Florida Star v. B. J. F.

                                        Error while running script in elastic search , gateway timeout

                                        Adding quotations to stringified JSON object values