2

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.

5
  • Is there any initialstate? Commented Aug 9, 2021 at 9:41
  • Hmm, yes. const [list, setList] = useState([]) Commented Aug 9, 2021 at 9:43
  • 1
    I read the doc for your question and don't find right thing. Commented Aug 9, 2021 at 9:53
  • Hmm, yeah bro :| Commented Aug 9, 2021 at 9:58
  • Is CoreUI use github.com/coreui/coreui-datatables for DataTables? Commented Aug 9, 2021 at 10:02

2 Answers 2

4

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
/>
Sign up to request clarification or add additional context in comments.

1 Comment

They have a very poor documentation!
0

Here is how it works for me:

<CDataTable
  items={list}
  fields={fields}
  columnFilter
  tableFilter={{ 'placeholder': 'Type something...'}}
  itemsPerPageSelect
  itemsPerPage={5}
  hover
  sorter
  sorterValue={{column: "title", asc: true}}
  onSorterValueChange={(params) => {
    if (params.column === order.sorting.column && params.asc === order.sorting.asc) return;
    dispatch(actions.getOrders(.....));
    dispatch(actions.sortingChanged(params));
  }}
  pagination
/>

You should avoid cyclic code call by making sure in onSorterValueChange() that params.column & params.asc are not equal to what is already set. If true - return; otherwise - call your API request and change state with new sorting params.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.