Comments (9)
@vincjo, thanks for your reply.
We have a use case where we could have a nested object like this:
const data = [
{
name: 'Mario',
status: 'DRAFT',
ammount: 41,
job: {
id: 5,
name: 'Developer'
}
},
{
name: 'George',
status: 'OPEN',
ammount: 54,
job: {
id: 6,
name: 'Sales'
}
},
{
name: 'Mario',
status: 'OPEN',
ammount: 31,
job: {
id: 7,
name: 'Engineer'
}
}
];
And this is a model that can have more or fewer key properties; it's dynamic, that's why can't have a custom filter for each of them. So we are using the callback like this:
(row) => getPropertyValue(row, key)
where key could be 'jobs.id', 'name', 'jobs.name', etc...
from datatables.
@vincjo, I was able to solve the problem with the following code, creating a dynamic function to ensure that when converted to a string it's always unique:
function createNamedFunction(key) {
return new Function('row', `
function getValueByPath(obj, path) {
return path.split('.').reduce((currentObject, key) => {
return currentObject ? currentObject[key] : undefined;
}, obj);
}
return getValueByPath(row, '${key}');`);
}
handler.filter(value, createNamedFunction(key), check.contains);
from datatables.
Oh, ok. Nice
I also added a workaround in version 1.14.3
(this library needs a better way to name filters).
You can choose what best suits your needs.
In ThFilter.svelte
<script lang="ts">
import type { DataHandler } from '$lib';
export let handler: DataHandler;
export let key: any = '';
let value = '';
const filter = handler.createFilter((row) => row[key])
</script>
<th>
<input
type="text"
placeholder="Filter"
bind:value
on:input={() => filter.set(value)}
/>
</th>
Instead of handler.filter()
shortcut, you declare a new filter instance, by using handler.createFilter()
.
This instance creates a random string as a unique identifier.
Thanks for raising this point
from datatables.
Cool. Thank you so much for your help. I'll close this.
from datatables.
@vincjo, sorry for open it again. Just want to ask if is it the same case with the sort function?
from datatables.
Indeed, didn't think about it.
I added a workaround for column sorting, where you can pass an identifier to the sort method.
Released in 1.14.4
.
Examples
Using handler
instance:
handler.sort(orderBy, identifier)
Using <Th>
component:
<Th orderBy={(row) => row[key]} identifier="th0"/>
Using your code example:
<table>
<thead>
<tr>
{#each keys as key, i}
<Th {handler} orderBy={key} identifier={'th' + i}>{key}</Th>
{/each}
</tr>
[...]
from datatables.
In ThFilter.svelte
, since the key is the variable, you should pass it directly as the filter param rather than using a callback:
handler.filter(value, key)
instead of:
handler.filter(value, (row) => row[key])
Then, as you mentioned, the parseField()
function will determine that "key" is of type "string", and will create the callback.
The identifier will contain the actual value of "key" instead of "row[key]"
from datatables.
Okay, it looks nice. Thank you. Is there a way to have multiple sorts?
For example:
Sort by id asc
Then by amount desc
Then by date asc
To solve this, I just have to create a custom sort function and then reset the rows with handler.setRows(sortedRows)
from datatables.
There is a way:
handler.sortAsc('id')
handler.sortDesc('amount')
handler.sortAsc('date')
(You can add an identifier as a second parameter)
When you update rows using setRows()
, last 3 sorts are played automatically.
from datatables.
Related Issues (20)
- Get Filtered Rows HOT 1
- Sorting with Nested Objects HOT 2
- Pagination Next is disabled when there is only 1 page, but clickable when there is no data HOT 2
- Radio button group issue HOT 7
- Suggestion: Add an API function to the get all rows HOT 2
- Mismatching tags in `Optional blocks` example page HOT 1
- [Bug] change event not triggering after using setRows HOT 1
- Inverse Filter HOT 4
- Components style HOT 15
- Will there be support for Vue? HOT 1
- [Bug] Datahandler Type Error HOT 2
- Remote PageHandler clears selection HOT 7
- Zero rows shows as all selected HOT 1
- Single Filter Multiple Matches HOT 1
- Filter only working on first column for SSR HOT 2
- filter nested tables, does not hide items inside the nested table HOT 1
- Adjusting the width of a specific TD HOT 1
- Sort based on raw data, not the formatted content HOT 2
- Row selection with filtering issue HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datatables.