getResultSortedRowIds
The getResultSortedRowIds
method returns the Ids
of every ResultRow
in the ResultTable
of the given query, sorted according to the values in a specified ResultCell
.
getResultSortedRowIds(
queryId: string,
cellId?: string,
descending?: boolean,
offset?: number,
limit?: number,
): Ids
Type | Description | |
---|---|---|
queryId | string | The |
cellId? | string | The |
descending? | boolean | Whether the sorting should be in descending order. |
offset? | number | The number of |
limit? | number | The maximum number of |
returns | Ids | An array of the sorted |
This has the same behavior as a Store
's getSortedRowIds
method. For example, if the query Id
is invalid, the method returns an empty array. Similarly, the sorting of the rows is alphanumeric, and you can indicate whether it should be in descending order. The offset
and limit
parameters are used to paginate results, but default to 0
and undefined
to return all available ResultRow
Ids
if not specified.
Note that every call to this method will perform the sorting afresh - there is no caching of the results - and so you are advised to memoize the results yourself, especially when the ResultTable
is large. For a performant approach to tracking the sorted ResultRow
Ids
when they change, use the addResultSortedRowIdsListener
method.
Example
This example creates a Queries
object, a single query definition, and then calls this method on it (as well as a non-existent definition) to get the ResultRow
Ids
.
import {createQueries, createStore} from 'tinybase';
const store = createStore().setTable('pets', {
fido: {species: 'dog', color: 'brown'},
felix: {species: 'cat', color: 'black'},
cujo: {species: 'dog', color: 'black'},
});
const queries = createQueries(store).setQueryDefinition(
'dogColors',
'pets',
({select, where}) => {
select('color');
where('species', 'dog');
},
);
console.log(queries.getResultSortedRowIds('dogColors', 'color'));
// -> ['cujo', 'fido']
console.log(queries.getResultSortedRowIds('catColors', 'color'));
// -> []
Since
v2.0.0