简体   繁体   中英

How to get data attributes in vue.js

I have a button, and a modal is poped up when it is clicked. The data in the modal is passed with data attributes for the button.

My button,

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

It has some data attributes like id and name .

My modal,

<div id="editModal" 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">Edit</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label class="control-label col-sm-2">ID:</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="fid" disabled>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-sm-2">Name:</label>
                            <div class="col-sm-10">
                                <input type="name" class="form-control" id="n">
                            </div>
                        </div>
                    </form>
                    <div class="modal-footer">
                        <button type="button" class="btn actionBtn btn-success"
                            @click.prevent="updateItem()" data-dismiss="modal">
                            Update <span id="footer_action_button"
                                class='glyphicon glyphicon-check'> </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>

Actually, am doing with javascript, and the modal is displaying id and name as I wanted. And i also need to pass those data attributes to the update() function.

How to workaround in vuejs with data attributes.

$(document).on('click', '.edit-modal', function() {
            $('#fid').val($(this).data('id'));
            $('#n').val($(this).data('name'));
        });

Well, I don't know if it's a workaround but what you can do is to create a modal component and then pass data to it through props or better yet by using slots like in modal example I've linked.

This should point you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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