简体   繁体   English

React-Redux:将数量值添加到订单/购物车中的表格

[英]React-Redux: Add quantity value to a table in order/cart

I am trying to create a purchase order screen and I am facing issues while adding the quantity of the product.我正在尝试创建采购订单屏幕,但在添加产品数量时遇到了问题。

Workflow a)Fetch the product details from state and get in the drop down.工作流程 a) 从 state 获取产品详细信息并进入下拉菜单。 Select the product from drop down b)Add the quantity in the text field. Select 下拉列表中的产品 b) 在文本字段中添加数量。 Click on Add c)This will add the product details to a table.单击添加 c) 这会将产品详细信息添加到表格中。

But I am not sure how to set a constant quantity for each product selected.但我不确定如何为所选的每种产品设置一个恒定数量。

屏幕供参考

Now when I am not sure how to add the quantity to the product selected.现在,当我不确定如何将数量添加到所选产品时。 Sorry, the code might be messed up, I am still learning.对不起,代码可能搞砸了,我还在学习。 Adding the code below.添加下面的代码。

let count=0;
    const [validated, setValidated] = useState(false);
    const dispatch = useDispatch()
    const [medicineName, setMedicineName] = useState('')
    const [quantity, setQuantity] = useState(0)
    const [tableData, setTableData] = useState([])

    const productList = useSelector( state => state.productList )
    const { loading, error, products } = productList
    
    const userLogin = useSelector(state => state.userLogin)
    const {userInfo} = userLogin

    const [dropDownData, setDropDownData] = useState(products)

    useEffect(()=>{
           setDropDownData(products)
        },[products])

    useEffect(() => {

        if(!userInfo){
            history.push('/login')
        }
        
        dispatch(listProducts())

    },[dispatch, history, userInfo])

    const submitHandler = (e) => {
        e.preventDefault()
        const arr = dropDownData.filter((product) => 
            product.medicineName.toLowerCase().indexOf(medicineName.toLowerCase()) > -1)
        
        setTableData(tableData => tableData.concat(arr))
        const arr2 = dropDownData.filter((product) => 
            product.medicineName.toLowerCase().indexOf(medicineName.toLowerCase()))
        
        setDropDownData(arr2)
        
    }

return(
        <>
        <h2>PurchaseOrderScreen</h2>
        
        <Form onSubmit={submitHandler} validated={validated} noValidate>
        <Row>
            <Col md={7}>
                <Form.Group controlId='medicineName'>
                    <FloatingLabel controlId="floatingSelect" label="Medicine">
                        <Form.Control as='select' value={medicineName} className="mb-3"
                                onChange={(e) => setMedicineName(e.target.value)}
                                required
                                >
                                <option value=''>Select Medicine</option>
                                {dropDownData.map(product => (
                                    <option value={product.medicineName}>{product.medicineName}</option>
                                ))  }
                            </Form.Control>
                        </FloatingLabel>
                        
                </Form.Group>
            </Col>
            <Col md={3}>
                <Form.Group className="mb-3" controlId='quantity'>
                    <FloatingLabel controlId="floatingInput" label="Quantity" >
                        <Form.Control   type="text"  placeholder="Quantity"
                                        value={quantity}
                                        onChange = {(e)=> setQuantity(e.target.value)}
                                        required 
                                    />
                    </FloatingLabel>
                    </Form.Group>
            </Col>
            <Col md={2}>
                <Button type='submit' variant='primary'>
                    >Add
                </Button>
            </Col>
        </Row>
        </Form>

        <Table striped bordered hover responsive='md' className='table-sm mt-3' id="table-to-xls">
                        <thead>
                            <tr>
                                <th><span className='btn'>Remove</span></th>
                                <th ><span className='btn'>Sl</span></th>
                                <th ><span className='btn'>Medicine</span></th>
                                <th ><span className='btn'>C.stock</span></th>
                                <th ><span className='btn'>Quantity</span></th>
                                <th ><span className='btn'>Low Stock</span></th>
                                <th ><span className='btn'>Reorder Quantity</span></th>
                            </tr>
                        </thead>
                        <tbody>
                            {tableData.map(product => (
                                    <tr key={product._id} >
                                        <td> X </td>
                                        <td>{count+1}</td>
                                        <td>{product.medicineName}</td>
                                        <td>{product.currentStock}</td>
                                        <td>{quantity}</td>
                                        <td>{product.lowStockValue}</td>
                                        <td>{product.reOrderValue}</td>
                                    </tr>
                                )) }
                        </tbody>                        
                </Table>

Can you please let me know how the quantity can be added.你能告诉我如何添加数量吗? Please let me know if you need any details.如果您需要任何详细信息,请告诉我。

In order to add the quantity to the table, you need to store it somewhere.为了将数量添加到表中,您需要将其存储在某个地方。 You have an array called tableData that you currently are adding your products to.您有一个名为tableData的数组,您当前正在向其中添加产品。 Maybe instead of adding the products, you could add an object:也许您可以添加 object 而不是添加产品:

// Inside the submitHandler function
const productToAdd = dropDownData.find((product) =>
   product.medicineName.toLowerCase().indexOf(medicineName.toLowerCase()) > -1);
const rowToAdd = {product: productToAdd, quantity: quantity};
setTableData(tableData => [...tableData, rowToAdd])

and then later in your render:然后在你的渲染中:

{tableData.map(row => (
  <tr key={row.product._id} >
    <td> X </td>
    <td>{count+1}</td>
    <td>{row.product.medicineName}</td>
    <td>{row.product.currentStock}</td>
    <td>{row.quantity}</td>
    <td>{row.product.lowStockValue}</td>
    <td>{row.product.reOrderValue}</td>
  </tr>
)) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM