Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
number formatting in antD tables, or localeCompare() number strings with commas

I have values I want to display in an AntD table. I want them to be sortable, but also formatted with commas, like "1,000". Ideally I would store them in React state as integers, and then format them as strings with commas upon display, but I haven't found a way in AntD tables to do this conversion last minute. Here's an example column JSX element displaying unformatted but sortable integers:

            <Column
              title="Population"
              dataIndex="population"
              key="population"
              sorter={(a, b) => a.population - b.population}
            />

I have tried converting all my state to formatted strings, and then the table displays them nicely, but the sorting is messed up. I thought localeCompare with numeric = True would fix this, but it doesn't.

With this input:

      let arr = ['999', '1,000', '1,001'];
      console.log(
        arr.sort((a, b) =>
          a.localeCompare(b, undefined, {
            numeric: true,
            ignorePunctuation: true,
          })
        )
      );

I expect the input to be sorted numerically, but it isn't. Perhaps I misunderstand how localeCompare() works. This is the result:

[ "1,000", "1,001", "999" ]

So I have two questions, and answering either one would fix my problem:

  1. Can I have an AntD table with columns that formats integers into a readable format on display, and if so, how?
  2. If not, how can I simply sort string representations of integers with commas (and $ amounts for that matter)? I could go regex here of course but I'm hoping there's a simpler solution I've missed. Changing integers into strings and then back to integers to sort them seems very silly.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think the best approach in your case would be to use the render property of the Column.

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { Table } from "antd";

const { Column } = Table;

const data = [
  {
    key: "1",
    name: "John Brown",
    salary: 1000
  },
  {
    key: "2",
    name: "Jim Green",
    salary: 2000
  },
  {
    key: "3",
    name: "Joe Black",
    salary: 999
  },
  {
    key: "4",
    name: "Jim Red",
    salary: 1001
  }
];

ReactDOM.render(
  <Table dataSource={data}>
    <Column title="Name" dataIndex="name" key="name" />
    <Column
      title="Salary"
      dataIndex="salary"
      key="salary"
      sorter={(a, b) => a.salary - b.salary}
      render={(value) => {
        return "$" + value.toLocaleString("en");
      }}
    />
  </Table>,
  document.getElementById("container")
);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda