1

I have an array of JSON data like :

 loggers = [{
        "allAvailableLevel": ['WARN', 'DEBUG', 'INFO'],
        "level": "WARN",
        "logger": "com.test1",
        "status": "success"
    },
{
        "allAvailableLevel": ['WARN', 'DEBUG', 'INFO'],
        "level": "WARN",
        "logger": "com.test2",
        "status": "success"
    }
    ]

I am using dropdown inside a table column and for that using below code, and basically traversing Loggers array but not able to extract allAvailableLevel data.

 <table class="table table-bordered">
        <thead>
          <tr>
            <th>#</th>
            <th>Class</th>
            <th>Current Level</th>
            <th>All Available Levels</th>
            <!-- Only display "Action" header if level is changed-->
            <th>
              Action
            </th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(logger, index) in loggers" :key="logger">
            <td>{{ index + 1 }}</td>
            <td>{{ logger.logger }}</td>
            <td>{{ logger.level }}</td>
            <td>
              <b-dropdown
                boundary="viewport"
                id="dropdown-dropup"
                size="sm"
                :text="selectedLevelText"
                split
                class="m-2"
              >
                <b-dropdown-item-button
                  v-for="logger in loggers[0].allLevel"
                  :key="logger"
                  @click.prevent="changeLevel(level)"
                  >{{ logger }}</b-dropdown-item-button
                >
              </b-dropdown>
            </td>
            <td v-if="levelChanged">
              <b-button
                size="sm"
                variant="secondary "
                @click.prevent="updateLevel(selectedLevelText)"
                >Update</b-button
              >
            </td>
          </tr>
        </tbody>
      </table>

with above code my dropdown looks like :

enter image description here

I want to display it like this :

enter image description here

How do I traverse my data inside the vue template to get only the data of "allAvailableLevel"?

enter image description here

3
  • What is loggers inside v-for="logger in loggers"? Commented Mar 31, 2020 at 14:52
  • @palaѕн loggers is an array of json data which I have mentioned above. Commented Mar 31, 2020 at 14:53
  • Then try v-for="logger in loggers[0].allAvailableLevel" and update text like {{ logger }} Commented Mar 31, 2020 at 14:55

2 Answers 2

2

You should iterate over that nested array as follows :

 <b-dropdown-item-button
                      v-for="level in logger.allAvailableLevel"
                      :key="level"
                      @click.prevent="changeLevel(level)"
                      >{{ level }}</b-dropdown-item-button
                    >
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I updated my question, now I have more than one element , so hardcoding with Loggers[0] would not solve the problem.
it seems that you're looping over that array in order to generate a table with a column containing a dropdown, please share that table code
0

Do this:

<tr v-for="(logger, index) in loggers" :key="logger">
  <td>{{ index + 1 }}</td>
  ...
  <td>
    <b-dropdown
      boundary="viewport"
      id="dropdown-dropup"
      size="sm"
      :text="selectedLevelText"
      split
      class="m-2"
    >
      <b-dropdown-item-button
        v-for="level in logger.allAvailableLevel"
        :key="level"
        @click.stop="changeLevel(level)"
        >{{ logger }}</b-dropdown-item-button
      >
    </b-dropdown>
  </td>
</tr>

1 Comment

But how can we do it without hardcoding loggers[0]?

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.