【问题标题】:How to update editedRows from DataGrid MUI to firebase?如何将editedRows从DataGrid MUI更新到firebase?
【发布时间】:2021-10-18 17:48:07
【问题描述】:

如何将我刚刚编辑的值更新到 firebase? 到目前为止,这是我从 editRow 了解到的(如果我错了,请纠正我)。

DataGrid 内部

//Enable edit mode for row and not cell
editMode="row"

//any update you do in the row is stored in this variable so when you finish it overrides
//w/e you had there with editRowsModel value
editRowsModel={editRowsModel}

//Calls for the function that will be handeling the updates and the all the updates into "model"
onEditRowsModelChange={handleEditRowsModelChange}

//For Double Click + Control 
//(That way if uses double click by mistake it doesn't enable editMode)
onCellDoubleClick={(params, event) => {
        if (!event.ctrlKey) {
          event.defaultMuiPrevented = true;
        }
      }}

常量

//Variable that will hold the edited variable
const [editRowsModel, setEditRowsModel] = React.useState({});

//Edit function
const handleEditRowsModelChange = React.useCallback((model) => {
  setEditRowsModel(model);
}, []);

现在我想在编辑功能中编辑该行,但如何更新到 Firebase? 就像我有这个:

const [editRowsModel, setEditRowsModel] = React.useState({});
const handleEditRowsModelChange = React.useCallback((model) => {
  setEditRowsModel(model);
  console.log(model.uid)
  /*
   estudiantes.filter( x => {
     const temporalQuery = db.collection("usuarios").doc(user.uid).collection("estudiantes").doc(x.uid);
     temporalQuery.update({
       nombre: model.nombre,
       colegio: model.colegio,
       grado: model.grado
      })
    })
*/
}, []);

但是这不起作用,因为我看不到模型中的各个值,因为它只是说“未定义”,否则这将起作用。如何获得模型的各个值?

因为它是未定义的,所以当我尝试该代码时会出错。

欢迎任何帮助/提示。

模型如何记录

console.log(model)
console.log(model.name)
console.log(model[0].name)

【问题讨论】:

  • 所以您想检测用户何时完成编辑并在 firebase 上更新提交的数据,对吗?
  • 如果我只是 console.log 的“模型”,我可以随时看到值,模型显示整行数据,但是如果有意义的话,我知道如何单独获取这些数据?因为我不能只是简单地做 model[0].name 或类似的事情,就像我在它出现未定义之前所做的那样让我上传它是如何打印的
  • individually take this data,你是指用户输入或提交新行数据的时候?
  • 当我进入编辑模式时,模型会自动存储行数据,我想用这些数据来更新 firebase,但是当我尝试记录“数组的一部分”时它显示为未定义。 idk,如果我正确地解释我自己
  • 编辑模型不是数组,它是一个对象,键是行id,值是编辑后的行数据。

标签: reactjs firebase google-cloud-firestore datagrid material-ui


【解决方案1】:

我不确定我是否正确理解了您的问题,但如果您想访问编辑行数据,您可以这样做:

const [editRowsModel, setEditRowsModel] = React.useState({});
const [editRowData, setEditRowData] = React.useState({});

const handleEditRowsModelChange = React.useCallback(
  (model) => {
    const editedIds = Object.keys(model);

    // user stops editing when the edit model is empty
    if (editedIds.length === 0) {
      alert(JSON.stringify(editRowData, null, 4));
      // update on firebase
    } else {
      setEditRowData(model[editedIds[0]]);
    }

    setEditRowsModel(model);
  },
  [editRowData]
);

console.log(editRowData);

return (
  <div style={{ height: 300, width: "100%" }}>
    <DataGrid
      rows={rows}
      columns={columns}
      editMode="row"
      editRowsModel={editRowsModel}
      onEditRowsModelChange={handleEditRowsModelChange}
    />
  </div>
);

现场演示

【讨论】:

  • 是的,这应该可以,我只需要弄清楚如何获取嵌套文档的 UID,我会在发现后立即发布包含更新的答案
  • 最后我发现了 firebase 的一个问题,但我知道为什么它没有意义 rip
  • 啊,我发现了这个问题,所以我虽然 editRowData 正在正确解析数据,但我错了,它是 Object, Object。但是,当我尝试执行以下let tempUID = JSON.stringify(editRowData.uid) 时,它也出现了错误,它作为值出现:“呈现的 uid”有没有办法删除“单词”值,如果有意义的话,只需从值中取出数字吗?它实际上是这样出现的:{"value":"Gb4QwckjwnhTVfhCf2gw"}
  • 让它工作我所要做的就是将它称为editRowData.uid.value
猜你喜欢
  • 2021-11-20
  • 2021-12-06
  • 2021-12-13
  • 2019-01-23
  • 1970-01-01
  • 2022-10-25
  • 2022-12-15
  • 1970-01-01
  • 2016-09-19
相关资源
最近更新 更多