screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/vue@3.2.12/dist/vue.global.js"></script> </head> <body> <div id="app" class="filter-container"> <h2 class="filter-heading">Vue Js filter between two dates</h2> <h3 class="filter-subheading">Choose Date</h3> <input type="date" v-model="startDate" @change="validateDates" class="date-input" /> <input type="date" v-model="endDate" @change="validateDates" class="date-input" /> <ul class="filtered-list"> <li v-for="item in filteredItems" :key="item.id" class="list-item">{{ item.name }}</li> </ul> <p v-if="invalidDates" class="error-message">Invalid date range</p> </div> <script type="module"> const app = Vue.createApp({ data() { return { startDate: "", endDate: "", items: [ { id: 1, name: "Item 1", date: "2023-07-05" }, { id: 2, name: "Item 2", date: "2023-07-08" }, { id: 3, name: "Item 3", date: "2023-07-10" }, { id: 4, name: "Item 4", date: "2023-07-12" }, { id: 5, name: "Item 5", date: "2023-07-15" }, { id: 6, name: "Item 6", date: "2023-07-18" }, { id: 7, name: "Item 7", date: "2023-07-20" }, { id: 8, name: "Item 8", date: "2023-07-22" }, { id: 9, name: "Item 9", date: "2023-07-25" }, { id: 10, name: "Item 10", date: "2023-07-28" }, { id: 11, name: "Item 11", date: "2023-07-30" }, { id: 12, name: "Item 12", date: "2023-08-02" }, { id: 13, name: "Item 13", date: "2023-08-05" }, { id: 14, name: "Item 14", date: "2023-08-08" }, { id: 15, name: "Item 15", date: "2023-08-10" }, { id: 16, name: "Item 16", date: "2023-08-12" }, { id: 17, name: "Item 17", date: "2023-08-15" }, ], invalidDates: false, }; }, computed: { filteredItems() { if (!this.startDate || !this.endDate || this.invalidDates) { return []; // Return an empty array if no date range specified or invalid dates } return this.items.filter((item) => { const itemDate = new Date(item.date); const start = new Date(this.startDate); const end = new Date(this.endDate); return itemDate >= start && itemDate <= end; }); }, }, methods: { validateDates() { this.invalidDates = false; if (this.startDate && this.endDate) { const startDate = new Date(this.startDate); const endDate = new Date(this.endDate); if (startDate > endDate) { this.invalidDates = true; } } }, }, }); app.mount("#app"); </script> <style scoped> .filter-container { max-width: 600px; margin: 0 auto; padding: 20px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .filter-heading { font-size: 24px; color: #333; margin-bottom: 10px; } .filter-subheading { font-size: 18px; color: #555; margin-bottom: 10px; } .date-input { width: 200px; padding: 8px; border: 1px solid #ccc; border-radius: 4px; margin-bottom: 10px; } .filtered-list { list-style: none; padding: 0; } .list-item { font-size: 16px; color: #555; margin-bottom: 5px; } .error-message { font-size: 16px; color: red; margin-top: 10px; } </style> </body> </html>