javascriptreactjscore-ui

How to show filters on certain columns for CoreUI Smart Table?


I am using CoreUI React Smart Table, I cannot seem to figure out how to put the filter on certain columns only.

I want it to look like this: Screenshot

The documentation is here, but it does not really seem to show how: https://coreui.io/react/docs/components/smart-table/

Any help would be really appreciated.

Here is my code:

import React, { useState } from 'react'
import { CCardBody, CButton, CSmartTable } from '@coreui/react-pro'
import data from './_data.js'

const SmartTableDownloadableExample = () => {
  const [currentItems, setCurrentItems] = useState(data)

  const csvContent = currentItems.map((item) => Object.values(item).join(',')).join('\n')

  const csvCode = 'data:text/csv;charset=utf-8,SEP=,%0A' + encodeURIComponent(csvContent)

  return (
    <CCardBody>
      <CButton
        color="primary"
        className="mb-2"
        href={csvCode}
        download="coreui-table-data.csv"
        target="_blank"
      >
        Download current items (.csv)
      </CButton>
      <CSmartTable
        columnFilter={{ column: 'id' }}
        columnSorter
        items={data}
        itemsPerPage={5}
        onFilteredItemsChange={setCurrentItems}
        pagination
      />
    </CCardBody>
  )
}


export default SmartTableDownloadableExample

Solution

  • I figured it out:

    You need to add an columns object:

    const columns = [
      {
        key: 'name',
        _style: { width: '40%' },
        _props: { color: 'primary', className: 'fw-semibold' },
      },
      'registered',
      { key: 'role', filter: false, sorter: false, _style: { width: '20%' } },
      { key: 'status', _style: { width: '20%' } },
      {
        key: 'show_details',
        label: '',
        _style: { width: '1%' },
        filter: false,
        sorter: false,
        _props: { color: 'primary', className: 'fw-semibold' },
      },
    ]
    

    Then in the CSmartTable:

      <CSmartTable
        **columns={columns}**
        columnFilter
        columnSorter
        items={data}
        itemsPerPage={5}
        onFilteredItemsChange={setCurrentItems}
        pagination
      />
    

    As per: https://coreui.io/react/docs/components/smart-table/