React butifule dnd мне нужно что бы мой dnd работал с такой структурой const initial = {…
- User
React butifule dnd мне нужно что бы мой dnd работал с такой структурой const initial = { 'В работе': { id: 1, tasks: [ { id: '1', name: 'Модуль Сервис 1' }, { id: '2', name: 'Модуль Сервис 2' }, ] }, 'Готово к тестированию': { id: 2, tasks: [ { id: '3', name: 'Модуль Сервис 3' }, { id: '4', name: 'Модуль Сервис 4' }, ] } Но сейчас он может работать только с такой const initial = { 'C1': [ { id: '1', name: 'Модуль Сервис 1' }, { id: '2', name: 'Модуль Сервис 2' }, ], 'C2': [ { id: '3', name: 'Модуль Сервис 3' }, { id: '4', name: 'Модуль Сервис 4' }, ], }
- Assistant
Чтобы сделать это, вам нужно немного изменить структуру данных и код для работы с такой новой структурой. Вот пример реализации:
- Импортируйте необходимые компоненты:
import React, { useState } from "react"; import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";- Преобразуйте исходные данные:
const initialData = { 'В работе': { id: 1, tasks: [ { id: '1', name: 'Модуль Сервис 1' }, { id: '2', name: 'Модуль Сервис 2' }, ] }, 'Готово к тестированию': { id: 2, tasks: [ { id: '3', name: 'Модуль Сервис 3' }, { id: '4 ', name: 'Модуль Сервис 4' }, ] } }; const columnsFromBackend = Object.keys(initialData).map((key, index) => { return { id: `column-${index + 1}`, title: key, items: initialData[key].tasks }; }); const [columns, setColumns] = useState(columnsFromBackend);- Модифицируйте функцию
onDragEndдля работы с новой структурой данных:
const onDragEnd = (result, columns, setColumns) => { if (!result.destination) return; const { source, destination } = result; if (source.droppableId !== destination.droppableId) { const sourceColumn = columns.find(column => column.id === source.droppableId); const destColumn = columns.find(column => column.id === destination.droppableId); const sourceItems = Array.from(sourceColumn.items); const destItems = Array.from(destColumn.items); const [removed] = sourceItems.splice(source.index, 1); destItems.splice(destination.index, 0, removed); setColumns( columns.map(column => { if (column.id === sourceColumn.id) { column.items = sourceItems; } if (column.id === destColumn.id) { column.items = destItems; } return column; }) ); } else { const column = columns.find(column => column.id === source.droppableId); const copiedItems = Array.from(column.items); const [removed] = copiedItems.splice(source.index, 1); copiedItems.splice(destination.index, 0, removed); setColumns( columns.map(column => { if (column.id === source.droppableId) { column.items = copiedItems; } return column; }) ); } };- Обновите компоненты
DraggableиDroppableдля работы с новой структурой данных:
return ( <div> <DragDropContext onDragEnd={result => onDragEnd(result, columns, setColumns)} > {columns.map((column, index) => ( <Droppable droppableId={column.id} key={index}> {(provided) => ( <div {...provided.droppableProps} ref={provided.innerRef} > <div>{column.title}</div> {column.items.map((item, index) => ( <Draggable key={item.id} draggableId={item.id} index={index} > {(provided) => ( <div ref={provided.innerRef} {...provided.draggableProps} {...provided.dragHandleProps} > {item.name} </div> )} </Draggable> ))} {provided.placeholder} </div> )} </Droppable> ))} </DragDropContext> </div> );Теперь ваш компонент с drag-and-drop должен работать с вашей исходной структурой данных. Просто замените константу
initialнаinitialData, и ваш компонент будет готов к использованию.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.