# jquery-datagrid **Repository Path**: mirrors/jquery-datagrid ## Basic Information - **Project Name**: jquery-datagrid - **Description**: jQuery Datagrid 是一款用来呈现数据网格的 jQuery 插件,适用于本地或远程数据,设计是自适应的,简单易用,可扩展 - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: https://www.oschina.net/p/jquery+datagrid - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-08-18 - **Last Updated**: 2025-12-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README jquery.datagrid =============== - Fetch data from any source: local data or remote data (using ajax, deferred function or plugin) - Render a simple HTML Table easy to style (no imposed css) - Simple columns definition - Semi-automatic sorter and pager (for remote data, you need to code server side) - Plugins for cell, pager and sorter renderers (easy to create, very easy to extend) - Events on each step (you do what you want with your data) - Convert form elements (input, select) into automatic filters (magic!) # Demo jquery.datagrid demo # Install `bower install jquery.datagrid` or `npm install jquery.datagrid` # Configuration Just load `jquery.datagrid.js` (and optional plugin scripts if you want to use them) A simple example with **remote data** (fetch by ajax post) ```html ``` Another simple example with **local data** ```html ``` ## Options ```javascript // All options with default values { source: "default", // plugin url: "", data: false, autoload: true, paramsDefault: {}, paramsMapping: { page: "page", paging: "paging", orderby: "orderby", direction: "direction" }, parse: function( data ) { if ( $.type( data ) === 'string' ) { return JSON.parse( data ); } else { return data; } }, col: [], attr: {}, attrSortable: {}, noData: "no data", onBefore: false, onData: false, onRowData: false, onComplete: false, sorter: "default", // plugin pager: "default", // plugin pagerPosition: "bottom" } ``` ### Options detail List of `option names` ( *expected values* ) #### Get data - `source` ( *string* || *object* || *function* ) : data fetching method: function, plugin name (string) or plugin name with config (object). - *see __Source Plugins__ below* - `url` ( *string* ) : server url where data are fetch (with POST params data). Used by `default` source. - `data` ( *false* || *array* ) : local data (no remote fetch). If not `false`, `source` will be automatically set to `"data"`. - `autoload` ( *boolean* ) : auto load data. If set to `false`, you need to load manualy the data with `datagrid.fetch()` method. - `paramsDefault` ( *object* ) : default params added to the data request. - `paramsMapping` ( *object* ) : you can map param names used for paging and sorting (keys used: see default value). - `parse`( *function* ) : callback function that parse data before render. By default, decode data in `JSON` if data is a `string`. #### Render data - `col` ( *array* ) : array of column definition objects. - *see __Column options__ below* - `attr` ( *false* || *object* ) : an object of attribute-value pairs: generate the table element attributes. - `attrSortable` ( *false* || *object* ) : an object of attribute-value pairs: params for `$(th).attr()` if column is sortable. - `noData` ( *string* || *function* ) : `string`: it will be displayed instead of the table if there is no data. `function`: the result returned by the function will be displayed. #### Plugins - `sorter` ( *string* || *object* ) : display text or icon on table header when columns are sorted (asc or desc). - *see __Plugins__ below* - `pager` ( *string* || *object* ) : render the pager. `default` pager write page numbers in a `span`, all in a `div`. - *see __Plugins__ below* - `pagerPosition` ( *false* || *"top"* || *"bottom"* || *["top","bottom"]* ) : display the pager on `"bottom"` of the `table`, or on the `"top"`, or both with `["top","bottom"]`. #### Events - `onBefore` ( *false* || *function* ) : callback `function()`. Scope is `datagrid`. - `onData` ( *false* || *function* ) : callback `function( { "total": Total number of data without paging, "data": [ Array of row ] } )` - `onRowData` ( *false* || *function* ) : callback `function( data[ numrow ], numrow, $tr )` - `onComplete` ( *false* || *function* ) : callback `function()`. Scope is `datagrid`. ## Columns ### Column options ```javascript // All column options with default values { field: "", // Field name title: "", // Title display in the `th`content attrHeader: {}, // Param for `$(th).attr()` attr: {}, // Param for `$(td).attr()` sortable: false, // `true` activate column sort on `th` click render: "default" // *see cell rendering below* } ``` ### Cell Rendering How to display `td` content depends on column `render` value. - Use a __plugin__ registered with `"plugin-name"` (`"default"` plugin just display `field` value) ```javascript render: "plugin-name" ``` - Use a registered __plugin__ `"plugin-name"` with `params` ```javascript render: { "plugin-name": params } ``` - Use a callback __function__ ```javascript render: function( data ){ // scope (this) is the `$(td)` in the callback // fill `td` with `$(td).html( returned value )` return data.value; // if you return false, `td` content will not be changed // you can update cell with `this.html()`, `this.append()`, ... this.html( data.value ); return false; // `data` is an object like this data = { value: "the value of the field key", field: "the field key name", row: "row data object (row.fieldname = value)", colindex: "column number (first is 0)" } } ``` # Flow of data and events ### Fetch To fetch data and launch the flow, you need to call datagrid `fetch( filters )` method. It will be automatically executed when `option.autoload = true`. `filters` is an optional object of attribute-value pairs. They are merged with paging and sorting params and send when fetching data. Fetching data do not reset previous params (usefull with auto filters). You can reset params to default with `reset()` method. ```javascript // Scope (this) in the callback is `datagrid` (the plugin). "onBefore()" event is called (if defined) before fetching data. ``` How data is fetched depends on `source` option type: #### string or object Plugin will be used (if it exists). `"default"` plugin use `$.post( options.url )` to get data. *see __Source plugin__ below* #### deferred function `$.when( options.source() )` is used. Scope (`this`) in the callback is the datagrid instance. So you can get datagrid params with `params()` method. After data is fetched from source, `render( data )` is called automatically when you `defer.resolve( data )`. ```javascript // example with deferred $.get() "source": function() { return $.get( "url", this.params() ); } ``` ```javascript // example with deferred function "source": function() { return $.Deferred(function( defer ) { async_call( callback_success( data ) { defer.resolve( data ); }, callback_error( error ) { defer.reject( error ); } ); }).promise(); } ``` ### Parse Data is parsed by `parse()` method. Default method parse data with `JSON.parse()` (if data is a string). Method can be changed with datagrid `parse` option. ### Data Format Data format expected to render the datagrid is an object like this: ```javascript { total: Number of rows (without pagination), data: [{ "fieldname1": value1, "fieldname2": value2, ... }, { "fieldname1": value3, "fieldname2": value4, ... } }] } ``` ### Render HTML table is displayed when `datagrid.render( data )` method is called. ```javascript // Data is in expected format. // Data returned replace old data. Return false or nothing to not change data. "onData( data )" event is called (if defined). ``` If a `sorter` plugin is defined, click events are attached on `th` (if column `sortable` option is set to `true`) ```javascript // Usefull to change attributes of a `tr`. // Data returned replace old data. Return false or nothing to not change data. "onRowData( rowdata, numrow, $tr )" event is called on each "tr" line (if defined). ``` Each cell is displayed (*see cell rendering*) ```javascript "onComplete()" event is called (if defined) when all is rendered. ``` # Methods Methods can be called in 2 ways: - With selector used to create datagrid ```javascript $( selector ).datagrid( "methodName", methodParams ); ``` - Or with a reference to the datagrid instance ```javascript datagrid.methodName( methodParams ); ``` You can get a reference to the datagrid instance with ```javascript $( selector ).datagrid( "datagrid" ); // it's chainable $( selector ).datagrid( "datagrid" ).methodName( methodParams ); ``` ### Methods list ```javascript // fetch source (get data). `filters` is an object of attribute-value pairs (optional). $( selector ).datagrid( "fetch", filters ); ``` ```javascript // get page number (first page is 1). $( selector ).datagrid( "page" ); // set page number. `fetch()` is not called. $( selector ).datagrid( "page", page ); ``` ```javascript // get paging number (default paging is 15). $( selector ).datagrid( "paging" ); // set paging number. `fetch()` is not called. $( selector ).datagrid( "paging", paging ); ``` ```javascript // get orderby (default orderby is ""). $( selector ).datagrid( "orderby" ); // set orderby. `fetch()` is not called. // sorter plugin use field name. $( selector ).datagrid( "orderby", orderby ); ``` ```javascript // get direction (default direction is ""). $( selector ).datagrid( "direction" ); // set direction. `fetch()` is not called. // sorter plugin use "asc" or "desc". $( selector ).datagrid( "direction", direction ); ``` ```javascript // get params used for data request. $( selector ).datagrid( "params" ); // reset params to default. $( selector ).datagrid( "reset" ); ``` ```javascript // render HTML table. $( selector ).datagrid( "render", data ); ``` ```javascript // define selected form element(s) (and children) as automatic filters. *see __Filters__ below* $( selector ).datagrid( "filters", selector ); ``` # Filters You can magically add automatic filters with the `filters( selector )` method. It works with all form elements. ```javascript // just pass a $element (jquery element) or a "selector" string to the filters method $( selector ).datagrid( "filters", $element ); // you can also use `datagrid` reference $( selector ).datagrid( "datagrid").filters( $element ); ``` All form elements (input, select, textarea) contents in the `$element` win a `change` event that automatically call `datagrid.fetch()`. The changed element value is added to the sent params (key is the html element name). You can disable an element by adding a `"data-datagrid-filter"="disable"` attribute. # Plugins If you don't specify a plugin, `default` plugin is used. Plugin are used to: - fetch data (`source` plugins) - display information on sorted columns header (`sorter` plugins) - display formated data in the table cells (`cell` plugins) - display pagination (`pager` plugins). ### Use a plugin For each plugin's type, the plugin name is unique. If you add 2 plugins on the same type with the same name, the second will replace the first. ```javascript // with default options (or if plugin has no options) "plugin-type": "plugin-name" ``` ```javascript // with options (depends on plugin) "plugin-type": { "plugin-name": options } ``` ### Create a plugin All plugins are created like this ```javascript $.fn.datagrid( "plugin", "plugin-type", "plugin-name", callback ); ``` - `"plugin-type"` is a `String`. Allowed values are `"source"`, `"cell"`, `"sorter"` or `"pager"`. - `"plugin-name"` is what you want (just a `String`). If you use `"default"` name, you'll change default plugin. - `callback` is a `Function`. Arguments send to the function and expected return value depends on plugin's type (see each type for more information). ### Extend a plugin You can also extend existing plugins ```javascript $.fn.datagrid( "plugin", "plugin-type", "new-plugin-name", "extended-plugin-name", options ); ``` - `"plugin-type"` is a `String`. Value need to match with plugin extended. - `"new-plugin-name"` is the name of your new plugin (a `String`). - `"extended-plugin-name"` is the name of the plugin extended (the "source"). You can extend `"default"` plugin. - `options` is an `Object` passed as last argument to `"extended-plugin-name"` callback when `"new-plugin-name"` is called. `options` are merged. And off course you can extend an extended plugin! :-) ## Source plugin `source` plugin is used to fetch data. In the callback, `this` is the instance of the datagrid plugin. So you can get datagrid params with `params()` method. To display the HTML table, you need to call datagrid `render( data )` method. #### Source plugin "default" (alias "post") `"default"` plugin (alias `"post"`) call `$.post()` to get data. The only option is `url`. If not set, it use datagrid `url` option. #### Source plugin "data" `"data"` plugin is used for local data. You can change sorter and filter functions by your own with `sorter` and `filter` options. If `data` option is filled, it will be used instead of datagrid `data` option. All options are optional. ```javascript $( selector ).datagrid({ source: { data: { sorter: function( data, key, comparator ) { // `data`: all the data // `key`: sorted fieldname // `comparator`: `1` (asc) or `-1` (desc) return sortedData; }, filter: function( data, filters ) { // `data`: all the data // `filters`: object of attribute-value pairs return filteredData; }, data: [] // array of object (attribute-value pairs) } } }); ``` #### Source plugin: create custom ```javascript // example: "get" instead of "post" ajax $.fn.datagrid( "plugin", "source", "get", function( sourceOptions ) { var options = { url: "" }; if ( sourceOptions ) { $.extend( options, sourceOptions ); } var datagrid = this; $.get( options.url, datagrid.params(), function( result ) { datagrid.render( result ); }); }); ``` ## Cell plugin `cell` plugin is used to render `