Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the values of data-* attributes in vuejs

I have a button which when clicked opens up a modal, and the content to show in the modal is based on the data-attributes passed to the button.

My button,

<button class="btn btn-info" data-toggle="modal"
        data-target="#someModal" :data-id=item.id :data-name=item.name>
        Edit
    </button>

In my modal, I have some buttons and when clicked I should call a vuejs function with a parameter, which is the data-attribute.

My modal button,

<div class="modal-footer">
    <button type="button" class="btn btn-danger"
        @click.prevent="deleteItem()" data-dismiss="modal">
        Delete
    </button>
    <button type="button" class="btn btn-warning" data-dismiss="modal">
        <span class='glyphicon glyphicon-remove'></span> Close
    </button>
</div>

Here I have to pass a parameter to deleteItem(), and that parameter is the data-id which I get from the button above.

Code for Modal

    <div id="deleteModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Delete</h4>
                </div>
                <div class="modal-body">
                    <div class="deleteContent">
                        Are you Sure you want to delete ?
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn actionBtn btn-danger"
                            @click.prevent="deleteItem()"
                            data-dismiss="modal">
                            Delete <span id="footer_action_button"
                                class='glyphicon glyphicon-trash'> </span>
                        </button>
                        <button type="button" class="btn btn-warning"
                            data-dismiss="modal">
                            <span class='glyphicon glyphicon-remove'></span> Close
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
like image 431
Mann Avatar asked Feb 16 '17 09:02

Mann


3 Answers

HTML

<button type="button" @click.prevent="deleteItem()" :data-id="1" data-dismiss="modal">
Delete <span id="footer_action_button" class='glyphicon glyphicon-trash'> </span>
</button>

Vue

methods:{
  deleteItem: function(){
    var id = event.target.getAttribute('data-id');
    console.log(id) // 1
  }
}

Here is the demo https://codepen.io/maab16/pen/jONZpVG

like image 145
Md Abu Ahsan Basir Avatar answered Sep 20 '22 22:09

Md Abu Ahsan Basir


You can pass the "item.id" like this too:

<button type="button" @click="deleteItem(item.id)">Delete</button>
like image 44
Andrey Prokhorov Avatar answered Sep 17 '22 22:09

Andrey Prokhorov


I recommend doing a console.log(this) inside a component function, then calling that function on a button click so you can inspect all the properties of the component.

(See the attached image, below, for example output of the above console.log statement.)

This shows you that amongst others, you have access to the $el property holding the DOM element. Which opens up a whole lot of possibilities, such as being able to add the following code to the mounted lifecycle hook of your component:

mounted() {
  console.log(this);
  this.myAttribute = this.$el.getAttribute('data-attribute-name');
},

And for this example, this.myAttribute would have been defined in (for example) your data property:

// This value gets attributed during an earlier lifecycle hook.
// It will be overridden in the component's mounted() lifecycle hook.
data() {
  return {
    myAttribute: 'defaultvalue'
  }
}

Vue.js (v2) Lifecycle hooks documentation


Example output when executing console.log(this) inside a component:

enter image description here

like image 32
LittleTiger Avatar answered Sep 18 '22 22:09

LittleTiger