jquery easyui DataGrid

 


Usage

Markup

<table id="tt">table>

jQuery

$('#tt').datagrid(options);

The DataGrid data format sample

{"total":28,"rows":[{"productid":"FI-SW-01","unitcost":10.00,"status":"P","listprice":16.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":58.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":93.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":93.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"AV-CB-01","unitcost":92.00,"status":"P","listprice":193.50,"attr1":"Adult Male","itemid":"EST-18"}]}

Dependencies

  • panel
  • resizable
  • linkbutton
  • pagination

Options

Extend from $.fn.panel.defaults.
Override defaults with $.fn.datagrid.defaults.

DataGrid Properties

The properties extend from panel. below is the added properties for datagrid.

Name Type Description Default
columns array The datagrid columns config object, see column properties for more details. null
frozenColumns array Same as the columns property, but the these columns will be frozen on left. null
fitColumns boolean True to auto expand/contract the size of the columns to fit the grid width and prevent horizontal scrolling. false
striped boolean True to stripe the rows. false
method string The method type to request remote data. post
nowrap boolean True to display data in one line. true
idField string Indicate which field is an identity field. null
url string A URL to request data from remote site. null
loadMsg string When loading data from remote site, show a prompt message. Processing, please wait …
pagination boolean True to show a pagination toolbar on datagrid bottom. false
rownumbers boolean True to show a row number column. false
singleSelect boolean True to allow selecting only one row. false
pageNumber number When set pagination property, initialize the page number. 1
pageSize number When set pagination property, initialize the page size. 10
pageList array When set pagination property, initialize the page size selecting list. [10,20,30,40,50]
queryParams object When request remote data, sending additional parameters also. {}
sortName string Defines which column can be sorted. null
sortOrder string Defines the column sort order, can only be 'asc' or 'desc'. asc
remoteSort boolean Defines if to sort data from server. true
editors object Defines the editor when editing a row. predefined editors
view object Defines the view of datagrid. default view

Column Properties

The DataGrid Columns is an array object, which element is an array too.
The element of element array is a config object, which defines every column field.
code example:

columns:[[
    {field:'itemid',title:'Item ID',rowspan:2,width:80,sortable:true},
    {field:'productid',title:'Product ID',rowspan:2,width:80,sortable:true},
    {title:'Item Details',colspan:4}
],[
    {field:'listprice',title:'List Price',width:80,align:'right',sortable:true},
    {field:'unitcost',title:'Unit Cost',width:80,align:'right',sortable:true},
    {field:'attr1',title:'Attribute',width:100},
    {field:'status',title:'Status',width:60}
]]
Name Type Description Default
title string The column title text. undefined
field string The column field name. undefined
width number The width of column. undefined
rowspan number Indicate how many rows a cell should take up. undefined
colspan number Indicate how many columns a cell should take up. undefined
align string Indicate how to align the column data. 'left','right','center' can be used. undefined
sortable boolean True to allow the column can be sorted. undefined
checkbox boolean True to show a checkbox. undefined
formatter function The cell formatter function. take three parameter:
value: the field value.
rowData: the row record data.
rowIndex: the row index.
undefined
sorter function The custom field sort function, take two parameters:
a: the first field value.
b: the second field value.
undefined
editor string,object Indicate the edit type. When string indicates the edit type, when object contains two properties:
type: string, the edit type, possible type is: text,textarea,checkbox,numberbox,validatebox,datebox,combobox,combotree.
options: object, the editor options corresponding to the edit type.
undefined

Editor

Override defaults with $.fn.datagrid.defaults.editors.
Every editor has following actions:

Name Parameters Description
init container, options Initialize the editor and return the target object.
destroy target Destroy the editor if necessary.
getValue target Get value from editor text.
setValue target , value Set value for editor.
resize target , width Resize the editor if necessary.

For example, the text editor is defined as following:

$.extend($.fn.datagrid.defaults.editors, {
    text: {
        init: function(container, options){
            var input = $('').appendTo(container);
            return input;
        },
        getValue: function(target){
            return $(target).val();
        },
        setValue: function(target, value){
            $(target).val(value);
        },
        resize: function(target, width){
            var input = $(target);
            if ($.boxModel == true){
                input.width(width - (input.outerWidth() - input.width()));
            } else {
                input.width(width);
            }
        }
    }
});

DataGrid View

Override defaults with $.fn.datagrid.defaults.view.
The view is an object that will tell datagrid how to render rows. The object must defines the following functions:

Name Parameters Description
render target, container, frozen Called when the data is loaded.
target: DOM object, the datagrid object.
container: the rows container.
frozen: indicate if to render the frozen container.
renderRow target, fields, frozen, rowIndex, rowData This is an option function and will be called by render function.
refreshRow target, rowIndex Defines how to refresh the specified row.
onBeforeRender target, rows Fires before the view is rendered.
onAfterRender target Fires after the view is rendered.

Events

The events extend from panel, below is the added events for datagrid.

Name Parameters Description
onLoadSuccess data Fires when data is loaded successfully.
onLoadError none Fires when some error occur to load remote data.
onBeforeLoad param Fires before a request is made to load data. If return false the load action will be canceled.
onClickRow rowIndex, rowData Fires when user click a row, the parameters contains:
rowIndex: the clicked row index, start with 0
rowData: the record corresponding to the clicked row
onDblClickRow rowIndex, rowData Fires when user dblclick a row, the parameters contains:
rowIndex: the clicked row index, start with 0
rowData: the record corresponding to the clicked row
onSortColumn sort, order Fires when user sort a column, the parameters contains:
sort: the sort column field name
order: the sort column order
onResizeColumn field, width Fires when user resize the column.
onSelect rowIndex, rowData Fires when user select a row, the parameters contains:
rowIndex: the selected row index, start with 0
rowData: the record corresponding to the selected row
onUnselect rowIndex, rowData Fires when user unselect a row, the parameters contains:
rowIndex: the unselected row index, start with 0
rowData: the record corresponding to the unselected row
onSelectAll rows Fires when user select all rows.
onUnselectAll rows Fires when user unselect all rows.
onBeforeEdit rowIndex, rowData Fires when user start editing a row, the parameters contains:
rowIndex: the editing row index, start with 0
rowData: the record corresponding to the editing row
onAfterEdit rowIndex, rowData, changes Fires when user finish editing, the parameters contains:
rowIndex: the editing row index, start with 0
rowData: the record corresponding to the editing row
changes: the changed field/value pairs
onCancelEdit rowIndex, rowData Fires when user cancel editing a row, the parameters contains:
rowIndex: the editing row index, start with 0
rowData: the record corresponding to the editing row
onHeaderContextMenu e, field Fires when the header of datagrid is right clicked.
onRowContextMenu e, rowIndex, rowData Fires when a row is right clicked.

Methods

Name Parameter 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 setted to true the frozen column fields is returned.
getColumnOption field Return the specified column option.
resize param Do resize and do layout.
reload param Reload the rows. If the 'param' is specified, it will replace with the queryParams property.
fitColumns none Make columns auto expand/collapse to fit datagrid width.
fixColumnSize none fix columns size.
fixRowHeight index fix the specified row height.
loadData data Load local data, the old rows will be removed.
getData none Return the loaded data.
getRows none Return the current page rows.
getRowIndex row Return the specified row index, the row parameter can be a row record or an id field value.
getSelected none Return the first selected row record or null.
getSelections none Return all selected rows, when no record selected, am empty array will return.
clearSelections none Clear all selections.
selectAll none Select all current page rows.
unselectAll none Unselect all current page rows.
selectRow index Select a row, the row index start with 0.
selectRecord idValue Select a row by passing id value parameter.
unselectRow index Unselect a row.
beginEdit index Begin editing a row.
endEdit index End editing a row.
cancelEdit index Cancel editing a row.
getEditors index Get the specified row editors. Each editor has the following properties:
actions: the actions that the editor can do.
target: the target editor jQuery object.
field: the field name.
type: the editor type.
getEditor options Get the specified editor, the options contains two properties:
index: the row index.
field: the field name.
refreshRow index Refresh a row.
validateRow index validate the specified row, return true when valid.
appendRow row Append a new row.
deleteRow index Delete a row.
getChanges type Get changed rows since the last commit. The type parameter indicate which type changed rows, possible value is: inserted,deleted,updated,etc. When the type parameter is not assigned, return all changed rows.
acceptChanges none Commits all the changes data since it was loaded or since the last time acceptChanges was called.
rejectChanges none Rolls back all the changes data since it was created, or since the last time acceptChanges was called.
mergeCells options Merge some cells to one cell, the options contains following properties:
index: the row index.
field: the field name.
rowspan: the rowspan count to be merged.
colspan: the colspan count to be merged.
showColumn field Display the specified column.
hideColumn field Hide the specified column.

http://jquery-easyui.wikidot.com/document:datagrid

 

你可能感兴趣的:(jquery easyui DataGrid)