我如何根据数据库中的条件在vue中创建一个类

问题描述 投票:0回答:1

我有多个 div 元素,其类基于特殊条件。一旦我从数据库收到数据,我就会检查这个条件。问题是组件是在从数据库接收数据之前渲染的。我尝试过创建一个异步函数并使用await,但它不起作用。 (我对JS和Vue不太精通)

成分:

<v-table v-if="allActions.length != 0">
  <thead>
    <tr>
      <th class="text-center text-blue" :colspan="lastWeekCount + 1">Недели</th>
    </tr>
    <tr>
      <th class="text-left text-blue">Actions</th>
      <th v-for="week in 15" :key="week" class="text-center text-blue">
        {{ week + firstWeekCount - 1 }}
      </th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="action in allActions" :key="action">
      <td>{{ action.reason }}</td>
      <td v-for="week in 15" :key="week" class="text-center">
        <div>
          <v-dialog max-width="500">
            <template
              v-slot:activator="{
                props: activatorProps,
              }"
            >
              <div
                v-if="checkActionStatus(week + firstWeekCount - 1, action.id)"
                @click="selectChosenAction(week + firstWeekCount - 1, action.id)"
                v-bind="activatorProps"
                :class="checkActionStatus(week + firstWeekCount - 1, action.id)"
                style="height: 15px; width: 15px"
              ></div>
            </template>

            <template v-slot:default="{ isActive }">
              <v-card title="Change a status">
                <v-card-text>
                  <v-select
                    v-model="chosenAction.status"
                    :items="['OK', 'nOK', 'Empty']"
                    label="Status state"
                    :disabled="isStatus"
                    item-title="name"
                    return-object
                    variant="solo"
                  ></v-select>
                  <div v-if="isStatus" class="text-red-lighten-1">
                    Action is already chosen
                  </div>
                </v-card-text>
                <v-card-actions>
                  <v-spacer></v-spacer>
                  <v-btn text="Close" @click="isActive.value = false"></v-btn>
                  <v-btn
                    class="ma-2"
                    color="success"
                    variant="flat"
                    v-bind="props"
                    @click="updateChosenAction"
                    >Save</v-btn
                  >
                </v-card-actions>
              </v-card>
            </template>
          </v-dialog>
        </div>
      </td>
    </tr>
  </tbody>
</v-table>

带有异步等待的函数:

const checkActionStatus = async (week, action_id) => {
  try {
    const response = await httpServer.post("selectActionByWeek", { week: week, action: action_id });
    if (response.data[0]) {
      switch (response.data[0].status) {
        case 'Empty':
          return "bg-blue-grey-lighten-3 rounded-circle mx-auto";
        case 'OK':
          return "bg-light-green-accent-3 rounded-circle mx-auto";
        case 'nOK':
          return "bg-amber-lighten-2 rounded-circle mx-auto";
        default:
          return "bg-blue-grey-lighten-3 rounded-circle mx-auto";
      }
    } else {
      return "bg-blue-grey-lighten-3 rounded-circle mx-auto";
    }
  } catch (error) {
    console.log(error);
    return "bg-blue-grey-lighten-3 rounded-circle mx-auto";
  }
};
vue.js
1个回答
0
投票

OP 将他的电话转移到

@click
,从这个其他答案中获得一些灵感也有一点帮助。

© www.soinside.com 2019 - 2024. All rights reserved.