【问题标题】:Linking a row in a table to a url将表中的行链接到 url
【发布时间】:2018-05-02 21:14:00
【问题描述】:

我正在开发一个实现 vue-tables-2 的 vue 组件。我有一个工作示例here。我想要做的是将编辑 href url 链接到整行而不是仅编辑单元格。使用 vanilla javascript,我尝试使用 mounted 钩子 (this.$el) 访问虚拟 dom 并将行包装在 href 中并隐藏编辑列,尽管感觉有点像 hack。关于如何实现这一点的任何建议?

  <template>
    <div class="col-md-8 col-md-offset-2">
        <div id="people">
            <v-client-table :data="tableData" :columns="columns">
                <template slot="edit" slot-scope="props">
                   <div>
                        <a class="fa fa-edit" :href="edit(props.row.id)">thing</a>
                    </div>
                </template>
            </v-client-table>
        </div>
    </div>
</template>

<script>
    import {ServerTable, ClientTable, Event} from 'vue-tables-2';
    import Vue from 'vue';
    import axios from 'axios';

    export default {
        methods: {
          edit: function(id){
                        return "edit/hello-" + id
          }
        },
        data() {
            return {
                columns: ['edit', 'id','name','age'],
                tableData: [
                    {id:1, name:"John",age:"20"},
                    {id:2, name:"Jane",age:"24"},
                    {id:3, name:"Susan",age:"16"},
                    {id:4, name:"Chris",age:"55"},
                    {id:5, name:"Dan",age:"40"}
                ]
            };
        }
    }
</script>

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-tables-2


    【解决方案1】:

    可以收听的表格组件emits a row-click event。它为您提供单击的行。我建议您不要尝试使用链接,而是监听事件并导航到您想要的位置。

    这是更新后的模板。

    <v-client-table :data="tableData" :columns="columns" @row-click="onRowClick">
    

    在方法中:

    onRowClick(event){
      window.location.href = `/edit/hello-${event.row.id}`
    }
    

    Example.

    【讨论】:

    • 太棒了,这完全正确。我知道我把事情复杂化了。非常感谢您的帮助!
    • 谢谢,正是我需要的。我用过这个。$router.push(/details/${event.row.id});取而代之的是 window.location,但完全按照您的描述工作 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 2013-07-21
    相关资源
    最近更新 更多