【问题标题】:Add custom column to Datagrid in admin-on-rest在 admin-on-rest 中将自定义列添加到 Datagrid
【发布时间】:2018-08-04 19:40:02
【问题描述】:

我在 React.js 中使用 admin-on-rest 创建了一个管理面板。我有一个页面显示从 api 提取数据的用户列表。我需要在该表的每一行末尾添加一个自定义列,该列将为每个用户显示一个进度条。

我正在使用一个名为rc-progress 的模块来生成进度条。这是当前在用户列表中的单独页面上显示的示例:

import React from 'react';
import { Line } from 'rc-progress';

var Progress = React.createClass({
render: function render() {

    return (
       <Line percent="40" strokeWidth="1" strokeColor="#38bcd5" />
    );
}
});

export default Progress;

我不确定如何在 admin-on-rest 中将自定义列附加到 Datagrid 以添加到该进度条中。

这是我为用户列表正确显示来自 API 的数据的代码。我添加了一条评论,我想在其中添加进度条:

import React from 'react';
import { List, Datagrid, TextField} from 'admin-on-rest';
import { Line } from 'rc-progress';

export const UserList = (props) => (
<List {...props}>
    <Datagrid>
        <TextField source="firstName" />
        <TextField source="lastName" />
        <TextField source="email" />
        <TextField source="phone" />
        <TextField source="type" />

        //I'd like to add the progress bar below:
        //<Line percent="40" strokeWidth="1" strokeColor="#38bcd5" />

    </Datagrid>
</List>

);

导出默认用户列表;

非常感谢任何帮助!

更新(解决方案)

所以根据 Gildas 的建议,我尝试使用 FunctionField。这是代码的工作版本。 (进度条现在有硬编码值)

import React from 'react';
import { List, Datagrid, TextField} from 'admin-on-rest';
import { Line } from 'rc-progress';
import { FunctionField } from 'admin-on-rest'

export const UserList = (props) => (
<List {...props}>
    <Datagrid>
        <TextField source="firstName" />
        <TextField source="lastName" />
        <TextField source="email" />
        <TextField source="phone" />
        <TextField source="type" />

        //Here is the working version below:
        <FunctionField label="Progress" render= {function render() { return ( <Line percent="40" strokeWidth="1" strokeColor="#38bcd5" />);}} />

    </Datagrid>
</List>

);


export default UserList;

这是当前的屏幕截图:

【问题讨论】:

    标签: reactjs datagrid progress-bar admin-on-rest


    【解决方案1】:

    FunctionField 应该可以解决问题:https://marmelab.com/admin-on-rest/Fields.html#functionfield

    如果没有,您是否尝试过遵循自定义字段文档?

    【讨论】:

    • 谢谢吉尔达斯。我已经用我为 FunctionField 尝试的内容更新了这个问题。我不确定我在渲染道具上的语法是否正确,或者我什至可以尝试在该渲染函数中使用来自 rc-progress 的代码。让我知道是否有任何想法。谢谢!
    • 您知道material-ui.com/#/components/linear-progress 吗?这将为您节省额外的依赖:)
    • 感谢您发布此更新,这将对其他人有所帮助!
    • 我没有看过线性进度,虽然它看起来像一个基于页面/组件加载的动画进度条。我不确定如何覆盖默认行为以显示静态百分比。
    • &lt;LinearProgress variant="determinate" value={myValue} /&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多