javascriptreactjsdatatablecore-ui

CoreUI DataTable sort values by specific column values


Right now, it's not showing sorted result according to title. I want to show the rows by default in ascending.

Please have a look:

enter image description here

Below is my code:

const fields = [
  { key: 'title', _classes: 'font-weight-bold' },
  { key: 'category', _style: { width: '20%'} }
]
<CDataTable
    items={list}
    fields={fields}
    columnFilter
    tableFilter={{ 'placeholder': 'Type something...'}}
    itemsPerPageSelect
    itemsPerPage={5}
    hover
    sorter
    pagination
/>

Note: From the Documentation: I have tried something i.e. sorter={{'resetable': 'ascending'}} but nothing works for me.


Solution

  • There's two points:

    1. For descending sort you must set asc:false
    2. Default sort value must pass to sorterValue option in CDataTable

    So , you can try this:

    <CDataTable
        items={list}
        fields={fields}
        columnFilter
        tableFilter={{ 'placeholder': 'Type something...'}}
        itemsPerPageSelect
        itemsPerPage={5}
        hover
        sorter
        sorterValue={{column: "title", asc: true}}
        pagination
    />