edit(update) working
This commit is contained in:
parent
32830a9dd2
commit
7822f0938e
@ -49,65 +49,66 @@
|
|||||||
|
|
||||||
<div v-else class="text-gray-500">Loading student info...</div>
|
<div v-else class="text-gray-500">Loading student info...</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, onMounted } from 'vue'
|
import { ref, onMounted } from 'vue'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
import { useDirectus } from '../../app/composables/useDirectus'
|
import { useDirectus } from '../../app/composables/useDirectus'
|
||||||
|
|
||||||
const directus = useDirectus()
|
const { fetchCollection, updateItem } = useDirectus()
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
const studentId = route.query.id
|
const studentId = route.query.id
|
||||||
const form = ref(null)
|
const form = ref(null)
|
||||||
const classes = ref([])
|
const classes = ref([])
|
||||||
|
|
||||||
const fetchClasses = async () => {
|
const fetchClasses = async () => {
|
||||||
try {
|
try {
|
||||||
const { data } = await directus.items('classes').readByQuery({
|
classes.value = await fetchCollection('classes', {
|
||||||
fields: ['id', 'name', 'section'],
|
fields: ['id', 'name', 'section'],
|
||||||
sort: ['name']
|
sort: ['name']
|
||||||
})
|
})
|
||||||
classes.value = data
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error loading classes:', err)
|
console.error('Error loading classes:', err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchStudent = async () => {
|
const fetchStudent = async () => {
|
||||||
try {
|
try {
|
||||||
const student = await directus.items('students').readOne(studentId)
|
const studentData = await fetchCollection('students', {
|
||||||
form.value = student
|
filter: { id: { _eq: studentId } },
|
||||||
|
limit: 1
|
||||||
|
})
|
||||||
|
form.value = studentData[0] // Get first item from array
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error loading student:', err)
|
console.error('Error loading student:', err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await fetchClasses()
|
await fetchClasses()
|
||||||
await fetchStudent()
|
await fetchStudent()
|
||||||
})
|
})
|
||||||
|
|
||||||
const submitForm = async () => {
|
const submitForm = async () => {
|
||||||
try {
|
try {
|
||||||
await directus.items('students').updateOne(studentId, form.value)
|
await updateItem('students', studentId, form.value)
|
||||||
alert('Student updated successfully!')
|
alert('Student updated successfully!')
|
||||||
router.push('/students')
|
router.push('/students')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error updating student:', err)
|
console.error('Error updating student:', err)
|
||||||
alert('Failed to update student.')
|
alert('Failed to update student.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
.input {
|
.input {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
padding: 0.5rem;
|
padding: 0.5rem;
|
||||||
border: 1px solid #ccc;
|
border: 1px solid #ccc;
|
||||||
border-radius: 0.375rem;
|
border-radius: 0.375rem;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user