Datagrid – 在Material UI中使用Reactjs将总数100003格式化为1M+?

huangapple go评论68阅读模式
英文:

Datagrid - Format total count 100003 to 1M+ in Material UI with Reactjs?

问题

我正在使用ReactJS在Mui中实现DataGrid。我有100万条数据。
目前,我总共显示了100,000条。

是否有显示总数为1M+、1000+或其他缩写方式来展示大数字的方法?

请查看附加的图片以供参考。

Datagrid – 在Material UI中使用Reactjs将总数100003格式化为1M+?

提前感谢您。

英文:

I'm implementing Datagrid in Mui with reactjs. I have 1M data.
So I'm currently showing 100000 in total count.

Is there any way to show total count as 1M+ or 1000+ or any other shorthand ways to display a large number?

Please find the attached image for your reference.

Datagrid – 在Material UI中使用Reactjs将总数100003格式化为1M+?

Thanks in advance.

答案1

得分: 1

希望这能给你一个清晰的理解。
尝试像这样传递值以表示百万:

value >= 1000000 && Math.abs(Number(在此处输入您的值....)) / 1.0e6).toFixed(1) + " M+"
英文:

Hope this gives you a clarity.
Try passing the value like this for millions

value >= 1000000 && Math.abs(Number(your value here....)) / 1.0e6).toFixed(1) + " M+"

答案2

得分: 0

const formatTotalCount = (params) => {
  const totalCount = params.value;
  if (totalCount >= 1000000) {
    return `${(totalCount / 1000000).toFixed(1)}M+`;
  } else if (totalCount >= 1000) {
    return `${(totalCount / 1000).toFixed(1)}K+`;
  } else {
    return totalCount.toString();
  }
};

const columns = [
  { field: 'totalCount', headerName: '总计', width: 150, renderCell: formatTotalCount },
];

const rows = [
  // 这里放置您的数据行
];

const MyDataGrid = () => {
  return (
    <div style={{ height: 400, width: '100%' }}>
      <DataGrid rows={rows} columns={columns} pageSize={5} />
    </div>
  );
};

export default MyDataGrid;
英文:

You can do something like this by displaying 1k or 1m accordingly if value it out of range.

const formatTotalCount = (params) =&gt; {
  const totalCount = params.value;
  if (totalCount &gt;= 1000000) {
    return `${(totalCount / 1000000).toFixed(1)}M+`;
  } else if (totalCount &gt;= 1000) {
    return `${(totalCount / 1000).toFixed(1)}K+`;
  } else {
    return totalCount.toString();
  }
};

const columns = [
  { field: &#39;totalCount&#39;, headerName: &#39;Total Count&#39;, width: 150, renderCell: formatTotalCount },
];

const rows = [
  // Your data rows here
];

const MyDataGrid = () =&gt; {
  return (
    &lt;div style={{ height: 400, width: &#39;100%&#39; }}&gt;
      &lt;DataGrid rows={rows} columns={columns} pageSize={5} /&gt;
    &lt;/div&gt;
  );
};

export default MyDataGrid;

huangapple
  • 本文由 发表于 2023年7月24日 15:56:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定