# j-Grid

> Display tabular records with filtering, paging and a configurable datasource.

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: en
Content-SHA256: f681777167bcda4d24aafb2c8be7e364025e40c4aaa663bba57380e1ad59e7ce
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: components@910ed1a86f6e72b4f031d3ad0dc7844f9fc5cb8e
Review: not-recorded

- needed `bootstrap` grid system

__Data-source__ needs to contain:

```javascript
{
    items: [{ name: 'Row 1' }, { name: 'Row 2' }, ...] // items
    page: 1,    // current pages
    pages: 30,  // count of pages
    limit: 5,   // items limit per page
    count: 150  // count of items in DB
}
```

__Configuration__:

- `filter` {Boolean} shows a filter (optional, default: `true`)
- `filterlabel` {String} a default placeholder for all filters (optional)
- `external` {Boolean} is an external data-source (dynamic data loading) (optinal, default: `false`)
- `pluralizepages` {String} pluralization for pages (optional, default: `# pages,# page,# pages,# pages`)
- `pluralizeitems` {String} pluralization for items (optional, default: `# items,# item,# items,# items`)
- `exec` {String} a link to `function(type, filter, sort, page)`, it's evaluated if the grid performs some operations
- `padding` {Number} height padding (optional, default: `0`)
- `autosize` {Boolean} enables full-height auto-size (optional, default `true`)
- `boolean` {String} a values for filtering of boolean values (optional, default: `true|on|yes`)
- `pagination` {Boolean} shows pagination panel (optional, default: `false`)
- `init` {Function} an init function for e.g. custom columns
- `checked` {Function} if rows will contain `<input type="checkbox` then this function will be evaluated when the checkbox is checked

__Column properties__:

- `name` {String} a name of field in the object
- `text` {String} a column label
- `title` {String} a column tooltip (optional)
- `size` {Number} a column width: `1` small, `2` medium, `3` large, `4` very large (optional, default `1`)
- `size` {String} a column width in pixels, value needs to contains `px` e.g. `20px`
- `filter` {String/Boolean} a placeholder for the filter or `boolean` can disable filter for this column (optional)
- `format` {String/Number} for formatting values (`string` for date, `number` for numbers)
- `background` {String} a background color (optional)
- `align` {String} can be `center` or `right` (optional, default: `left`)
- `template` {String} can be a Tangular template and the model is the whole object of row
- `render` {String/Function(value, column, row)} a custom rendering, `string` is evaluated as an arrow function
- `header` {String/Function(column)} a custom rendering of header, `string` is evaluated as an arrow function
- `sort` {Boolean} enables sorting (optional, default: `true`)
- `class` {String} additional classes for the column, e.g. `hidden-xs`
- `search` {Boolean/String} `true` will filter a value according to the `template` result or `String` can be a Tangular template which will be used as a value for search

__Filtering__:

- `numbers`: (1) `number` searches an exact number, (2) `number / number` is an interval between numbers
- `strings`: (1) `string` searches an exact string, (2) `string1, string2, string3` is a multiple search criterium
- `dates`: (1) year `2017` searches all dates in this year, (2) `2017-02-12 / 2017-03-13` is an interval between dates, (3) `20.12 / 31.12` an interval between dates in the current year

Use it with `<ui-component name="grid">`. When the component is not loaded yet, jComponent downloads it on first use from the address in `DEF.fallback` (the Componentator CDN by default). Author: Peter Širka. Source: the Componentator components repository, MIT license.

```html example.html
<style>
	body{padding:50px;margin:0;background-color:#F0F0F0;font-family:Arial;height:100%;}
	html{height:100%;}
</style>

<ui-component name="grid" path="griddata" config="exec:filtering">
	<script type="text/plain">
		[
			{ name: 'name', text: 'Name', size: 2, render: 'n => n.toUpperCase()', filter: 'Search in name' },
			{ name: 'price', text: 'Price', size: 1, format: 2, filter: 'Search in prices', align: 'right' },
			{ name: 'description', text: 'Description', size: 3, filter: 'Search in description' },
			{ name: 'created', text: 'Created', size: 1, filter: 'Search in dates', format: 'dd.MM.yyyy', align: 'center' }
		]
	</script>
</ui-component>

<script>
	var griddata = { page: 1, count: 18, pages: 3, limit: 6, items: [] };
	var items = griddata.items;

	items.push({ name: '1 Lorem ipsum dolor.', description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, mollitia!', price: 233, created: new Date().add('-5 days') });
	items.push({ name: '2 Lorem ipsum dolor.', description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, mollitia!', price: 122, created: new Date().add('-1 year') });
	items.push({ name: '3 Lorem ipsum dolor.', description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, mollitia!', price: 119, created: new Date().add('-1 month') });
	items.push({ name: '4 Lorem ipsum dolor.', description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, mollitia!', price: 35.34, created: new Date().add('-14 days') });
	items.push({ name: '5 Lorem ipsum dolor.', description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, mollitia!', price: 439.33, created: new Date().add('-5 hours') });
	items.push({ name: '6 Lorem ipsum dolor.', description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur, mollitia!', price: 111.12, created: new Date().add('-349 minutes') });

	// Auto-resize
	$(window).on('resize', function() {
		setTimeout2('resize', function() {
			EMIT('resize');
		}, 100);
	});

	function filtering(type, filter, sort, page) {
		console.log(type, filter, sort, page);
	}

</script>
```
