Django Datatables View filter/search

1.1k Views Asked by At

Using django-datatable-view at https://github.com/pivotal-energy-solutions/django-datatable-view for a class based DataTable.

It is all working and loading but i have the issue with searching data. I cannot work out how to search for the whole sentence entered, at the minute it seems to search per word.

class StoresDatatable(Datatable):

    class CustomColumn(columns.DisplayColumn):
            def __init__(self, *args, **kwargs):
                self.field = kwargs.get('field', None)
                if self.field: kwargs.pop('field')
                super().__init__(*args, **kwargs)

            def search(self, model, term):
                return Q(**{ '%s__name' % self.field : term })


    attr = CustomColumn(
        'Attrs', 
        'count_attr', # data source 
        field='attributes',
        processor="get_attributes", 
        allow_regex=True
    )

Code that triggers on choosing filter (outside of table).

const searchValue = 'new test'
                                           
$(".datatable")
    .DataTable()
    .column(columnIndex)
    .search(searchValue, true, true, false)

Searching for 'new test' searches twice, so i would get results for 'new' and results for 'test'. What i need is just results for 'new test'.

Print shows what is being searched

<th data-name="attrs" data-config-sortable="true" data-config-visible="true">Attrs</th>
test
<th data-name="attrs" data-config-sortable="true" data-config-visible="true">Attrs</th>
new

1

There are 1 best solutions below

0
On

I missed a simple thing available in the docs. Needed to wrap in quotes and it works!!