2

I created Magento 2 Grid with UI component and it works with all of the things (filters, columns, sorting ) but I want to search with Case sensitive(as of now it works with searching but by default is not case sensitive. For example ABCDEF and ABCdef both are different). So is there any way to search with case sensitive in UI component?

2

The problem could be with the source: I'm assuming here that your grid uses a collection to show your entities. So your search is applied on the collection. This roughly translates to a SQL-query like:

SELECT * FROM `table_name` WHERE `field` LIKE '%ABCdef%'
SELECT * FROM `table_name` WHERE `field` = 'ABCdef'

If your column in MySQL is not set as binary, your search is case-insensitive. This is the default value for a lot of columns, and is also the default if you create your own table columns without explicitly setting it to binary:

search in SQL

So if you want your search to be case-sensitive, make sure that your storage engine is as well. This can be done in the install-script of your module or in an upgrade script.

I'm not sure if the default Magento addColumn() / updateColumn() support this by setting options, so it could be that you need to do some raw SQL query for this one. For example:

ALTER TABLE `table_name` CHANGE `field` `field` TEXT CHARACTER SET utf8 BINARY NULL COMMENT 'My Field';

update

If you don't want / can alter your table, or you want to case sensitive search to be optional from the frontend you'll have to hook in to the filtering process of your collection so it will inject a binary search ad-hoc:

SELECT * FROM `table_name` WHERE BINARY `field` LIKE '%ABCdef%'
SELECT * FROM `table_name` WHERE BINARY `field` = 'ABCdef'

I haven't tested it yet, but this should work:

$collection->addFieldToFilter(new \Zend_Db_Expr('BINARY `field`'), ['like' => 'abc']);
  • Thanks for answer, It seems like a valid point, let me check it at my end +1. one more thing suppose If I will add my column as binary then it will always search as case sensitive but I in the filter column I added option to search case sensitive with Yes or No, so do you have any Idea or any suggestions for this – Keyur Shah Oct 24 '18 at 12:12
  • If that is the case then you don't need to alter your table but just add BINARY to your query. I'll update my answer. But this implies that you must add some logic to how your collection is filtered. – Giel Berkers Oct 24 '18 at 12:45
  • You are absolutely right in your answer :) but I am filtering collection in the grid (which is built by UI component) in this case I am not sure @Giel – Keyur Shah Oct 24 '18 at 13:00
  • If I'm not mistaking a UI component calls a dataprovider (XHR) that is driven internally by a collection. So what you need to do is make sure that your "case sensitive" option is passed to the dataprovider in the backend and filter the collection accordingly. This could imply that you might have to do some more backend work, since typically for such tasks you would create a virtual type for your grid collection but now you need to add some logic to that. Perhaps a plugin could do the job as well. – Giel Berkers Oct 24 '18 at 13:11
  • Thank you so much for your suggestions, I think I just need to play inside to make it work. :) – Keyur Shah Oct 24 '18 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.