Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boyjarv's avatar

How can I show there are no todos?

<template>
  <div v-if="todo.title.length >= 0" class="bg-gray-200 h-100 p-4 rounded m-4">
    <div class="my-2">
      <span class="font-bold">({{ todo.id }})&nbsp;{{ todo.title }}</span
      ><br />
      {{ todo }}
      {{ todo.description }}<br />
    </div>
    <div class="flex flex-row items-center justify-between">
      <AppButton
        type="delete"
        :processing="isLoading"
        @click.prevent="deleteTodo(todo.id)"
        data-element="button"
        >Delete&nbsp;&nbsp;<i class="fa-solid fa-trash"></i
      ></AppButton>
    </div>
  </div>
  <div v-else>
    <p>There are no todos!</p>
  </div>
</template>

<script>
import Swal from "sweetalert2";
import { ref } from "vue";
import axios from "axios";
import AppButton from "@/components/AppButton.vue";
export default {
  components: {
    AppButton,
  },
  name: "ContactDetails",
  props: {
    todoDetails: {},
  },
  data() {
    return {
      title: "",
      description: "",
      isOpen: ref(false),
      isEdit: false,
      todo: this.todoDetails,
    };
  },
  methods: {
    deleteTodo(id) {
      Swal.fire({
        title: "Are you sure?",
        text: "You won't be able to revert this!",
        icon: "warning",
        showCancelButton: true,
        confirmButtonColor: "#3085d6",
        cancelButtonColor: "#d33",
        confirmButtonText: "Yes, delete it!",
      }).then((result) => {
        if (result.isConfirmed) {
          Swal.fire("Deleted!", "Your file has been deleted.", "success");
          axios.delete(`todo/${id}`);
          this.$emit("remove");
        }
      });
      // this.isOpen = true;
      // this.isEdit = true;
    },
    closeModal() {
      this.isOpen = !this.isOpen;
    },
  },
};
</script>

When there is a todo, it shows up and {{ todo }} outputs as:

{ "id": 88, "title": "gfh", "description": "fghf", "status": "New", "created_at": "2022-10-30T23:10:27.000000Z", "updated_at": "2022-10-30T23:10:27.000000Z" } 

How can I show? There are no todos!

when I delete all todos, nothing is shown?!

0 likes
2 replies
lbecket's avatar

The first thing that jumps out is that your check for todo.title.length >= 0 seems prone to failure. If the property exists and it's an empty string, then this will still evaluate to true, but if the property doesn't exist then you'll get an error because you can't call the length method on an undefined property. You could fix this with todo.title?.length > 0, but I almost wonder if you're better off checking for the existence of the entire object. I'm not sure what the data structure is that you're passing through todoDetails, but my assumption would be that it's an array since you presumably have many todos. If that's the case, then what you probably want is is to evaluate that the count of "todos" in the passed array is greater than zero, which could be done with v-if="todo.length > 0"

1 like

Please or to participate in this conversation.