rubanraj54 / vue-bootstrap4-table

Advanced table based on Vue 2 and Bootstrap 4 ⚡️
MIT License
220 stars 58 forks source link

Custom filter type or overpower disable input? #33

Closed grafxflow closed 5 years ago

grafxflow commented 5 years ago

I am trying to do an automated column query in Laravel which looks like below.

// Check for basic text search
foreach ($objectMode->filters as $key => $value) {
            if($value->type === 'simple'){
                $query->where($value->name, 'LIKE', "%{$value->text}%");
            }
        }

Now this works fine for things like Firstname and Lastname which are actual columns in the database, but I want to CONCAT them in the output table and query this as well. It causes a problem due to the column name not existing in the database because it's a custom name.

->whereRaw("(CONCAT(first_name, '  ', last_name) LIKE "%{$value->text}%");

Can I add a custom filter name instead of simply.

filter: {
type: "simple",
filter: {
type: "concat_simple",

And then do a database query like...

// Check for basic text search
foreach ($objectMode->filters as $key => $value) {
            if($value->type === 'simple'){
                $query->where($value->name, 'LIKE', "%{$value->text}%");
            }
            if($value->type === 'concat_simple'){
                $query->whereRaw("(CONCAT(first_name, '  ', last_name) LIKE "%{$value->text}%");
                // Or if its possible after the Select CONCAT
                // where (CONCAT)username LIKE %{$value->text}%"
                 $query->where($value->name, 'LIKE', "%{$value->text}%");
            }
 }

Currently not using 'simple' in the filter disables/hides the input option. Or is there a way I can manually enable the search input no matter what the filter type?

Or maybe add 2 values and explode the comma and concat + like that way?

{
        label: "Username",
        name: "first_name, last_name",
rubanraj54 commented 5 years ago

Hi @grafxflow ,

can you try something like this temporarily?

Create an array with possible concat fields and check the $value->name is present in the array or not. If it is present in the array, then do concat search else do normal search.

$concatFields = ["first_name","extrafield1","extrafield2"];
// Check for basic text search
foreach ($objectMode->filters as $key => $value) {
    if($value->type === 'simple'){
        if(in_array($value->name,$concatFields)) {
            $query->whereRaw("(CONCAT(first_name, '  ', last_name) LIKE "%{$value->text}%");
            // Or if its possible after the Select CONCAT
            // where (CONCAT)username LIKE %{$value->text}%"
             $query->where($value->name, 'LIKE', "%{$value->text}%");
        } else {
            $query->where($value->name, 'LIKE', "%{$value->text}%");
        }
    }
 }

But I will try to add more information with the filter object to identify the fields more uniquely. May be i will add the column slot name along with the filter object.

Cheers, Ruby.

grafxflow commented 5 years ago

Hi @rubanraj54,

Thanks for the reply, it's pointed me in the right direction :)

cheers grafxflow

rubanraj54 commented 5 years ago

Glad that it helped you.

Cheers, Ruby.