我在从已在页面渲染器上输出的组件中的渲染数据获取值时面临问题。我需要做的是,当有人在文本字段中输入数据时,它应该将其发送到数据库,但要从运行时数据中获取该字段数据。

目前,当我键入某些内容时,它会显示未定义的字段等。
这不是表单数据,而是需要从文本字段更新的数据。
因此,如果用户在文本字段中写一些xyz,我们需要根据与该字段关联的ID更新该数据。

我无法将数据输入:console.log(Id,projectId,userId,日期,e.target.value)
我使用了达到目的的reduce方法,但现在有了另一个用例。

我不想将隐藏字段设置为不正确的方法。

问题是,当有人在文本字段中键入数据时,我需要从中获取该文本字段数据以及关联的ID和相应的数据,并将其传递给ajax调用。

我需要使用ajax发送该数据,但是一旦输入内容,它就会显示未定义。我可以轻松地从项目数组中获取数据,但对我来说毫无用处。我认为数组的减少方法不适用于我的用例。

这是项目数组:

data = [
  {
    "id": 27,
    "projectno": "007823",
    "projectname": "non-A Project 2",
    "dailyproof": 1,
    "probability": "1.0",
    "toleranceregistering": 2,
    "customer_name": "Peter",
    "user_id": "4",
    "days_allocated": "231.0",
    "days_real": "5.0",
    "hours_real": "6.0",
    "project_times": [
      {
        "id": 11,
        "activity": "\"yht\"",
        "date": "2020-04-28",
        "hours": "2.0",
        "project_id": 27,
        "token": "\"trr\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:36:55.479Z",
        "updated_at": "2020-04-22T12:36:55.479Z"
      },
      {
        "id": 12,
        "activity": "\"yht\"",
        "date": "2020-04-03",
        "hours": "2.0",
        "project_id": 27,
        "token": "\"trr\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:37:08.763Z",
        "updated_at": "2020-04-22T12:37:08.763Z"
      },
      {
        "id": 13,
        "activity": "\"yht\"",
        "date": "2020-04-14",
        "hours": "2.0",
        "project_id": 27,
        "token": "\"dfg\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:37:19.177Z",
        "updated_at": "2020-04-22T12:37:19.177Z"
      }
    ]
  },
  {
    "id": 28,
    "projectno": "007333",
    "projectname": "non-A Project 2",
    "dailyproof": 0,
    "probability": "1.0",
    "toleranceregistering": 2,
    "customer_name": "Peter",
    "user_id": "4",
    "days_allocated": "231.0",
    "days_real": "3.333333333333333333333333334",
    "hours_real": "4.0",
    "project_times": [
      {
        "id": 18,
        "activity": "\"tgr\"",
        "date": "2020-04-16",
        "hours": "2.0",
        "project_id": 28,
        "token": "\"ujy\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:39:41.465Z",
        "updated_at": "2020-04-22T12:39:41.465Z"
      },
      {
        "id": 19,
        "activity": "\"ddd\"",
        "date": "2020-04-11",
        "hours": "2.0",
        "project_id": 28,
        "token": "\"fff\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:39:55.020Z",
        "updated_at": "2020-04-22T12:39:55.020Z"
      }
    ]
  },
  {
    "id": 29,
    "projectno": "00721",
    "projectname": "non-A Project 2",
    "dailyproof": 1,
    "probability": "1.0",
    "toleranceregistering": 2,
    "customer_name": "Peter",
    "user_id": "4",
    "days_allocated": "231.0",
    "days_real": "5.0",
    "hours_real": "6.0",
    "project_times": [
      {
        "id": 22,
        "activity": "\"cdf\"",
        "date": "2020-04-11",
        "hours": "2.0",
        "project_id": 29,
        "token": "\"fgff\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:41:26.392Z",
        "updated_at": "2020-04-22T12:41:26.392Z"
      },
      {
        "id": 23,
        "activity": "\"tg\"",
        "date": "2020-04-15",
        "hours": "2.0",
        "project_id": 29,
        "token": "\"ad\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:41:38.747Z",
        "updated_at": "2020-04-22T12:41:38.747Z"
      },
      {
        "id": 24,
        "activity": "\"ff\"",
        "date": "2020-04-19",
        "hours": "2.0",
        "project_id": 29,
        "token": "\"bbb\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:41:47.500Z",
        "updated_at": "2020-04-22T12:41:47.500Z"
      }
    ]
  },
  {
    "id": 30,
    "projectno": "0074",
    "projectname": "non-A Project 2",
    "dailyproof": 1,
    "probability": "1.0",
    "toleranceregistering": 2,
    "customer_name": "Peter",
    "user_id": "4",
    "days_allocated": "231.0",
    "days_real": "3.333333333333333333333333334",
    "hours_real": "4.0",
    "project_times": [
      {
        "id": 25,
        "activity": "\"ff\"",
        "date": "2020-04-12",
        "hours": "2.0",
        "project_id": 30,
        "token": "\"bbb\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:42:09.385Z",
        "updated_at": "2020-04-22T12:42:09.385Z"
      },
      {
        "id": 26,
        "activity": "\"rter\"",
        "date": "2020-04-19",
        "hours": "2.0",
        "project_id": 30,
        "token": "\"gfdg\"",
        "user_id": 4,
        "created_at": "2020-04-22T12:42:19.861Z",
        "updated_at": "2020-04-22T12:42:19.861Z"
      }
    ]
  }
]

getDaysNumber('2020', '04') {
        const dayNums = [];
        const daysInMonth = new Date(year, month, 0).getDate();
        for (let i = 1; i <= daysInMonth; i++) {
            dayNums.push(i);
            // console.log(i, ' xxx ');
        }
        return dayNums;
    }

{


data.map((h, index) => (
                                    <TableRow key={`mi-${index}`}>
                                        <TableCell align="right">{h.projectno}</TableCell>
                                        <TableCell align="right">{h.projectname}</TableCell>
                                        <TableCell align="right">{h.customer_name}</TableCell>
                                        <TableCell align="right">{h.days_allocated}</TableCell>
                                        <TableCell align="right">{h.days_real}</TableCell>
                                        <TableCell align="right">{h.hours_real}</TableCell>
                                        {daysNumber.reduce((acc, number, index) => {
                                            const found = h.project_times.find(item => number == item["date"].split('-')[2].replace(/^0+/, ''))
                                            const Id = found && found["id"];
                                            const projectId = found && found["project_id"];
                                            const userId = found && found["user_id"];
                                            const date = found && found["date"];
                                            const hours = found && found["hours"];
                                            found && console.log(Id, projectId, userId, date);
                                            return [...acc,
                                                h.dailyproof == 1 && hours > 0.0 ?
                                                    <TableCell align="right" key={`mi-${index}`}
                                                               onClick={this.launchCreateContactDialog}>{hours}</TableCell>
                                                    :
                                                    <TableCell align="right" key={`mi-${index}`}>
                                                        <TextField required fullWidth size="small"
                                                                   variant="outlined"
                                                                   onKeyUp={(e) => console.log(Id, projectId, userId, date, e.target.value)}/>
                                                    </TableCell>
                                            ]
                                        }, [])
                                        }
                                    </TableRow>
                                ))
                            }

最佳答案

find调用有时可能返回undefined

const found = h.project_times.find(item => number == item["date"]
  .split('-')[2]
  .replace(/^0+/, '')
)


找不到匹配项时,这是预期的。而且由于未定义,所有这些都将以undefined结尾:

const Id = found && found["id"];
const projectId = found && found["project_id"];
const userId = found && found["user_id"];
const date = found && found["date"];
const hours = found && found["hours"];


因此,console.log语句注销值undefined并不罕见。

听起来您需要做一些事情:


将此数据保留为组件中的状态。
添加一个函数来改变这种状态。
添加一个存储状态的函数(调用API)


我没有足够的上下文来为您回答#3,但是这是您想要使用的模式类型:

import { useEffect, useState } from 'react';

function HoursEntry() {
  const [state, setState] = useState();

  useEffect(() => {
    // Do your data fetching here; for now will use your constant
    setState(data);
  }, []);

  function updateHours({ userId, projectId, hourEntryId, date, hours }) {
    // Build newData based on the changes...
    setState(newData);
  }

  // All the rendering stuff. Rendered components should be mappings of what's in
  // state...
    <TextField
      required fullWidth
      size="small"
      variant="outlined"
      value={hours}
      onChange={(e) => updateHours({
        userId,
        projectId,
        hourEntryId: Id,
        date,
        hours: parseFloat(e.target.value)
      })}/>
  // ...
}


updateHours函数内部,将使用预期的修改来创建数据的新副本。例如,如果要更新现有对象,则将更新其hours属性。如果要更新没有记录的内容,则将创建一个新记录,以此类推。关键是调用setState以更新组件中的数据。

然后,一旦有了提交按钮或必须存储的任何方法,就将引用此状态作为最新数据副本。

这是任何形式的表单输入组件的通用模式;这是更新状态的练习。

09-25 17:59
查看更多