0

I have table with users datas in vue bootstrap. I'm using Vue Router to go on user profile page too. Profile pages path is /user/idNumber and I want to route it under Name. Something like this:

<a href="/users/idNumber"> Name </a>

How I can do that?

Code:

<b-table striped hover :items="usersList" :fields="fields">
    <template v-slot:cell(_id)="data">
        <router-link :to="`/user/${data.value}`">{{ data.value }}</router-link>
    </template>
    <template v-slot:cell(fullname)="data">
        <router-link :to="`/user/${data.value}`">{{ data.value }}</router-link>
    </template>
</b-table>

usersList:

[{"_id":"5df9663acf06e2001742ac17","name":"Retdadada5155465","lastname":"Nienow","email":"[email protected]","age":"1995","gender":"female","weight":58,"height":166,"activity":1.4,"value":3,"_createdOn":"2019-12-17T23:35:22.339Z","_updatedOn":"2019-12-19T20:57:30.588Z"},{"_id":"5df842f2cf06e2001742a8ec","name":"Retdadada516","lastname":"Nienow","email":"[email protected]","age":"1993","gender":"female","weight":58,"height":166,"activity":1.4,"value":3,"_createdOn":"2019-12-17T02:52:34.183Z","_updatedOn":"2019-12-19T20:57:42.352Z"},{"_id":"5df7c972bca42200177decb4","name":"Lue","lastname":"Schneider","email":"[email protected]","age":"1997","gender":"female","weight":60,"height":180,"activity":1.6,"_createdOn":"2019-12-16T18:14:10.554Z","_updatedOn":"2019-12-19T20:57:51.550Z"}]'

fields:

["_id",
                {
                    key: "fullname",
                    label: "Fullname",
                    sortable: true,
                    formatter: (value, key, item) => {
                        return item.name + " " + item.lastname
                    },
                },
                {
                    key: "birthYear",
                    label: "Age: ",
                    sortable: true,
                    formatter: (value, key, item) => {
                        return new Date().getFullYear() - item.age
                    },
                },
            ],

Regards

4
  • What does your userList and fields looks like ? Commented Dec 19, 2019 at 21:37
  • I just add this lines. Commented Dec 19, 2019 at 21:42
  • So you should be able to click on the name, and then it should redirect you, right? Commented Dec 19, 2019 at 21:43
  • Yeah. Redirect to "users/userID" not /name. Commented Dec 19, 2019 at 21:44

2 Answers 2

1

A little change to your second template with the fullname does the job. The first one should be alright as i see it already.

You just have to use the data.item instead, to utilize whatever data is in the looped item

<template>
  <b-table striped hover :items="usersList" :fields="fields">
    <template v-slot:cell(_id)="data">
      <router-link :to="`/user/${data.value}`">{{ data.value }}</router-link>
    </template>
    <template v-slot:cell(fullname)="data">
      <router-link :to="`/user/${data.item._id}`">{{ data.value }}</router-link>
    </template>
  </b-table>
</template>

Working sandbox: https://codesandbox.io/s/bootstrap-vue-sandbox-wvv9j

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Now it'ok, I earlier tried with only ":to="/user/${item._id}"... Quite shame.
0

If you look at the documentation here: https://bootstrap-vue.js.org/docs/components/table/#scoped-field-slots you see that when using a scoped field slot, the data object has several properties. One of them is item which represents the item for that row as a whole. So you can do this instead:

<router-link :to="`/user/${data.item.someProperty}`">
    {{data.value}}
</router-link>

Where someProperty is the property of the user object you want to put in the route path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.