列的总和

问题描述 投票:0回答:1

我正在使用React-Admin来创建用于证明工具以及谁借用工具的小应用程序。

我有具有ID,代码,名称,状态atd的工具表。

使用列表进行渲染。

    <List {...props} filters={<ToolFilter />}>
        <Datagrid rowClick="edit">
            <TextField source="id" />
            <TextField source="code" label="Kód" />
            <TextField source="name" />
            <TextField source="state" />
            <NumberField source="free" />
            <DateField source="add_time" />
            <EditButton />
        </Datagrid>
    </List>

比我有表B_tools,它包含借用工具的数据。它具有id,userId,toolId,durationOfBorrow

我想做的是将列添加到工具列表中,每个工具的SUMs durationOfBorrow都来自B_tool表,并将其呈现到工具列表中。

例如,如果我有B_tool表:

id   userId   toolId   durationOfBorrow
1    1        1        7
2    1        1        7
3    2        2        2
4    1        2        2

我需要这样的工具列表:


id   code     name     state   durationOfBorrow
1    123      Drill    1       14 (7+7)
2    456      Wrench   1       4 (2+2)

我尝试使用React-Admin文档中的Query the API with fetch。我已经准备了路线app.get('/ api / tools / borrowcount:id',toolController.borrowCount);应该返回列的总和:

borrowCount(req, res) {
        //console.log(req);  
        const filtry = JSON.parse(req.query.filter);
        console.log(filtry);



        const options = {
            attributes: [
                [sequelize.fn('sum', sequelize.col('time')), 'total_time'],
              ],
            raw: true,
            where: ({

            }),
            order: 
                []
            ,
        };


        if (typeof filtry.id !== "undefined") {
            options.where.id = filtry.id;
        }

        return B_tool
        .findAll(options)
            //console.log(user);
            //res.status(201).send(test); 
        .then(b_tool => {
            //console.log(user.rows);
            res.status(200).send(b_tool);
        })
        .catch(error => res.status(400).send(error));
    },

但是不知道如何实现它以将其显示在工具列表中。

react-admin
1个回答
0
投票

我明白了。

我创建了使用查询api / toolsborrow /:id并返回total_time的元素BorrowCount。

import React from 'react';
import { useQuery, Loading, Error } from 'react-admin';

const BorrowCount = ({ record }) => {

    const { data, loading, error } = useQuery({ 
        type: 'getOne',
        resource: 'toolsborrow',
        payload: { id: record.id }
    });

    if (loading) return <Loading />;
    if (error) return <Error />;
    if (!data) return null;
    console.log(data);
    return (
        data[0].total_time
    )
};

export default BorrowCount;

在API中,我称为新控制器:

app.get('/api/toolsborrow/:id', toolController.borrowCount);

并且控制器返回time列的SUM,其中ToolId是工具行的id

borrowCount(req, res) {
        const options = {
            attributes: [
                [Sequelize.fn('sum', Sequelize.col('time')), 'total_time'],
              ],
            raw: true,
            where: ({
                ToolId: req.params.id
            }),
            order: 
                []
            ,
        };


        return B_tool
        .findAll(options)
        .then(b_tool => {
            res.status(200).send(b_tool);
        })
        .catch(error => res.status(400).send(error));
    },

最后,我只使用工具列表中的新元素BorrowCount

    <List {...props} filters={<ToolFilter />}>
        <Datagrid rowClick="edit">
            <TextField source="id" />
            <TextField source="code" label="Kód" />
            <TextField source="name" />
            <TextField source="state" />
            <BorrowCount label="Total_sum"/>
            <NumberField source="free" />
            <DateField source="add_time" />
            <EditButton />
        </Datagrid>
    </List>

如果有人有更好的解决方案,请留在这里。

© www.soinside.com 2019 - 2024. All rights reserved.