lizmap-web-client icon indicating copy to clipboard operation
lizmap-web-client copied to clipboard

Filter by column in attribute table

Open nboisteault opened this issue 7 years ago • 21 comments

It would be interesting to have text input above all/some columns to filter them one by one.

nboisteault avatar Feb 02 '18 08:02 nboisteault

Here is some code to add this enhancement by extending Lizmap.

Limitation : it adds a text input on every columns with values. The filter just look for strings matching the word searched. I've put a setTimeout because of #997 .

This code can be adapted to make smarter search based on attributes type.

lizMap.events.on({
	attributeLayerContentReady: function(e) {
		setTimeout(function(){
			$('#attribute-layer-table-'+e.featureType+'_wrapper thead:first th').not('.sorting_disabled').each( function () {
			    var title = $(this).text();
			    $(this).html( '<input type="text" placeholder="Search'+title+'" />' );
			});

			var table = $('#attribute-layer-table-'+e.featureType).DataTable();

			table.columns().every( function () {
		       var column = this;

		       $( 'input', this.header() ).on( 'keyup change', function () {
		           if ( column.search() !== this.value ) {
		               column
		                   .search( this.value )
		                   .draw();
		           }
		       }).click(function(e) {
				   // We don't want to sort when users click on the search field
				   e.stopPropagation();
				});
		   });
			lizMap.refreshDatatableSize("#attribute-layer-main-"+e.featureType)
		}, 10);
	}
});

nboisteault avatar Oct 29 '18 14:10 nboisteault

Thank you for this code. It's really useful.

However, this works for some layers and not others. I couldn't find a common factor for the layers that work or don't work... Do you have any idea?

loic74 avatar Nov 21 '18 13:11 loic74

Do you have a public link to share ?

nboisteault avatar Nov 21 '18 14:11 nboisteault

Unfortunately, there is no public link... I sent you an email with username and password.

loic74 avatar Nov 23 '18 08:11 loic74

@loic74 : thank you for the access, it helped me found the bug. The issue was with accented layers. Here is fixed code :

lizMap.events.on({
	attributeLayerContentReady: function(e) {
		setTimeout(function(){
			var cleanLayerName = lizMap.cleanName(e.featureType);

			if( $('#attribute-layer-table-'+cleanLayerName+'_wrapper').data('filtersON') == undefined ){
				// Set flag to add filters only once
				$('#attribute-layer-table-'+cleanLayerName+'_wrapper').data('filtersON', true);

				$('#attribute-layer-table-'+cleanLayerName+'_wrapper thead:first th').not('.sorting_disabled').each( function () {
				    var title = $(this).text();
				    $(this).html( '<input type="text" placeholder=" '+title+'" />' );
				});

				var table = $('#attribute-layer-table-'+cleanLayerName).DataTable();

				table.columns().every( function () {
			       var column = this;

			       $( 'input', this.header() ).on( 'keyup change', function () {
			           if ( column.search() !== this.value ) {
			               column
			                   .search( this.value )
			                   .draw();
			           }
			       }).click(function(e) {
					   // We don't want to sort when users click on the search field
					   e.stopPropagation();
					});
			   	});
				lizMap.refreshDatatableSize("#attribute-layer-main-"+cleanLayerName);
			}
		}, 500);
	}
});

nboisteault avatar Nov 28 '18 08:11 nboisteault

Well done. After clearing the browser cache, everything works fine. Thank you again.

loic74 avatar Nov 28 '18 10:11 loic74

@josemvm @loic74 I've updated my last answer to fix a bug.

nboisteault avatar Feb 13 '19 15:02 nboisteault

nice @nboisteault thanks :-)

josemvm avatar Feb 13 '19 15:02 josemvm

@nboisteault can you add your code here: https://github.com/3liz/lizmap-javascript-scripts

rldhont avatar Feb 13 '19 15:02 rldhont

@rldhont https://github.com/3liz/lizmap-javascript-scripts/pull/1

nboisteault avatar Feb 14 '19 08:02 nboisteault

The script works fine, it’s really useful. To have a correct result of a search we need to write “string to search”, otherwise the result won’t be exactly equal to the string. For example, if we write pozzo 3 the resulting records are referred to pozzo 3, pozzo 13, pozzo 31 etc . Is it possible to insert the “ “ directly in the script? Other improvements could be: a button X to clear previous search and resize column width in function of the length of the field name.

tethysgeco avatar Feb 15 '19 10:02 tethysgeco

@tethysgeco you can replace :

column
    .search( this.value )
    .draw();

by

column
    .search( this.value, true, false )
    .draw();

to tell datatables to match exactly the string. Reference.

nboisteault avatar Feb 27 '19 16:02 nboisteault

I have updated my code here to tell how to active filter only on specific columns. I'll publish another example with checkboxes dropdowns later.

nboisteault avatar Feb 27 '19 16:02 nboisteault

We tried to add an array of coloumns but we didn't manage it. Could you provide us an example? Sorry but we aren't expert in js. Thanks

tethysgeco avatar Jun 17 '19 09:06 tethysgeco

I tried to use the code but nothing happens when I open the attribute table. Is there something to configure before ?

Cww7 avatar Nov 06 '19 07:11 Cww7

@Cww7 Yes, look at comment // You can put an array of index in columns() to tell on which column you want the search input to appear. You have to add an array with indexes of the column you want a filter on. An example : $('#attribute-layer-table-'+cleanLayerName).dataTable().api().columns([2, 3]).every( function () {

nboisteault avatar Nov 06 '19 09:11 nboisteault

Nothing change... No need to indicate the attribute layer ?

Cww7 avatar Nov 06 '19 10:11 Cww7

Do you have a public link to share ?

nboisteault avatar Nov 06 '19 11:11 nboisteault

No, unfortunately... I usually use other js scripts and it works. My tables are on postgresql 11.5 database, with Lizmap 3.3 and Qgisserver 3.4. I have to think about it but I have no solution for the moment.

Cww7 avatar Nov 06 '19 12:11 Cww7

@Cww7 I have updated the script to fix a bug. All columns of every layers have filter active by default, I'll add a way to configure that later.

nboisteault avatar Nov 08 '19 09:11 nboisteault

It works great !! Thx @nboisteault !

Cww7 avatar Nov 12 '19 10:11 Cww7