Practical 7
Practical 7
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Practical 7a</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
</head>
<body>
<div id="app">
<p v-uppercase>This text will turn into uppercase when clicked.</p>
</div>
<script src="app.js"></script>
</body>
</html>
App.js
Vue.directive('uppercase', {
bind(el) {
el.style.cursor = 'pointer';
el.addEventListener('click', () => {
const text = el.innerText;
el.innerText = text.toUpperCase();
});
}
});
new Vue({
el: '#app'
});
II. Creating a Dynamic List with Vue.js Custom Directive
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task Management App</title>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.3/css/bulma.min.css">
</head>
<body>
<div id="app" class="container">
<h1 class="title">Task Management App</h1>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script>
Vue.directive('uppercase', {
bind(el) {
el.style.cursor = 'pointer';
},
update(el) {
if (el.tagName === 'SPAN') {
el.innerText = el.innerText.toUpperCase();
}
}
});
new Vue({
el: '#app',
data: {
newTask: '',
newDeadline: '',
tasks: [],
priority: 'low',
today: new Date().toISOString().split('T')[0]
},
methods: {
addTask() {
if (this.newTask && this.newDeadline) {
this.tasks.push({ description: this.newTask, deadline:
this.newDeadline, priority: this.priority });
this.newTask = '';
this.newDeadline = '';
this.priority = 'low';
}
},
toggleCase(task) {
task.description = task.description === task.description.toUpperCase() ?
task.description.toLowerCase() : task.description.toUpperCase();
},
formatDate(dateString) {
const options = { year: 'numeric', month: 'long', day: 'numeric' };
return new Date(dateString).toLocaleDateString('en-US', options);
},
getPriorityClass(priority) {
return {
'has-text-danger': priority === 'high',
'has-text-warning': priority === 'medium',
'has-text-success': priority === 'low'
};
},
checkDate(event) {
const selectedDate = new Date(event.target.value);
const today = new Date();
if (selectedDate < today) {
event.target.value = '';
alert('Please select a future date.');
}
},
markAsDone(index) {
this.tasks.splice(index, 1);
}
}
});
</script>
</body>
</html>
III. Create a directive that formats and displays dates in a human-readable format
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Directive for Date Formatting</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
</head>
<body>
<div id="app">
<p v-format-date="date">
</p>
</div>
<script>
Vue.directive('format-date', {
inserted: function(el, binding) {
const date = binding.value;
const formattedDate = formatDate(date);
el.textContent = formattedDate;
}
});
new Vue({
el: '#app',
data: {
date: '2022-03-05T12:00:00'
}
});
function formatDate(dateString) {
const options = { year: 'numeric', month: 'long', day: 'numeric' };
return new Date(dateString).toLocaleDateString('en-US', options);
}
</script>
</body>
</html>