【发布时间】:2021-11-22 19:01:27
【问题描述】:
我有一个 DataGrid 表,其中包含来自 Firebase 的数据,我想知道如何删除和更新 Firebase 信息?
我有这段代码可以删除该行,它确实可以工作但是,因为我没有添加任何东西来更新 firebase,它不会永久删除它(这很有意义):
编辑:删除了不必要的代码以保留删除功能 这是在检查一行之后,然后它让您删除该检查的行(并且它有效),但我没有看到空间(它带来编译错误)在那段代码中添加 firebase delete() 函数。
<IconButton
onClick={() => {
const selectedIDs = new Set(selectionModel);
setEstudiantes((r) =>
r.filter((x) =>
!selectedIDs.has(x.id)
));
}}
>
<DeleteOutlinedIcon />
</IconButton>
这就是我检查行的方式(它确实有效):
checkboxSelection
//Store Data from the row in another variable
onSelectionModelChange = {(id) => {
setSelectionModel(id);
const selectedIDs = new Set(id);
const selectedRowData = estudiantes.filter((row) =>
selectedIDs.has(row.id)
);
setEstudiantesData(selectedRowData)
}
}
{...estudiantes}
但是,我确实有与我的 firebase 连接的删除功能,并删除了我在迁移到 MUI DataGrid 之前所做的文档,但我不知道如何集成它。这就是您通常在 firebase 中删除某些内容的方式
db.collection("usuarios")
.doc(user.uid)
.collection("estudiantes")
.doc(document name variable)
.delete();
感谢您的任何提示/帮助。
*更新这是它的样子
它会按预期进行删除,但不会更新 firebase 和 idk 在哪里添加执行此操作的代码,因为我尝试添加它时出现错误:
如果我只是刷新它就会回来:
更新添加DataGrid的代码:
return (
<Container fixed>
<Box mb={5} pt={2} sx={{textAlign:'center'}}>
<Button
startIcon = {<PersonAddIcon />}
variant = "contained"
color = "primary"
size = "medium"
onClick={crearEstudiante} >
Crear Estudiantes
</Button>
<Box pl={25} pt={2} mb={2} sx={{height: '390px', width: "850px", textAlign:'center'}}>
<DataGrid
rows={estudiantes}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5]}
components={{
Toolbar: CustomToolbar,
}}
checkboxSelection
//Store Data from the row in another variable
onSelectionModelChange = {(id) => {
setSelectionModel(id);
const selectedIDs = new Set(id);
const selectedRowData = estudiantes.filter((row) =>
selectedIDs.has(row.id)
);
setEstudiantesData(selectedRowData)
}
}
{...estudiantes}
/>
</Box>
<Button
startIcon = {<ShoppingCartSharpIcon />}
variant = "contained"
color = "primary"
size = "medium"
onClick={realizarPedidos} >
Crear pedido
</Button>
</Box></Container>
)
更新添加当我尝试添加逻辑以更新 firebase 时出现的错误图片,无论我将其放在删除逻辑中的哪个位置,它只会给我一个错误,我老实说不知道放在哪里,因为我不太了解 DataGrid 上 MUI 的选择
更新添加我的整个代码:
import React, { useState, useEffect} from 'react'
import {db} from './firebase';
import { useHistory } from 'react-router-dom';
import "./ListadoEstudiantes.css"
import { DataGrid,
GridToolbarContainer, GridToolbarFilterButton, GridToolbarDensitySelector} from '@mui/x-data-grid';
import { Button, Container } from "@material-ui/core";
import { IconButton} from '@mui/material';
import PersonAddIcon from '@mui/icons-material/PersonAddSharp';
import ShoppingCartSharpIcon from '@mui/icons-material/ShoppingCartSharp';
import DeleteOutlinedIcon from '@mui/icons-material/DeleteOutlined';
import { Box } from '@mui/system';
function ListadoEstudiantes({user}) {
const history = useHistory("");
const crearEstudiante = () => {
history.push("/Crear_Estudiante");
};
const [estudiantesData, setEstudiantesData] = useState([])
const parseData = {
pathname: '/Crear_Pedidos',
data: estudiantesData
}
const realizarPedidos = () => {
if(estudiantesData == 0)
{
window.alert("Seleccione al menos un estudiante")
}
else {
history.push(parseData);
}
};
function CustomToolbar() {
return (
<GridToolbarContainer>
<GridToolbarFilterButton />
<GridToolbarDensitySelector />
</GridToolbarContainer>
);
}
const [estudiantes, setEstudiantes] = useState([]);
const [selectionModel, setSelectionModel] = useState([]);
const columns = [
{ field: 'id', headerName: 'ID', width: 100 },
{field: 'nombre', headerName: 'Nombre', width: 200},
{field: 'colegio', headerName: 'Colegio', width: 250},
{field: 'grado', headerName: 'Grado', width: 150},
{
field: "delete",
width: 75,
sortable: false,
disableColumnMenu: true,
renderHeader: () => {
return (
<IconButton
onClick={() => {
const selectedIDs = new Set(selectionModel);
setEstudiantes((r) =>
r.filter((x) =>
!selectedIDs.has(x.id)
));
}}
>
<DeleteOutlinedIcon />
</IconButton>
);
}
}
];
const deleteProduct = (estudiante) => {
if (window.confirm('Quiere borrar este estudiante ?')){
db.collection("usuarios").doc(user.uid).collection("estudiantes").doc(estudiante).delete();
}
}
useEffect(() => {
}, [estudiantesData])
const estudiantesRef = db.collection("usuarios").doc(user.uid).collection("estudiantes")
useEffect(() => {
estudiantesRef.onSnapshot(snapshot => {
const tempData = [];
snapshot.forEach((doc) => {
const data = doc.data();
tempData.push(data);
});
setEstudiantes(tempData);
console.log(estudiantes)
})
}, []);
useEffect (() => {
const estData = window.localStorage.getItem("estudiantes");
setEstudiantes(JSON.parse(estData))
}, [])
useEffect (() => {
window.localStorage.setItem("estudiantes", JSON.stringify(estudiantes))
})
return (
<Container fixed>
<Box mb={5} pt={2} sx={{textAlign:'center'}}>
<Button
startIcon = {<PersonAddIcon />}
variant = "contained"
color = "primary"
size = "medium"
onClick={crearEstudiante} >
Crear Estudiantes
</Button>
<Box pl={25} pt={2} mb={2} sx={{height: '390px', width: "850px", textAlign:'center'}}>
<DataGrid
rows={estudiantes}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5]}
components={{
Toolbar: CustomToolbar,
}}
checkboxSelection
//Store Data from the row in another variable
onSelectionModelChange = {(id) => {
setSelectionModel(id);
const selectedIDs = new Set(id);
const selectedRowData = estudiantes.filter((row) =>
selectedIDs.has(row.id)
);
setEstudiantesData(selectedRowData)
}
}
{...estudiantes}
/>
</Box>
<Button
startIcon = {<ShoppingCartSharpIcon />}
variant = "contained"
color = "primary"
size = "medium"
onClick={realizarPedidos} >
Crear pedido
</Button>
</Box></Container>
)
}
export default ListadoEstudiantes
【问题讨论】:
-
从这个stackoverflow thread,我找到了一个代码框link,其中包含有关如何从 Material UI Datagrid Table 中删除特定行以及要删除行/数据的源代码也从 firebase firestore,在图标按钮的 onClick 上添加 deleteProduct 功能,例如 onClick ={deleteProduct} 并检查它是否有效。
-
从我所看到的情况来看,我在行中使用的删除方法几乎相同,但不是来自 firebase,我在该代码中没有看到任何用于 firebase 的更新方法。
-
是的,我只是分享它以供参考。我想我明白你需要什么。您正在从表中删除一行,但它没有在 firebase 中更新。你能看看这个stackoverflow thread吗?
-
我确实有删除按钮 + 从 firebase 更新的代码我只是不知道如何集成它,因为我添加更新代码时会出错。跨度>
-
您遇到的错误是什么?
标签: reactjs firebase google-cloud-firestore mui-datatable