0

I have some data I'm trying to display in a b-table

It works fine with most data, but when I hit another array inside the referenced array, it doesn't render properly.

So I tripped over this markup

<template v-slot:cell(name)="data">
   <b class="text-info">{{ data.value.last.toUpperCase() }}</b>, <b>{{ data.value.first}}</b>
</template>

From bootstraps' table documentation.

That's great! Unfortunately, I can't get data out properly.

The issue is that the data contains different types of arrays depending on what is being used:

"range": {
        "type": "point",
        "distance": {
           "type": "touch"
         }
},

and

"range": {
     "type": "point",
      "distance": {
       "type": "feet",
       "amount": 150
    }
},

So I can easily retrieve the type variable, as it's always there, but in case distance.type and distance.amount exists, I'd want to display that as well. Unfortunately this causes an error - because sometimes it doesn't contain amount.

<b-table striped hover :items="spells">
   <template v-slot:cell(range)="rangeData">
      <b class="text-info">
         {{rangeData.item.range.distance}}, <--- this displays the content of the problematic array
         {{rangeData.item.range.distance.feet}}, <-- this causes undefined errors and fails to load
         {{rangeData.item.range.type}}  <--- this displays properly
     </b>
   </template>
</b-table>

So a solution I was hoping to find, was to make a conditional field, which checks if the data is reachable, if not, don't display anything. But I can't find any answers or documentation for it.

Wrapping distance.feet in a v-if still causes errors and then doesn't display anything.

2
  • {{rangeData.item.range.distance.feet}}, is it correct ? i think it should have {{rangeData.item.range.distance.type}}, Commented May 12, 2020 at 20:20
  • type displays, because it's always there - but that's not the value I'm looking for. Commented May 12, 2020 at 20:57

1 Answer 1

1

According to your question, you are trying to use condition to show data if available otherwise you don't want to show.

For that you simply have do as below-

<b class-"text-info">
   {{rangeData.item.range && rangeData.item.range.distance ? rangeData.item.range.distance: "" }},
   {{rangeData.item.range && rangeData.item.range.distance && rangeData.item.range.distance.feet ? rangeData.item.range.distance.feet: "" }},
   {{rangeData.item.range && rangeData.item.range.type ? rangeData.item.range.type: "" }}
</b>
Sign up to request clarification or add additional context in comments.

1 Comment

I love you. This has been driving me nuts for at day. <3 Worked like a charm!

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.