# posts-data-table/1.1.4/assets/js/posts-data-table.js

Posts Table with Search &amp; Sort, version 1.1.4. 50 lines.

- Page: https://pluginprobe.com/plugins/posts-data-table/1.1.4/code/assets/js/posts-data-table.js
- Raw: https://pluginprobe.com/plugins/posts-data-table/1.1.4/raw/assets/js/posts-data-table.js
- Modified: 2018-08-06T14:14:12+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/posts-data-table/1.1.4/code/assets/js/posts-data-table.js#L10-L20`.

```javascript

( function( $ ) {

	$( document ).ready( function() {

		var tables = $( '.posts-data-table' );
		var adminBarVisible = $( '#wpadminbar' ).length;

		tables.each( function() {

			var config = {
				responsive: true,
				processing: true // display 'processing' indicator when loading
			};

			// Set language - defaults to English if not specified
			if ( ( typeof posts_data_table !== 'undefined' ) && posts_data_table.langurl ) {
				config.language = { url: posts_data_table.langurl };
			}

			// Initialise DataTable
			var table = $( this ).DataTable( config );

			// If scroll offset defined, animate back to top of table on next/previous page event
			$( this ).on( 'page.dt', function() {
				if ( $( this ).data( 'scroll-offset' ) !== false ) {
					var tableOffset = $( this ).parent().offset().top - $( this ).data( 'scroll-offset' );
					if ( adminBarVisible ) { // Adjust offset for WP admin bar
						tableOffset -= 32;
					}
					$( 'html,body' ).animate( { scrollTop: tableOffset }, 300 );
				}
			} );

			// If 'search on click' feature enabled then add click handler for links in category and author columns.
			// When a category or author is clicked, the table will filter by that value
			if ( $( this ).data( 'click-filter' ) ) {
				table.columns( ['category:name', 'author:name'] ).nodes().to$().each( function() {
					$( this ).children( 'a' ).on( 'click', function() {
						table.search( $( this ).text() ).draw();
						return false;
					} );
				} );
			}

		} ); // each table

	} ); // end document.ready

} )( jQuery );
```
