Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original
jQuery EasyUI Data Grid and Tree Plugin - Datagrid Data Grid
jQuery EasyUI Data Grid and Tree Plugin - Datagrid Data Grid
jQuery EasyUI Plugins
Extends $.fn.panel.defaults. Override the default defaults via $.fn.datagrid.defaults.
The datagrid displays data in a tabular format and provides rich support for selecting, sorting, grouping, and editing data. The datagrid is designed to reduce development time without requiring developers to have specific knowledge. It is lightweight yet feature-rich. Its features include cell merging, multi-column headers, frozen columns, footers, and more.
Depends on
panel
resizable
linkbutton
pagination
Usage
Create a datagrid from an existing table element, defining columns, rows, and data in HTML.
After changing data on the server, update the front-end data.
$('#dg').datagrid('reload'); // reload the current page data
DataGrid Properties
This property extends from panel; the following are properties added for the datagrid.
Name
Type
Description
Default value
columns
array
The configuration object for datagrid columns; for more details, see the column properties.
undefined
frozenColumns
array
Same as the column properties, but these columns will be frozen on the left.
undefined
fitColumns
boolean
When set to true, columns will automatically expand or shrink to fit the grid width and prevent horizontal scrolling.
false
resizeHandle
string
Adjusts column positions; available values are: 'left', 'right', 'both'. When set to 'right', users can resize a column by dragging its right edge in the column header.
This property is available since version 1.3.2.
right
autoRowHeight
boolean
Defines whether to set the row height based on the row content. Setting to false can improve loading performance.
true
toolbar
array,selector
The header toolbar of the datagrid panel. Possible values:
| null| onSelect | event | striped | boolean | If set to true, stripe the rows (odd and even rows use different background colors). | false| onSelect | event | method | string | The method type used to request remote data. | post| onSelect | event | nowrap | boolean | If set to true, display the data in a single row. Setting this to true can improve loading performance. | true| onSelect | event | idField | string | Indicates which field is the identifier field. | null| onSelect | event | url | string | The URL to request data from the remote site. | null| onSelect | event | data | array,object | The data to load. This property is available since version 1.3.2.
Code example:
| null| onSelect | event | loadMsg | string | The message displayed when loading data from a remote site. | Processing, please wait …| onSelect | event | pagination | boolean | If set to true, display the pagination toolbar at the bottom of the datagrid. | false| onSelect | event | rownumbers | boolean | If set to true, display a column with row numbers. | false| onSelect | event | singleSelect | boolean | If set to true, only one row can be selected. | false| onSelect | event | checkOnSelect | boolean | If set to true, clicking a row selects/deselects the checkbox. If set to false, the checkbox is selected/deselected only when the checkbox itself is clicked.
This property is available since version 1.3. | true| onSelect | event | selectOnCheck | boolean | If set to true, clicking a checkbox selects the row. If set to false, selecting the row does not select the checkbox.
This property is available since version 1.3. | true| onSelect | event | pagePosition | string | Defines the position of the pagination bar. Available values are: 'top', 'bottom', 'both'.
This property is available since version 1.3. | bottom| onSelect | event | pageNumber | number | Initializes the page number when the pagination property is set. | 1| onSelect | event | pageSize | number | Initializes the page size when the pagination property is set. | 10| onSelect | event | pageList | array | Initializes the page size selection list when the pagination property is set. | [10,20,30,40,50]| onSelect | event | queryParams | object | Extra parameters sent when requesting remote data.
Code example:
| {} | onSelect | event | sortName | string | Defines the column that can be sorted. | null| onSelect | event | sortOrder | string | Defines the sort order of the column; only 'asc' or 'desc' can be used. | asc| onSelect | event | multiSort | boolean | Defines whether multi-column sorting is enabled. This property is available since version 1.3.4. | false| onSelect | event | remoteSort | boolean | Defines whether to sort data on the server. | true| onSelect | event | showHeader | boolean | Defines whether to display the row header. | true| onSelect | event | showFooter | boolean | Defines whether to display the row footer. | false| onSelect | event | scrollbarSize | number | The width of the scrollbar (when vertical) or the height of the scrollbar (when horizontal). | 18| onSelect | event | rowStyler | function | Returns a style such as 'background:red'. This function takes two parameters:
rowIndex: the index of the row, starting from 0.
rowData: the record corresponding to the row.
代码实例:
$('#dg').datagrid({ rowStyler: function(index,row){ if (row.listprice>80){ return 'background-color:#6293BB;color:#fff;'; // return inline style // the function can return predefined css class and inline style // return {class:'r1', style:{'color:#fff'}}; } } });| onSelect | event| onSelect | event | loader | function | Defines how to load data from a remote server. Returning false cancels the action. This function has the following parameters:
param: the parameter object to pass to the remote server.
success(data): the callback function invoked when data is retrieved successfully.
error(): the callback function invoked when data retrieval fails. | json loader| onSelect | event | loadFilter | function | Returns the filtered data to display. This function has one parameter, 'data', which represents the original data. You can convert the original data into a standard data format. The function must return a standard data object with the 'total' and 'rows' properties.
Code example:
// removing 'd' object from asp.net web service json output
$('#dg').datagrid({
loadFilter: function(data){
if (data.d){
return data.d;
} else {
return data;
}
}
});
| onSelect | event| | editors | object | Defines the editor used when editing a row. | predefined editors| onSelect | event | view | object | Defines the view of the datagrid. | default view |
Column Properties
A DataGrid's columns form an array object, each of whose elements is also an array. The elements of that inner array are configuration objects that define the fields of each column.
The width of the column. If not defined, the width expands automatically to fit its content. Not defining a width will reduce performance.
undefined
rowspan
number
Indicates how many rows a cell spans.
undefined
colspan
number
Indicates how many columns a cell spans.
undefined
align
string
Indicates how to align the column's data; can be 'left', 'right', or 'center'.
undefined
halign
string
Indicates how to align the column's header. Possible values: 'left', 'right', 'center'. If no value is assigned, the header alignment matches the data alignment defined by the 'align' property. This property is available since version 1.3.2.
undefined
sortable
boolean
If set to true, the column can be sorted.
undefined
order
string
The default sort order; only 'asc' or 'desc' can be used. This property is available since version 1.3.2.
undefined
resizable
boolean
If set to true, the column can be resized.
undefined
fixed
boolean
If set to true, enable width adjustment when 'fitColumns' is set to true.
undefined
hidden
boolean
If set to true, hide the column.
undefined
checkbox
boolean
Set to true to display checkboxes. Checkboxes have a fixed width.
undefined
formatter
function
The cell formatting function, which takes three parameters:
value: the value of the field.
rowData: the record data of the row.
rowIndex: the index of the row.
代码实例:
$('#dg').datagrid({ columns:[[ {field:'userId',title:'User', width:80, formatter: function(value,row,index){ if (row.user){ return row.user.name; } else { return value; } } } ]] }); | undefined| onSelect | event | styler | function | A style function for the cell that returns a style string to customize the cell's style, for example 'background:red'. This function requires three parameters:
value: the value of the field.
rowData: the record data of the row.
rowIndex: the index of the row.
代码实例:
$('#dg').datagrid({ columns:[[ {field:'listprice',title:'List Price', width:80, align:'right', styler: function(value,row,index){ if (value < 20){ return 'background-color:#ffee00;color:red;'; // the function can return predefined css class and inline style // return {class:'c1',style:'color:red'} } } } ]] }); | undefined| onSelect | event | sorter | function | used for local sorting of custom field of sorting function, requires two parameters:
a: the first field value.
b: the second field value.
代码实例:
$('#dg').datagrid({ remoteSort: false, columns: [[ {field:'date',title:'Date',width:80,sortable:true,align:'center',
sorter:function(a,b){
a = a.split('/');
b = b.split('/');
if (a[2] == b[2]){
if (a[0] == b[0]){
return (a[1]>b[1]?1:-1);
} else {
return (a[0]>b[0]?1:-1);
}
} else {
return (a[2]>b[2]?1:-1);
}
}
} ]] }); | undefined| onSelect | event | editor | string,object | Indicates the editor type. When it is a string, it specifies the editor type; when it is an object, it contains two properties:
type: a string specifying the editor type. Possible types: text, textarea, checkbox, numberbox, validatebox, datebox, combobox, combotree.
options: an object, the editor options corresponding to the editor type. | undefined |
Editor
Override the default editors through $.fn.datagrid.defaults.editors.
Each editor has the following behaviors:
Name
Parameters
Description
init
container, options
Initializes the editor and returns the target object.
Override the default view via $.fn.datagrid.defaults.view.
A view is an object that tells the datagrid how to render rows. The object must define the following functions:
Name
Parameters
Description
render
target, container, frozen
Called when data is loaded.
target:DOM To the象,数据网格(datagrid)To the象。
container:行of容器。
frozen:指示是否呈现冻结容器。 |
| renderFooter | target, container, frozen | This is an option function for rendering the row footer.| onSelect | event | renderRow | target, fields, frozen, rowIndex, rowData | This is an option function that will be called by the render function.| onSelect | event | refreshRow | target, rowIndex | Defines how to refresh the specified row.| onSelect | event | onBeforeRender | target, rows | Fired before the view is rendered.| onSelect | event | onAfterRender | target | Fired after the view is rendered. |
Events
This event extends from the panel; the following are events added for the datagrid.
Name
Parameters
Description
onLoadSuccess
data
Fired when data is loaded successfully.
onLoadError
none
Fired when an error occurs while loading remote data.
onBeforeLoad
param
Fired before the request to load data is sent; returning false cancels the load.
onClickRow
rowIndex, rowData
Fired when the user clicks a row. Parameters include:
| onDblClickRow | rowIndex, rowData | Fired when the user double-clicks a row. Parameters include:
rowIndex: the index of the double-clicked row, starting at 0
rowData: the record corresponding to the double-clicked row | onSelect | event | onClickCell | rowIndex, field, value | Fired when the user clicks a cell. | onSelect | event | onDblClickCell | rowIndex, field, value | Fired when the user double-clicks a cell.
Code example:
// when double click a cell, begin editing and make the editor get focus
$('#dg').datagrid({
onDblClickCell: function(index,field,value){
$(this).datagrid('beginEdit', index);
var ed = $(this).datagrid('getEditor', {index:index,field:field});
$(ed.target).focus();
}
});
| onSelect | event | onSortColumn | sort, order | Fired when the user sorts a column. Parameters include:
sort: the field name of the sorted column
order: the sort order of the column| onSelect | event | onResizeColumn | field, width | Fired when the user resizes a column.| onSelect | event | onSelect | rowIndex, rowData | Fired when the user selects a row. Parameters include:
rowIndex: the index of the selected row, starting at 0
rowData: the record corresponding to the selected row| onSelect | event | onUnselect | rowIndex, rowData | Fired when the user deselects a row. Parameters include:
rowIndex: the index of the deselected row, starting at 0
rowData: the record corresponding to the deselected row| onSelect | event | onSelectAll | rows | Fired when the user selects all rows.| onSelect | event | onUnselectAll | rows | Fired when the user deselects all rows.| onSelect | event | onCheck | rowIndex,rowData | Fired when the user checks a row. Parameters include:
rowIndex: the index of the checked row, starting from 0.
rowData: the record corresponding to the checked row.
This event is available since version 1.3.| onSelect | event | onUncheck | rowIndex,rowData | Fired when the user unchecks a row. Parameters include:
rowIndex: the index of the unchecked row, starting from 0.
rowData: the record corresponding to the unchecked row
This event is available since version 1.3.| onSelect | event | onCheckAll | rows | Fired when the user checks all rows.This event is available since version 1.3.| onSelect | event | onUncheckAll | rows | Fired when the user unchecks all rows.This event is available since version 1.3.| onSelect | event | onBeforeEdit | rowIndex, rowData | Fired when the user starts editing a row. Parameters include:
rowIndex: the index of the edited row, starting from 0.
rowData: the record corresponding to the edited row| onSelect | event | onAfterEdit | rowIndex, rowData, changes | Fired when the user finishes editing a row. The parameters include:
rowIndex: the index of the edited row, starting at 0
rowData: the record corresponding to the edited row
changes: the changed field/value pairs| onSelect | event | onCancelEdit | rowIndex, rowData | Fired when the user cancels editing a row. The parameters include:
rowIndex: the index of the edited row, starting at 0
rowData: the record corresponding to the edited row| onSelect | event | onHeaderContextMenu | e, field | Fired when the datagrid header is right-clicked.| onSelect | event | onRowContextMenu | e, rowIndex, rowData | Fired when a row is right-clicked.
Method
Name
Parameters
Description
options
none
Return the options object.
getPager
none
Return the pager object.
getPanel
none
Return the panel object.
getColumnFields
frozen
Return the column fields. If frozen is set to true, the fields of the frozen columns are returned.
代码实例:
var opts = $('#dg').datagrid('getColumnFields'); // get unfrozen columns var opts = $('#dg').datagrid('getColumnFields', true); // get frozen columns| onSelect | event | getColumnOption | field | Returns the options of the specified column.| onSelect | event | resize | param | Resizes and lays out the columns.| onSelect | event | load | param | Loads and displays the rows of the first page. If the 'param' parameter is specified, it will replace the queryParams property. Typically, this method is called to load new data from the server by passing some query parameters.
| onSelect | event | reload | param | Reload the rows, just like the load method, but stay on the current page. | onSelect | event | reloadFooter | footer | Reload the footer rows. Code example:
// update footer row values and then refresh
var rows = $('#dg').datagrid('getFooterRows');
rows[0]['name'] = 'new name';
rows[0]['salary'] = 60000;
$('#dg').datagrid('reloadFooter');
// update footer rows with new data
$('#dg').datagrid('reloadFooter',[
{name: 'name1', salary: 60000},
{name: 'name2', salary: 65000}
]);
| onSelect | event | loading | none | Show the loading state. | onSelect | event | loaded | none | Hide the loading state.| onSelect | event | fitColumns | none | Make columns expand/collapse automatically to fit the datagrid's width.| onSelect | event | fixColumnSize | field | Fix the size of a column. If the 'field' parameter is not set, all columns will have fixed sizes.
Code example:
$('#dg').datagrid('fixColumnSize', 'name'); // fix the 'name' column size
$('#dg').datagrid('fixColumnSize'); // fix all columns size
| onSelect | event | fixRowHeight | index | Fix the height of the specified row. If the 'index' parameter is not set, all rows will have fixed heights.| onSelect | event | freezeRow | index | Freeze the specified rows so that they are always shown at the top when the datagrid scrolls down. This method is available since version 1.3.2.| onSelect | event | autoSizeColumn | field | Adjust the column width to fit its content. This method is available since version 1.3.| onSelect | event | loadData | data | Load local data; the old rows are removed.| onSelect | event | getData | none | Return the loaded data.| onSelect | event | getRows | none | Return the rows of the current page.| onSelect | event | getFooterRows | none | Return the footer rows.| onSelect | event | getRowIndex | row | Return the index of the specified row. The row parameter can be a row record or the value of an id field.| onSelect | event | getChecked | none | Return all rows selected by checkboxes. This method is available since version 1.3.| onSelect | event | getSelected | none | Return the first selected row, or null.| onSelect | event | getSelections | none | Return all selected rows; an empty array is returned when no records are selected.| onSelect | event | clearSelections | none | Clear all selections.| onSelect | event | clearChecked | none | Clear all checked rows. This method is available since version 1.3.2.| onSelect | event | scrollTo | index | Scroll to the specified row. This method is available since version 1.3.3.| onSelect | event | highlightRow | index | Highlight a row. This method is available since version 1.3.3.| onSelect | event | selectAll | none | Select all rows on the current page.| onSelect | event | unselectAll | none | Deselect all rows on the current page.| onSelect | event | selectRow | index | Select a row; row indices start at 0.| onSelect | event | selectRecord | idValue | Select a row by passing the value of its id as the parameter.| onSelect | event | unselectRow | index | Deselect a row.| onSelect | event | checkAll | none | Check all rows on the current page. This method is available since version 1.3.| onSelect | event | uncheckAll | none | Uncheck all rows on the current page. This method is available since version 1.3.| onSelect | event | checkRow | index | Check a row; row indices start at 0. This method is available since version 1.3.| onSelect | event | uncheckRow | index | Uncheck a row; row indices start at 0. This method is available since version 1.3.| onSelect | event | beginEdit | index | Begin editing a row.| onSelect | event | endEdit | index | End editing a row.| onSelect | event | cancelEdit | index | Cancel editing a row.| onSelect | event | getEditors | index | Get the editors of the specified row. Each editor has the following properties:
actions: the actions the editor can perform, the same as in the editor definition.
target: the jQuery object of the target editor.
field: the field name.
type: the editor type, for example: 'text', 'combobox', 'datebox', etc.| onSelect | event | getEditor | options | Get the specified editor. The options parameter contains two properties:
index: the row index.
field: the field name.
代码实例:
// get the datebox editor and change its value var ed = $('#dg').datagrid('getEditor', {index:1,field:'birthday'}); $(ed.target).datebox('setValue', '5/4/2012');| onSelect | event | refreshRow | index | Refresh a row.| onSelect | event | validateRow | index | Validate the specified row; returns true when valid.| onSelect | event | updateRow | param | Update the specified row. The param parameter includes the following properties:
index: the index of the row to update.
row: the new row data.
代码实例:
$('#dg').datagrid('updateRow',{ index: 2, row: { name: 'new name', note: 'new note message' } });| onSelect | event | appendRow | row | Append a new row. The new row is added at the end:
| onSelect | event | insertRow | param | Insert a new row. The param parameter includes the following properties:
index: the index of the row to insert. If not defined, the new row is appended.
row: the row data.
代码实例:
// insert a new row at second row position $('#dg').datagrid('insertRow',{ index: 1, // index start with 0 row: { name: 'new name', age: 30, note: 'some messages' } });| onSelect | event | deleteRow | index | Delete a row.| onSelect | event | getChanges | type | Get the rows changed since the last commit. The type parameter indicates the type of changed rows; possible values are: inserted, deleted, updated, etc. When the type parameter is not assigned, all changed rows are returned.| onSelect | event | acceptChanges | none | Commit all changed data since loading or since the last call to acceptChanges.| onSelect | event | rejectChanges | none | Roll back all changed data since creation or since the last call to acceptChanges.| onSelect | event | mergeCells | options | Merge several cells into one. The options parameter includes the following properties:
index: the column index.
field: the field name.
rowspan: the number of rows the merge spans.
colspan: the number of columns the merge spans.| onSelect | event | showColumn | field | Show the specified column.| onSelect | event | hideColumn | field | Hide the specified column. |