export default defineComponent({ name: 'ActionsCell', components: {}, props: { row:…
- User
export default defineComponent({ name: 'ActionsCell', components: {}, props: { row: Object as any }, setup(_, { emit }) { const editProject = (project: Project.FProject) => { console.log(project.id, '1') emit('editProject', project.id) } } })
export default defineComponent({ name: 'ProjectsTable', components: { TanstackTable }, props: { projects: { type: Array as PropType<Project.FProject[]> } }, setup(props, { emit }) { const projectsColumns = [ { id: 'select', header: ({ table }: { table: any }) => h(CustomCheck, { props: { checked: table.getIsAllRowsSelected(), indeterminate: table.getIsSomeRowsSelected() }, nativeOn: { change: table.getToggleAllRowsSelectedHandler() } }), cell: ({ row }: { row: any }) => h(CustomCheck, { props: { checked: row.getIsSelected(), disabled: !row.getCanSelect() } }), size: 70 }, columnHelper.accessor(row => row.id, { id: 'id', cell: info => h('router-link', { class: 'link', props: { to:
/project/${info.row.original.id}} }, [(info.getValue() || '-').toString()]), header: 'ID', size: 80 }), { id: 'actions', cell: info => h(ActionsCell, { props: { row: info.row.original } }), header: t('common.actions').toString(), size: 150 } ]<tanstack-table :tableColumns="projectsColumns" @editProject="editProject" />
<project-item v-for="project in projects" :key="project.id" :project="project" @editProject="editProject" />как поднять событие editProject с параметром id до компонента project item
- Assistant
To ensure that the
editProjectevent with the project ID is properly propagated from theActionsCellcomponent up to theProjectItemcomponent, you need to make sure that each layer of your component hierarchy listens for and re-emits the event appropriately. Here's how you can modify your code to handle this:ActionsCellComponent: This component should emiteditProjectas it's already done.
// In ActionsCell.vue export default defineComponent({ name: 'ActionsCell', props: { row: Object as any }, setup(_, { emit }) { const editProject = (project: any) => { console.log(project.id, '1'); emit('editProject', project.id); }; return { editProject }; } });ProjectsTableComponent: This component should listen for theeditProjectevent fromActionsCelland then emit this event again upward.
// In ProjectsTable.vue export default defineComponent({ name: 'ProjectsTable', components: { TanstackTable }, props: { projects: { type: Array as PropType<Project.FProject[]> } }, setup(props, { emit }) { const projectsColumns = [ // ... other columns ... { id: 'actions', cell: info => h(ActionsCell, { props: { row: info.row.original }, on: { editProject: (projectId: string) => { emit('editProject', projectId) } } }), header: t('common.actions').toString(), size: 150 } ]; const editProject = (projectId: string) => { // Handle the edit here, or emit further up if needed console.log('Edit project with ID:', projectId); emit('editProject', projectId); }; return { projectsColumns, editProject }; } });ProjectItemComponent: This assumes you have aProjectItemcomponent. It should receive theeditProjectevent and can handle any logic or also re-emit if needed.
<template> <div> <!-- ProjectItem template goes here --> </div> </template> <script> export default { name: 'ProjectItem', props: { project: Object }, methods: { onEditProject(projectId) { // Handle the edit, or re-emit console.log('Edit project at ProjectItem with ID:', projectId); this.$emit('editProject', projectId); } } }; </script>- Template Usage: Ensure your
ProjectItemcomponent listens for theeditProjectevent as it gets emitted from the parentProjectsTable.
<project-item v-for="project in projects" :key="project.id" :project="project" @editProject="editProject" />By following these steps, each component layer ensures correct event propagation:
ActionsCellemits theeditProjectevent,ProjectsTablelistens for it and re-emits, andProjectItemlistens to respond accordingly. Make sure the event names and handlers are consistent throughout your components.