Giter Club home page Giter Club logo

Comments (3)

ChristianRiesen avatar ChristianRiesen commented on May 27, 2024

Playing around a bit locally I got some ugly version to work with the following code, which of course is horrible:

function getNestedValue(obj, path) {
  return path.split('.').reduce((acc, part) => acc && acc[part], obj);
}

$: if (shouldFilterRows) {
  let rows = originalRows;
  if (value.trim().length > 0) {
    if (shouldFilterRows === true) {
      rows = rows.filter((row) => {
        return Object.entries(row) // Use entries to get key-value pairs
          .filter(([key]) => key !== "id")
          .some(([key, _value]) => {
            if (typeof _value === "object" && _value !== null) {
              // Handling nested objects
              return Object.entries(_value).some(([nestedKey, nestedValue]) => {
                if (typeof nestedValue === "string" || typeof nestedValue === "number") {
                  return nestedValue
                    .toString()
                    .toLowerCase()
                    .includes(value.trim().toLowerCase());
                }
                return false;
              });
            } else if (typeof _value === "string" || typeof _value === "number") {
              // Handling non-object (string/number) values
              return _value
                .toString()
                .toLowerCase()
                .includes(value.trim().toLowerCase());
            }
            return false;
          });
      });
    } else if (typeof shouldFilterRows === "function") {
      rows = rows.filter((row) => shouldFilterRows(row, value) ?? false);
    }
  }

  tableRows.set(rows);
  filteredRowIds = rows.map((row) => row.id);
}

So it searches through all data available, not just the visible data. Though if I remove the id column from the table, it no longer searches in that. There is some odd behavior here from a usage perspective. My expectation was that it uses the header defined fields, which is not the case.

I will give it a go with a user defined function next in the hopes of not needing my ugly hack. But a generic solution would be pretty welcome I think.

from carbon-components-svelte.

ChristianRiesen avatar ChristianRiesen commented on May 27, 2024

The horribleness continues, however this does the job for my usecase for the very moment in time. It's crappy if you have have nested replies, so perhaps having limited or flattened replies would be better for other use cases, or the ability to specify which fields to look for and supporting nested properties would be great.

Just pasting the Toolbar here for brevity, the rest is bog standard.

      <ToolbarSearch
        persistent
        value=""
        shouldFilterRows={(row, value) => {
          const searchValue = value.trim().toLowerCase();
        
          function searchInObject(obj) {
            return Object.entries(obj).some(([key, val]) => {
              if (typeof val === 'object' && val !== null) {
                return searchInObject(val);
              } else {
                return val.toString().toLowerCase().includes(searchValue);
              }
            });
          }
        
          // Check if the value is in the id or anywhere else in the row
          return searchInObject(row) || row.id.toString().toLowerCase().includes(searchValue);
        }}
        bind:filteredRowIds
      />

from carbon-components-svelte.

theetrain avatar theetrain commented on May 27, 2024

I was able to reproduce the issue; would be a good fix for DataTable.

Reproduction: https://svelte.dev/repl/4012e79a919f42d6b3fe5179ef774757?version=4.2.9 (cannot search for 'super').

from carbon-components-svelte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.