1
$\begingroup$

I would like to filter data in the table dynamically. Here is the example of the code:

search = "da";
data = {Range[100],ResourceFunction["RandomString"][{"a", "d", "e", "n", "r", "s"}, {100, 5}]} // Transpose;
Column@{
   InputField[Dynamic@search, String],
   Dynamic@search,
   Cases[data, {_, x_} /; StringContainsQ[x, search]] // TableView
 }

Its dynamically updates 'search' variable on the change of InputField, but not when I apply Cases filter. When I put 'Dynamic@search instead' of 'search' inside of 'StringContainsQ' it return the error message "Element da is not a valid string or pattern element in da". I will be appreciated if somebody could help me to solve the issue.

$\endgroup$
2
  • 2
    $\begingroup$ Cases doesn't know how to handle dynamic variables. You have to wrap Dynamic around the entire TableView to make it work, e.g. Column@{InputField[Dynamic@search, String], Dynamic@search, Cases[data, {_, x_} /; StringContainsQ[x, search]] // TableView // Dynamic}. This way, the entire view will be recreated every time search changes $\endgroup$
    – Lukas Lang
    Nov 17, 2022 at 16:26
  • 1
    $\begingroup$ Try Dynamic with the Column: Dynamic@Column@{ InputField[Dynamic@search, String] , search , TableView[Cases[data, {_, x_} /; StringContainsQ[x, search]]] } $\endgroup$
    – Syed
    Nov 17, 2022 at 16:33

1 Answer 1

3
$\begingroup$

Try:

search = "da";
SeedRandom[1];
data = {Range[100], 
    ResourceFunction["RandomString"][{"a", "d", "e", "n", "r", 
      "s"}, {100, 5}]} // Transpose;
Column@{InputField[Dynamic[search], String], Dynamic@search, 
    Cases[data, {_, x_} /; StringContainsQ[x, search]] // TableView //
      Dynamic} // Print;

enter image description here

and with a different input of "aa":

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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