/ Gists / use cases - computed properties
On gists

use cases - computed properties

Vue.js

shopping-cart.js Raw #

<template>
  <div>
    <ul>
      <li v-for="item in cartItems" :key="item.id">
        {{ item.name }} - {{ item.price }} - Quantity: {{ item.quantity }}
      </li>
    </ul>
    <p>Total Price: {{ totalPrice }}</p>
    <p>Total Quantity: {{ totalQuantity }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cartItems: [
        { id: 1, name: 'Item 1', price: 10, quantity: 2 },
        { id: 2, name: 'Item 2', price: 15, quantity: 1 },
        { id: 3, name: 'Item 3', price: 20, quantity: 3 }
      ]
    };
  },
  computed: {
    totalPrice() {
      return this.cartItems.reduce((total, item) => total + item.price * item.quantity, 0);
    },
    totalQuantity() {
      return this.cartItems.reduce((total, item) => total + item.quantity, 0);
    }
  }
};
</script>

form-validation.js Raw #

<template>
  <form @submit="submitForm">
    <input v-model="name" :class="{ 'is-invalid': !isNameValid }" type="text" placeholder="Name" required>
    <input v-model="email" :class="{ 'is-invalid': !isEmailValid }" type="email" placeholder="Email" required>
    <button type="submit" :disabled="!isFormValid">Submit</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      name: '',
      email: ''
    };
  },
  computed: {
    isNameValid() {
      return this.name.length > 0;
    },
    isEmailValid() {
      return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.email);
    },
    isFormValid() {
      return this.isNameValid && this.isEmailValid;
    }
  },
  methods: {
    submitForm() {
      // Submit form logic
    }
  }
};
</script>

filtering-sorting.js Raw #

<template>
  <div>
    <select v-model="selectedCategory">
      <option value="">All</option>
      <option v-for="category in categories" :key="category.id" :value="category.id">{{ category.name }}</option>
    </select>

    <ul>
      <li v-for="product in filteredProducts" :key="product.id">
        {{ product.name }} - {{ product.price }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedCategory: '',
      categories: [
        { id: 1, name: 'Category 1' },
        { id: 2, name: 'Category 2' },
        { id: 3, name: 'Category 3' }
      ],
      products: [
        { id: 1, name: 'Product 1', price: 10, categoryId: 1 },
        { id: 2, name: 'Product 2', price: 15, categoryId: 2 },
        { id: 3, name: 'Product 3', price: 20, categoryId: 3 }
      ]
    };
  },
  computed: {
    filteredProducts() {
      if (this.selectedCategory) {
        return this.products.filter(product => product.categoryId === this.selectedCategory);
      } else {
        return this.products;
      }
    }
  }
};
</script>

pagination.js Raw #

<template>
  <div>
    <ul>
      <li v-for="item in paginatedItems" :key="item.id">
        {{ item.name }} - {{ item.price }}
      </li>
    </ul>

    <div>
      <button @click="previousPage" :disabled="currentPage === 1">Previous</button>
      <span>Page {{ currentPage }} of {{ totalPages }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 3,
      items: [
        { id: 1, name: 'Item 1', price: 10 },
        { id: 2, name: 'Item 2', price: 15 },
        { id: 3, name: 'Item 3', price: 20 },
        { id: 4, name: 'Item 4', price: 25 },
        { id: 5, name: 'Item 5', price: 30 },
        { id: 6, name: 'Item 6', price: 35 },
        { id: 7, name: 'Item 7', price: 40 }
      ]
    };
  },
  computed: {
    paginatedItems() {
      const startIndex = (this.currentPage - 1) * this.itemsPerPage;
      const endIndex = startIndex + this.itemsPerPage;
      return this.items.slice(startIndex, endIndex);
    },
    totalPages() {
      return Math.ceil(this.items.length / this.itemsPerPage);
    }
  },
  methods: {
    previousPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
      }
    }
  }
};
</script>

conditional-styling.js Raw #

<template>
  <div>
    <ul>
      <li v-for="task in tasks" :key="task.id" :class="{ 'task-overdue': isOverdue(task), 'task-completed': task.completed }">
        {{ task.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tasks: [
        { id: 1, name: 'Task 1', dueDate: '2023-06-01', completed: false },
        { id: 2, name: 'Task 2', dueDate: '2023-05-30', completed: true },
        { id: 3, name: 'Task 3', dueDate: '2023-06-02', completed: false }
      ]
    };
  },
  methods: {
    isOverdue(task) {
      const dueDate = new Date(task.dueDate);
      const today = new Date();
      return dueDate < today;
    }
  }
};
</script>

<style scoped>
.task-overdue {
  color: red;
  font-weight: bold;
}

.task-completed {
  text-decoration: line-through;
}
</style>