简体   繁体   English

Vue.js 在渲染组件上调用函数

[英]Vue.js Calling function on a rendered component

I want to create an interactive scrumboard using Laravel and Vue.js containing multiple columns and within those columns multiple tickets.我想使用 Laravel 和 Vue.js 创建一个交互式 Scrumboard,其中包含多个列,并在这些列中包含多个票证。

These tickets are vue components with some nice edit / delete / (un)assign developer functionality and is used on other pages as well.这些票是 vue 组件,具有一些不错的编辑/删除/(取消)分配开发人员功能,也用于其他页面。

I have multiple columns defined like this:我有多个这样定义的列:

        <div id="scrumboard">
            <div class="scrumboard__column">
                <div class="scrumboard__title">Open</div>
                <div class="scrumboard__tickets_wrapper" data-status="open">
                    @if( $sprint->hasTicketsOfStatus("open") )
                        @foreach( $sprint->getTicketsByStatus("open") as $ticket )
                            <ticket :data="{{ $ticket->getJsonData(true) }}"></ticket>
                        @endforeach
                    @endif
                </div>
            </div>
            <div class="scrumboard__column">
                <div class="scrumboard__title">In progress</div>
                <div class="scrumboard__tickets_wrapper" data-status="progress">
                    @if( $sprint->hasTicketsOfStatus("progress") )
                        @foreach( $sprint->getTicketsByStatus("progress") as $ticket )
                            <ticket :data="{{ $ticket->getJsonData(true) }}"></ticket>
                        @endforeach
                    @endif
                </div>
            </div>
            <div class="scrumboard__column">
                <div class="scrumboard__title">Finished</div>
                <div class="scrumboard__tickets_wrapper" data-status="closed">
                    @if( $sprint->hasTicketsOfStatus("closed") )
                        @foreach( $sprint->getTicketsByStatus("closed") as $ticket )
                            <ticket :data="{{ $ticket->getJsonData(true) }}"></ticket>
                        @endforeach
                    @endif
                </div>
            </div>
        </div>

And as you can see it renders a ticket component for each ticket it finds for each column.正如你所看到的,它为它为每一列找到的每张票呈现一个票证组件。

No i have turned the scrumboard__tickets_wrapper div's into jquery ui sortable lists which allows you to swap the tickets between columns.不,我已将 scrumboard__tickets_wrapper div 转换为 jquery ui 可排序列表,它允许您在列之间交换票证。

<script>
    $(document).ready(function(){

        $(".scrumboard__tickets_wrapper").sortable({
            connectWith: '.scrumboard__tickets_wrapper',
            receive: function(event, ui){
                console.log("Switched columns");
                console.log(event);
                console.log(ui);
                var target = $(event.target);
                target.css("background-color", "#ff0000");
            }
        });
</script>

Everything is working so far, now my question is: how do I dynamically call the "updateStatus()" function on a ticket component once the ticket is dropped into another list?到目前为止一切正常,现在我的问题是:一旦票被放入另一个列表中,我如何在票组件上动态调用“updateStatus()”函数?

As you can see I can get the specific element being dropped and the sortable list it's been dropped into.如您所见,我可以获取被删除的特定元素以及它被放入的可排序列表。 So I know what the new status is by grabbing the data-status property of the wrapper + I know which element was dropped.所以我通过获取包装器的 data-status 属性知道新状态是什么+我知道哪个元素被删除了。

But how do I grab the instance of the ticket component in question and call the updateStatus function to save the new status?但是如何获取有问题的工单组件的实例并调用 updateStatus 函数来保存新状态?

Thanks in advance!提前致谢!

Screenshot of the scrumboard Scrumboard 的屏幕截图

Thanks David for pointing me in the right direction.感谢大卫为我指明了正确的方向。 The solution to my problem was proper component nesting.我的问题的解决方案是正确的组件嵌套。

The solution was to create 3 components with proper child-component inheritence.解决方案是创建 3 个具有适当子组件继承的组件。 And declaring the child-components within the template of it's parent.并在其父组件的模板中声明子组件。

This way I end up only declaring "" and the magic happens :D.这样我最终只声明了 "" 并且魔法发生了:D。

So I have made 3 components: - scrumboard > takes scrumboardColumn as component - scrumboardColumn > takes ticket as component - ticket所以我制作了 3 个组件: - scrumboard > 将 scrumboardColumn 作为组件 - scrumboardColumn > 将票证作为组件 - 票证

The root vue instance also loads the ticket component since the ticket component is also used on the backlog page.根 vue 实例还加载了工单组件,因为工单组件也用于 backlog 页面。

I haven't completely finished the final product but I got the sortable working by calling it from within the ready function of the scrumboardColumn component like David suggested.我还没有完全完成最终产品,但我通过像 David 建议的那样从 scrumboardColumn 组件的就绪函数中调用它来获得可排序的工作。

Hope this helps someone in the future!希望这对未来的人有所帮助!

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

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