vincjo / datatables

A toolkit for creating datatable components with Svelte
https://vincjo.fr/datatables
MIT License
372 stars 15 forks source link

search with complex table-data throws errors #7

Closed michapixel closed 1 year ago

michapixel commented 1 year ago

What fixed it for me was: changed ca line 35 of Context.js from

return Object.keys(row).some(k => {
  return row[k]
   .toString()
   .toLowerCase()
   .normalize("NFD")
   .replace(/[\u0300-\u036f]/g, "")
   .indexOf($globalFilter
   .toString()
   .toLowerCase()
   .normalize("NFD")
   .replace(/[\u0300-\u036f]/g, "")) > -1;
});

TO

return Object.keys(row).some(k => {
if(row[k] !== null) { 
  return row[k]
   .toString()
   .toLowerCase()
   .normalize("NFD")
   .replace(/[\u0300-\u036f]/g, "")
   .indexOf($globalFilter
   .toString()
   .toLowerCase()
   .normalize("NFD")
   .replace(/[\u0300-\u036f]/g, "")) > -1;
} else {
  return '';
}
});

is there a way to intervene witrh the search functionality? like the filter function ....

vincjo commented 1 year ago

Correct, thank you very much. I just applied your fix and reposted to npm.

I will look to allow passing a function as a parameter of search(). seems pretty consistent

vincjo commented 1 year ago

The search function handles nested objects recursively. (same for filters).

Moreover we can specify a scope to limit the columns searched

    const scope = [ 'last_name', 'first_name', 'pets' ]

    handler.search(value, scope)
knd775 commented 1 year ago

The search function handles nested objects recursively. (same for filters).

Moreover we can specify a scope to limit the columns searched

    const scope = [ 'last_name', 'first_name', 'pets' ]

    handler.search(value, scope)

Is it possible to add nested data to the scope? We're using graphql, so our data comes back heavily nested. I haven't had much luck sorting or searching the columns that use nested properties.

vincjo commented 1 year ago

in the current implementation this is not possible.

What would be needed is another method eg handler.customSearch(params)

vincjo commented 1 year ago

It is now possible to define a callback inside the scope array . Since 1.12.1. This allows to manage nested properties.

    const scope = [ 
        'last_name', 
        'first_name', 
        (row) => row.nestedProp.key
    ]

    handler.search(value, scope)