omines / datatables-bundle

DataTables bundle for Symfony
https://omines.github.io/datatables-bundle/
MIT License
252 stars 113 forks source link

Filter column not displayed #194

Closed FigaCZ closed 1 month ago

FigaCZ commented 3 years ago

Hello, maybe I didn't search docs deeply, but I am not able to get column filter displayed. I have this setup.

Controller:

 $userFilter = new ChoiceFilter();
        $userFilter->set([
            'choices' => ['foo' => 'bar', 'bar' => 'baz']
        ]);
        $table = $dataTableFactory->create()
        ->add('created_at', DateTimeColumn::class, ['label' => 'Datum', 'format' => 'j.n.Y H:i', "searchable" => false])
        ->add('userName', TextColumn::class, [
            'label' => 'Uživatel',
            "searchable" => true,
            'render' => function ($value, $context) {
                $user = $context->getUser();

                return $user ? $user->getFirstname().' '.$user->getLastname() : '';
            },
            'filter' => $userFilter
        ])->createAdapter(ORMAdapter::class, [
                'entity' => Copy::class,
            ])
            `->handleRequest($request);

datatables.yaml

# Latest documentation available at https://omines.github.io/datatables-bundle/#configuration
datatables:
    # Set options, as documented at https://datatables.net/reference/option/
    options:
        lengthMenu : [10, 25, 50, 100, 250, 500, 1000, 2500]
        pageLength: 50
        dom: "<'row' <'col-sm-12' ftr>><'row' <'col-sm-6'l><'col-sm-6 text-right'pi>>"
        searching: true

    template_parameters:
        # Example classes to integrate nicely with Bootstrap 3.x
        className: 'table table-striped table-bordered table-hover data-table'
        columnFilter: "both"

    # You can for example override this to "tables" to keep the translation domains separated nicely
    translation_domain: 'messages'

I am not able to see column filter. What is wrong? Setup or datatable template. Thanks for reply.

mash-potato47 commented 3 years ago

I'm also experiencing this problem and as mentioned above there is no documentation about how to implement this properly.

Can anyone explain how this should be implemented - is there additional javascript required to get it working?

Thanks!

Gerben321 commented 3 years ago

Same here.

kapcus commented 3 years ago

+1

GFChris commented 3 years ago

+1

MadCat34 commented 2 years ago

Same here...

It could be nice to have documentation for this...

ducho commented 2 years ago

Hi everybody! Did anyone come up with something with that filter? The author ignores our questions and no documentation. @MadCat34 @kapcus @Gerben321 @FigaCZ @mash-potato47

RobQuistNL commented 1 year ago

nope, filter option doesn't work

github-actions[bot] commented 1 year ago

Stale issue message

RobQuistNL commented 1 year ago

nah, not stale, just ignored :+)

curry684 commented 1 year ago

I neither wrote nor use nor reviewed this feature: https://github.com/omines/datatables-bundle/pull/120

Not much I can do to help with an issue I don't really fully understand myself. I'm keeping it open so other people can help each other, that's how open source works.

curry684 commented 1 year ago

There, now it has a label showing I also have no clue about how to solve this issue.

RobQuistNL commented 1 year ago

I neither wrote nor use nor reviewed this feature: #120

Not much I can do to help with an issue I don't really fully understand myself. I'm keeping it open so other people can help each other, that's how open source works.

I didn't mean it personal :) Just read it and it sounds a bit harsh, didn't mean it like that ;)

I have no clue on how to fix it either, otherwise I would have opened a PR :+)

github-actions[bot] commented 1 year ago

Stale issue message

art-fatal commented 1 year ago

+1

roman-1983 commented 1 year ago

+1

andrea-daru commented 1 year ago

Hi all! I'm migrating from SG Datatable to Omines Datatable as the previously mentioned is no longer maintained. I can't figure out how to configure column filters. I'm experiencing the same problem reported by other user in this issue. The issue is in closed status because no one can find a solution? Any info / help will be appreciated.

curry684 commented 1 year ago

Given all the comments above I'd recommend considering this specific feature 'broken'.

We use the library a lot at our company, so we do the maintenance, but only on core features that we developed ourselves and use. It would appear this contributed feature wasn't implemented well unfortunately, nor documented, so we'll likely remove it during an upcoming maintenance round.

Shotman commented 1 year ago

I managed to have column filtering on a project of mine, for now it's still early (like 30sec ago) later, most likely next week, I'll create a minimal reproduction repo and link it here, no idea if it's the bundle's intend way but if it works it works.

To sum it up for people who want to try I added this to the initComplete config inside my twig template

                initComplete: function () {
                let api = this.api();

                api.columns().every(function () {
                    let column = this;
                    let title = column.header().textContent;
                    let th = column.header();

                    // Create input element
                    let input = document.createElement('input');
                    input.classList.add('form-control');
                    input.addEventListener("click", function(e) {
                        e.stopPropagation();
                    });
                    input.placeholder = title;
                    th.replaceChildren(input);

                    // Event listener for user input
                    let debouncedFunction = debounce(() => {
                        if (column.search() !== this.value) {
                            let idx = [...th.parentNode.children].indexOf(th);
                            let val = input.value;
                            if(val.length < 3) return;
                            api.column(idx + ':visible')
                                .search(val ? '%'+val+'%' : '')
                                .draw();
                        }
                    },500)
                    input.addEventListener('keyup', debouncedFunction);
                });
            }

and to my column I added those options : ["searchable" => true, "filter" => $nameFilter, "operator" => "LIKE"] and $nameFilter is just a TextFilter on wich I called to set method and put "operator" => "LIKE" as well

And this adds a text input to each column that perfoms a like operation in the backend

Chris53897 commented 11 months ago

@Shotman I assume your solution search for the actual displayed entries (pageLength). Not in the database. Is that correct? This will be suiteable for small datasets and if all entries are displayed at once.

Shotman commented 11 months ago

Works on the database, but after some time using that it's not perfect, saving to query parameters doesn't work, url encoding fails because of the added % etc it's a bandaid to have something that kinda works but isn't a real solution but it could be a stepping stone for something solid and stable

Chris53897 commented 11 months ago

Thanks for the fast response. I will wait for the reproducer repo. It that is still the next step.

Shotman commented 11 months ago

Here is the repo, https://github.com/Shotman/datatable-repo, not the pretiest readme or anything but does the job to recreate what I did on my personal project

EDIT : and from what I've seen like just now, it's likely to be vulnerable to SQL injections with a good enough crafted filter so yeah do not use in production ^^'

github-actions[bot] commented 9 months ago

Stale issue message

auroraeosrose commented 6 months ago

So I've dug into the code here - this is not a small fix as it's been broken for a LONG time and never been forward ported In between the breakage there are PHP 8.x fixes that have to happen, symfony fixes that have to happen, twig fixes that have to happen...

But I need it for work so - meh

apparently there was an issue with the twig templates from quite a long time ago when settings/options for the base datatables class were refactored. As a "fix" the part that generated the input/js for doing the searching was just .. commented out I updated the twig templates, uncommented the lines, fixed some calls, added a constructor to the filter abstract class (seriously ick I shouldn't have to call set to use a filter without a PHP error.... ) but I'm still banging up against the JS side

I'm working on a fix (and warning I'm on Symfony 7 at this point) on a fork at this point

auroraeosrose commented 6 months ago

I have it about 90% working

https://github.com/burroaktoolinc/datatables-bundle/tree/feature_column_searching

That branch has the php/twig side working

Issue is I have to have this in the initComplete in the datatables declaration to get the JS side of the inputs hooked up - might be a better way? Also this has no debounce which probably should be added. Any JS gurus have ideas?

initComplete: function () {
            let api = this.api();

            api.columns().every(function () {
                const that = this

                // Attach to input fields
                $('input', this.footer()).on('keyup change clear', function () {
                    if (that.search() !== this.value) {
                        that.search(this.value).draw()
                    }
                })

                $('select', this.footer()).on('change', function () {
                    that.search(this.value).draw()
                })
            })
        }
auroraeosrose commented 5 months ago

I have it working now, but it required quite a bit of rework with some features/options copied over from the global search changes made - and it doesn't support multiple filters on a single column (yet)

And the js initComplete is still the way to "hook up" the inputs/selects which is not fabulous

Wuffz commented 4 months ago

I have it working now, but it required quite a bit of rework with some features/options copied over from the global search changes made - and it doesn't support multiple filters on a single column (yet)

And the js initComplete is still the way to "hook up" the inputs/selects which is not fabulous

Need any help on this? I'd like to see this merged so we can use it haha

auroraeosrose commented 4 months ago

@Wuffz

At this point I don't have a lot of impetus to contribute the fix back here - branch is freely available in my fork if you want to try to get it "right" enough to get merged here - https://github.com/burroaktoolinc/datatables-bundle/tree/feature_column_searching

It's working for us off our fork with the caveat of no multiple filters (cause we don't need it)

github-actions[bot] commented 2 months ago

Stale issue message