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

143
Visualizações
Custom sort method for react table header

I need to sort a react-table based on different values depending on the column header that is clicked. So far this is my code:

{
    sortMethod: (a: ListItem, b: ListItem) => {
      console.log(a, b);
      return 1;
    },
    Header: (rest) => {
      console.log(rest);
      return (
        <Flex height={16} width="100%" justifyContent="center">
          <Text onClick={() => rest.column.sortMethod()} color="black">Name</Text>
        </Flex>
      );
    },
    accessor: 'product.name',
    minWidth: 280,
  },

This being my table:

<Table
      key={`table.${products.length}.${selectedKeys.join('.')}`}
      isSelectable
      showPagination={showPagination}
      defaultPageSize={defaultPageSize}
      pageSizeOptions={pageSizes}
      onSelectionChange={(newSelectedProducts: ListItem[]) => {
        if (tableProductsWithTags.length > 0) {
          setSelectedProducts(newSelectedProducts.filter(Boolean));
        }
      }}
      noDataText={isLoading ? 'Loading ...' : 'No products'}
      columns={projectColumns}
      selectedKeys={selectedKeys}
      keyField="id"
      defaultSorted={sortingOptions}
      data={tableProductsWithTags}
      getTrProps={() => {
        return { style: { alignItems: 'start' }, passthrought: { onClick: setFilterOption } };
      }}
      getTdProps={
        ((_state, _rowInfo, column: { id: string }) => {
          const tdProps: { [key: string]: any } = {
            style: { minHeight: 55 }
          };
          if (column.id === 'tags') {
            tdProps.passthrough = { onEdit: handleEditProduct };
          }
          return tdProps;
        }) as ComponentPropsGetterRC
      }
      {...props}
    />

Passing a sort into the sortMethod prop seems like the correct way but how do I actually implement this? In the table you can see I pass in an onEdit method that handles when a cell is edited. I tried this way for a header as well but it seems like it does not have access to the same props.

about 4 years ago · Juan Pablo Isaza
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