Sorting
Pantanal Grid supports single and multiple column sorting. Click column headers to toggle sorting.
Basic Single Column Sorting
Click column headers to sort. Click again to reverse, click a third time to remove sort.
Multiple Column Sorting
Enable multiple column sorting. Click multiple column headers to sort by multiple columns. Sort indexes are shown.
Column-Level Sortable
Control sorting per column. Only columns with sortable: true can be sorted.
Single Column Sorting
Click column headers to sort. Try multiple column sorting by holding Shift and clicking.
By default, clicking a column header cycles through: none → ascending → descending → none.
<script setup lang="ts">
import { ref } from 'vue'
import { PantanalGrid, type ColumnDef, type SortDescriptor } from '@pantanal/grid'
const rows = ref([/* ... */])
const columns: ColumnDef[] = [
{ field: 'name', title: 'Name', sortable: true },
{ field: 'price', title: 'Price', sortable: true }
]
const sort = ref<SortDescriptor[]>([])
</script>
<template>
<PantanalGrid
:rows="rows"
:columns="columns"
key-field="id"
v-model:sort="sort"
/>
</template>Multiple Column Sorting
Enable multiple column sorting by setting sortableMode to 'multiple':
<script setup lang="ts">
import { ref } from 'vue'
import { PantanalGrid, type ColumnDef, type SortDescriptor } from '@pantanal/grid'
const rows = ref([/* ... */])
const columns: ColumnDef[] = [
{ field: 'name', title: 'Name', sortable: true },
{ field: 'price', title: 'Price', sortable: true },
{ field: 'category', title: 'Category', sortable: true }
]
const sort = ref<SortDescriptor[]>([])
</script>
<template>
<PantanalGrid
:rows="rows"
:columns="columns"
key-field="id"
v-model:sort="sort"
sortable-mode="multiple"
:sortable-show-indexes="true"
/>
</template>Custom Sort Function
You can provide a custom sort function for a column:
<script setup lang="ts">
const columns: ColumnDef[] = [
{
field: 'name',
title: 'Name',
sortable: true,
sortableCompare: (a, b, descending) => {
// Custom comparison logic
const result = a.localeCompare(b)
return descending ? -result : result
}
}
]
</script>Initial Sort
Set initial sort order:
<script setup lang="ts">
const sort = ref<SortDescriptor[]>([
{ field: 'price', dir: 'desc' },
{ field: 'name', dir: 'asc' }
])
</script>Sort Events
Listen to sort changes:
<template>
<PantanalGrid
:rows="rows"
:columns="columns"
key-field="id"
@update:sort="handleSortChange"
@sort="handleSort"
/>
</template>
<script setup lang="ts">
function handleSortChange(newSort: SortDescriptor[]) {
console.log('Sort changed:', newSort)
}
function handleSort(event: { sort: SortDescriptor[] }) {
console.log('Sort event:', event.sort)
}
</script>Sort Descriptor
The sort descriptor has the following structure:
interface SortDescriptor {
field: string // Column field name
dir: 'asc' | 'desc' // Sort direction
}Disabling Sort
Disable sorting for specific columns:
const columns: ColumnDef[] = [
{ field: 'id', title: 'ID', sortable: false },
{ field: 'name', title: 'Name', sortable: true }
]Or disable globally:
<PantanalGrid
:rows="rows"
:columns="columns"
key-field="id"
:sortable="false"
/>Custom Sort Function
Provide a custom comparison function:
<script setup lang="ts">
const columns: ColumnDef[] = [
{
field: 'name',
title: 'Name',
sortable: true,
sortableCompare: (a, b, descending) => {
// Custom comparison logic
const result = a.localeCompare(b, undefined, { sensitivity: 'base' })
return descending ? -result : result
}
}
]
</script>Server-Side Sorting
For server-side sorting, send sort descriptors to your API:
<script setup lang="ts">
watchEffect(async () => {
const params = new URLSearchParams({
page: String(page.value),
pageSize: String(pageSize.value)
})
if (sort.value.length > 0) {
params.append('sortBy', sort.value[0].field)
params.append('order', sort.value[0].dir)
}
const response = await fetch(`/api/data?${params}`)
// ...
})
</script>See Also
- Sorting Guide - Complete sorting documentation
- Server-Side Guide - Server-side sorting