Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filters #103

Closed
13ran opened this issue Jun 7, 2024 · 3 comments
Closed

Filters #103

13ran opened this issue Jun 7, 2024 · 3 comments

Comments

@13ran
Copy link

13ran commented Jun 7, 2024

When I have multiple filters and one is empty it still adds AND to the query and causes a pdo error. If all filters have values or if only one filter is defined the query works.

  $dt->filter('phone_number', function () {
    if ($this->searchValue() === '') {
      return '';
    }
    return '('.$this->column->name.' = '.$this->escape($this->searchValue()).' OR phone_number_formatted = '.$this->escape($this->searchValue()).')';
  });

  $dt->filter('year', function () {
    if ($this->searchValue() === '') {
      return '';
    }

    $val = explode('-delim-', $this->searchValue());

    return $this->between($val[0], $val[1] ?? null);
  });

Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND )t'

@n1crack
Copy link
Owner

n1crack commented Jun 7, 2024

Adding 'AND' for empty values are fixed.

but since filter method was originally added for the individual column filtering, I think we need to add a new method or some parameters to make it available for global too. Let me think about it for a while.

@13ran
Copy link
Author

13ran commented Jun 7, 2024

Sounds good thanks. It would be awesome if there was an exact match function so I don't have to define my own function

@n1crack
Copy link
Owner

n1crack commented Jun 8, 2024

  • added forceExactMatch method to use '=' instead of 'LIKE' as default filtering behavior.
$dt->forceExactMatch(true);
  • added 3rd optional parameter to filter method. (it is INDIVIDUAL by default, as it is before..)

\Ozdemir\Datatables\CustomFilterType::INDIVIDUAL : uses this filter only when individual search happens
\Ozdemir\Datatables\CustomFilterType::GLOBALLY : uses this filter only when global search happens
\Ozdemir\Datatables\CustomFilterType::ALL : uses this filter on both situations

I haven't tested it completely but I think it should work like this.

    $dt->filter('test_test', function () {
        if ($this->searchValue() === '') {
          return '';
        }
    
        $val = explode('-delim-', $this->searchValue());
    
        return $this->between($val[0], $val[1] ?? null);
    }, \Ozdemir\Datatables\CustomFilterType::INDIVIDUAL);

    $dt->filter('test_test', function () {
        if ($this->searchValue() === '') {
          return '';
        }
    
        $val = explode('-', $this->searchValue());
    
        return $this->between($val[0], $val[1] ?? null);
    }, \Ozdemir\Datatables\CustomFilterType::GLOBALLY);

@n1crack n1crack closed this as completed Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants