material-table icon indicating copy to clipboard operation
material-table copied to clipboard

Search doesnt include columns with custom `render`

Open DmitryNefedov opened this issue 8 years ago • 19 comments

Describe the bug Using column with custom render function exclude it from search.

To Reproduce Steps to reproduce the behavior:

  1. Use columns with render for id
  2. Search by partial id

Expected behavior Search results show columns with matching id

Actual behaviour Search cant use columns with render

Additional context E.g. columns

  const columns = [
    {title: 'Some Id', render: renderSomeId, field: 'some_id'},
...
];

...

const renderSomeId = (row) => {
    <Link to={someUrl + row.some_id}>
          {row.some_id}
        </Link>

}

As a result, we have links with id text shown. If we have ids like abc123, abc234, test000 we would expect that search for abc would show first 2 rows; however, "no results" shown.

DmitryNefedov avatar Oct 29 '18 13:10 DmitryNefedov

Hi @DmitryNefedov,

I will do this feature asap

mbrn avatar Oct 29 '18 14:10 mbrn

@DmitryNefedov ,

I change search to run on field value of every column. Not on render result. Because render result is a React.Element and it is not searchable. if you want to make a column that has custom render method searchable, you should specify the field for it

mbrn avatar Oct 30 '18 05:10 mbrn

If you have any trouble or requirement please feel comfort to ask me?

mbrn avatar Oct 30 '18 05:10 mbrn

@mbrn how I can make custom rendered field searchable?

sarates avatar Jan 22 '19 21:01 sarates

@sarates ,

Add customFilterAndSearch field to your column definition.

customFilterAndSearch: (value, rowData) => {
   return true; // customize here according your search algorithm.
}

mbrn avatar Jan 23 '19 05:01 mbrn

@mbrn thanks a lot!

sarates avatar Jan 23 '19 18:01 sarates

@mbrn Can you please insert some example code of customFilterAndSearch, I'm not understanding how to use this function

Prasad-YS avatar Feb 22 '19 05:02 Prasad-YS

Hi @Prasad-YS ,

I think that would be enough for you

<MaterialTable
  columns={[
    {
      title: 'Name',
      render: rowData => rowData.name + ' ' + rowData.surname,
      customFilterAndSearch: (term, rowData) => (rowData.name + ' ' + rowData.surname).indexOf(term) != -1
    },
    { title: 'Birth Year', field: 'birthYear', type: 'numeric' },
    {
      title: 'Birth Place',
      field: 'birthCity',
      lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
    }
  ]}
  data={[
    {
      name: 'Mehmet',
      surname: 'Baran',
      birthYear: 1987,
      birthCity: 63
    },
    {
      name: 'Zerya Betül',
      surname: 'Baran',
      birthYear: 2017,
      birthCity: 34
    },
  ]}
  title="Custom Render"
/>

mbrn avatar Feb 22 '19 18:02 mbrn

Hi @Prasad-YS ,

I think that would be enough for you

<MaterialTable
  columns={[
    {
      title: 'Name',
      render: rowData => rowData.name + ' ' + rowData.surname,
      customFilterAndSearch: (term, rowData) => (rowData.name + ' ' + rowData.surname).indexOf(term) != -1
    },
    { title: 'Birth Year', field: 'birthYear', type: 'numeric' },
    {
      title: 'Birth Place',
      field: 'birthCity',
      lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
    }
  ]}
  data={[
    {
      name: 'Mehmet',
      surname: 'Baran',
      birthYear: 1987,
      birthCity: 63
    },
    {
      name: 'Zerya Betül',
      surname: 'Baran',
      birthYear: 2017,
      birthCity: 34
    },
  ]}
  title="Custom Render"
/>

Thanks a lot

Prasad-YS avatar Feb 25 '19 04:02 Prasad-YS

I know it is close this, but I need Help the filter box dont appear for me if I dont write field: so I dont understand how you can made a filter with this customFilterAndSearch if the box dont appear

Best Regards

orestesparra avatar Mar 22 '19 15:03 orestesparra

You are right @orestes22

It is a bug. I fixed it. I will publish it asap.

mbrn avatar Mar 22 '19 17:03 mbrn

Still not working. If you dont have Field with something between "" , it isnt works.

Also I would like add in the Filter a Selection of checkbox with the option that appear in the colums. Can I implement that in the customFilterAndSearch ?

Best Regards

orestesparra avatar Mar 26 '19 21:03 orestesparra

you should have field or customFilterAndSearch in column definition. Can you check it again please?

mbrn avatar Mar 27 '19 04:03 mbrn

In your example works , but in my code doesnt, Im not sure why, because I check your code and you only have and "if" for show or not show that. but this is a minor things.

What comment do you have for this: I would like add in the Filter a Selection of checkbox. Can I implement that in the customFilterAndSearch ?

orestesparra avatar Mar 27 '19 13:03 orestesparra

I would like that people choose the word and after that I will filter using that

orestesparra avatar Mar 27 '19 13:03 orestesparra

You can do this by customFilterAndSearch .

mbrn avatar Mar 27 '19 16:03 mbrn

@mbrn what is the solution for grouping columns with custom render? (Already reported here)

anabeatrizzz avatar Nov 23 '21 21:11 anabeatrizzz

@mbrn Your code is not working for me, returning false but table register is still showing:

{
  // ...
  render: (rowData: any) => (
      <RenderComponent entity="user" entityId={rowData.userId} />
    ),
    customFilterAndSearch: async (filter, rowData) => {
      const users: User[] = await getUser().then((response: User) => response)
      const user = users.find((user: User) => user._id === rowData.userId)
      const userName = user.name.toLowerCase()
      const userFound = (userName.indexOf(filter.toLowerCase()) !== -1)
      console.log(userFound)
      return userFound
    },
}

anabeatrizzz avatar Nov 24 '21 18:11 anabeatrizzz

Hi @mbrn,

Your solution works great, but now the search function is case-sensitive. Is there a way to disable this?

Hi @Prasad-YS ,

I think that would be enough for you

<MaterialTable
  columns={[
    {
      title: 'Name',
      render: rowData => rowData.name + ' ' + rowData.surname,
      customFilterAndSearch: (term, rowData) => (rowData.name + ' ' + rowData.surname).indexOf(term) != -1
    },
    { title: 'Birth Year', field: 'birthYear', type: 'numeric' },
    {
      title: 'Birth Place',
      field: 'birthCity',
      lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
    }
  ]}
  data={[
    {
      name: 'Mehmet',
      surname: 'Baran',
      birthYear: 1987,
      birthCity: 63
    },
    {
      name: 'Zerya Betül',
      surname: 'Baran',
      birthYear: 2017,
      birthCity: 34
    },
  ]}
  title="Custom Render"
/>

don-spaghetti avatar Jan 13 '22 14:01 don-spaghetti