Home  >  Article  >  Web Front-end  >  jQuery LigerUI usage tutorial form (1)_jquery

jQuery LigerUI usage tutorial form (1)_jquery

WBOY
WBOYOriginal
2016-05-16 17:56:571359browse

The first example
Introduction
ligerGrid is the core control of the ligerui series of plug-ins. Users can quickly create a beautiful and powerful table that supports sorting, paging, multiple headers, fixed columns, etc.

Supports local data and server data (configuration data or url)
Supports sorting and paging (including Javascript sorting and paging)
Supports "show/hide" of columns
Supports multiple table headers
Support fixed columns
Support detail rows
Support summary rows
Support cell templates
Support table editing (three editing modes, cell editing, row editing, detail editing)
Support tree table
Support grouping
Code
First introduce basic css and js files

Copy code Code As follows:





Then you can use liger Grid
Copy the code The code is as follows:




Effect
Data structure
Table data structure
Table data has two attributes, one is Rows and the other is Total. Among them, Rows is a data array and Total is the total number of records.
In fact, Total does not need to provide it when using local data. The format we use local data can be like this:
Copy code The code is as follows:

{
Rows: [
{ id: '01', name: 'Department 01' },
{ id: '02', name: 'Department 02' },
{ id: '03', name: 'Department 03' },
{ id: '04', name: 'Department 04' },
{ id: '05', name: 'Department 05' },
{ id: '06', name: 'Department 06' },
{ id: '07', name: 'Department 07' }
]
}

id, name both It is a record attribute, which can be customized at will. When configuring columns, you do not have to configure the corresponding columns, you only need to configure the corresponding displayed columns. In subsequent operations, these properties can be obtained. For example, the method getSelected(). Custom cell function render.
Tree table data structure
The tree structure engineer adds a children parameter based on the table data, such as:
Copy code The code is as follows:

{
Rows: [
{ id: '01', name: 'Department 01', children: [
{ id : '0101', name: 'Department 0101' },
{ id: '0102', name: 'Department 0102' },
{ id: '0103', name: 'Department 0103' }
]
},
{ id: '02', name: 'Department 02' },
{ id: '03', name: 'Department 03' },
{ id: ' 04', name: 'Department 04' },
{ id: '05', name: 'Department 05' },
{ id: '06', name: 'Department 06' },
{ id: '07', name: 'Department 07' }
]
}

Two ways to bind data
ligerGrid has two ways to bind data, One is to use local data, and the other is to use server data.
In the first example, we configured the data parameter, this way is the local way. Another way is to configure url parameters and use remote data.

Configure column
How many columns are displayed in the table, column width, and the content to be displayed in the column cells are all configured by the columns attribute. The following are the configuration parameters of column:
Copy code The code is as follows:

{
display: 'serial number', //The text displayed in the header column, supports html
//Custom function for header content
headerRender: function (column) {
return "" column.display "";
},
name: 'id', //The row data attribute of cell mapping
align: 'center' , //Cell content alignment: left/center/right
hide: false, //Whether to hide
width: 100, //The width of the column
minWidth: 50, //The minimum width of the column
isSort: true, //Whether this column is allowed to be sorted, the default is to allow sorting
isAllowHide: true, //Whether hiding is allowed, if allowed, it will appear in the [Show/Hide column right-click menu]
type: 'string', //Type, used for sorting
//Custom cell renderer
render: function (record, rowindex, value, column) {
//This here points to grid
//record row data
//rowindex row index
//value current value, corresponding to record[column.name]
//column column information
return value; //return this HTML content displayed in the cell (generally organized according to value and row content)
},
//Column summary
totalSummary: {
align: 'center', //Summary cell content Alignment: left/center/right
type: 'count', //Summary type sum, max, min, avg, count. Can multiple types at the same time
render: function (e) { //Summary renderer, return html to load into the cell
//e Summary Object (including sum, max, min, avg, count)
return "
Total count:" e.count "
";
}
},
//Cell editor
editor: {
type: ' text'
},
//Multiple table headers support
columns: null
},

The columns of the table provide a very complete interface that can be expanded. Whether it is content cells or header cells, the content, layout, and size can be customized.
Custom header
For example, in the header, we can set the display directly to a piece of html:
Copy code Code As follows:

{
display: 'Department', //The text displayed in the header column, Support html
name: 'name',
align: 'left'
},

or use headerRender:
Copy code The code is as follows:

//Header content custom function
headerRender: function (column) {
return "< b>" column.display "";
},

Rendering

Custom cell
The name of column is the attribute that defines which cell is linked to the row data. For example, in the first line of the above example, if name is configured as id, then it should be displayed as "01". If it is configured as name, then it should be displayed as "department 01". There is also the align parameter, which determines the alignment of the cells.

If render is not configured, the content displayed in the cell will be determined by name.

Copy code The code is as follows:

{ name: 'id', display: 'Serial number', width: 200 },
{ name: 'name', display: 'name', width: 300 }


The above is the cell The default display mode. In addition to this method, formatters and custom functions can also be used.
Display rules for cell content:
, if render is configured, use render
. If the type parameter of column extends the corresponding formatter, then use the formatter for rendering. For example, the formatter
that defines the currency format is finally used. The default display mode
formatter
is implemented by extending $.ligerDefaults.Grid.formatters['columntype']. columntype is the column configuration. The type parameter. For example, if you want to format a currency format now:
Copy the code The code is as follows:

$.ligerDefaults.Grid.formatters['currency'] = function (num, column) {
//num current value
//column column information
if (!num ) return "$0.00";
num = num.toString().replace(/$|,/g, '');
if (isNaN(num))
num = "0.00";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num * 100 0.50000000001);
cents = num % 100;
num = Math .floor(num / 100).toString();
if (cents < 10)
cents = "0" cents;
for (var i = 0; i < Math.floor(( num.length - (1 i)) / 3); i )
num = num.substring(0, num.length - (4 * i 3)) ','
num.substring(num.length - (4 * i 3));
return "$" (((sign) ? '' : '-') '' num '.' cents);
};


In this way, as long as the column type is configured as currency. This function will be used to customize cell content
Copy code The code is as follows:

{ display: 'UnitPrice', name: 'UnitPrice', align: 'right' ,type:'currency' }

Rendering


Custom cell function
Custom cell function refers to configuring the render of column. We can organize arbitrary html.

Copy code The code is as follows:

var grid = $("#maingrid").ligerGrid ({
columns: [
{ name: 'id', display: 'serial number', width: 100,
render: function (record, rowindex, value, column) {
//this This points to grid
//record row data
//rowindex row index
//value current value, corresponding to record[column.name]
//column column information
return "< ;a href='edit.htm?id=" value "'>Edit";
}
},
{ name: 'id', display: 'serial number', width: 120,
render: function (record, rowindex, value, column) {
return '';
}
},
{ name: 'name', display: 'name', width: 300 }
],
data: { Rows: griddata }
});

Rendering


Cell Editor
All editor constructs are defined in $.ligerDefaults.Grid.editors, such as

Copy code The code is as follows:

editor: { type: 'spinner' }

will use $ .ligerDefaults.Grid.editors['spinner'] to create editors to build.

ligerGrid provides built-in editors such as check boxes, text boxes, dates, numeric adjusters, and drop-down boxes.

Rendering

There are many parameters for column. I will not list them all here. I only introduce a few commonly used parameters
For more, you can view the API: http:// api.ligerui.com
Sort and paging
There are also two ways of sorting and paging. One is local sorting and paging. One is server sorting and paging. Only local methods are introduced here.
The default situation. Sorting and paging are enabled. If you want to cancel the paging function, as follows:
Copy code The code is as follows:

usePager: false

效果图

事件和方法
事件
事件名 参数 描述
onAfterAddRow (e) 增加行后事件
onAfterBeginEdit (e) 开始编辑后事件
onAfterChangeColumnWidth (column, newwidth) 改变列宽度事件
onAfterShowData (data) 显示完数据事件
onAfterSubmitEdit (e) 提交编辑 事件
onBeforeChangeColumnWidth (column, newwidth) 验证 改变列宽度 是否通过
onBeforeCheckAllRow (checked, grid element) 选择前事件,可以通过return false阻止操作(复选框 全选/全不选)
onBeforeEdit (e) 编辑前事件
onBeforeShowData (data) 显示数据前事件,可以通过reutrn false阻止操作
onBeforeSubmitEdit (e) 验证编辑器结果是否通过
onBeginEdit (e) 验证 开始编辑 是否通过
onCancelEdit (e) 取消编辑 事件
onChangeSort () 改变排序事件
onCheckAllRow (checked, grid element) 选择事件(复选框 全选/全不选)
onCheckRow (checked, rowdata, rowindex, rowDomElement) 选择事件(复选框)
onContextmenu (parm, e) 右击事件
onDblClickRow (rowdata, rowindex, rowDomElement) 双击行事件
onDragCol (node) 拖动列事件
onError () 错误事件
onLoaded () 加载完函数
onLoading () 加载时函数
onReload () 刷新事件,可以通过return false来阻止操作
onSelectRow (rowdata, rowindex, rowDomElement) 选择行事件
onSubmit () 提交前事件
onSuccess () 成功事件
onToFirst () 第一页,可以通过return false来阻止操作
onToggleCol () 切换列事件
onToLast () 最后一页,可以通过return false来阻止操作
onToNext () 下一页,可以通过return false来阻止操作
onToPrev () 上一页,可以通过return false来阻止操作
onUnSelectRow (rowdata, rowindex, rowDomElement) 取消选择行事件

Example

Copy code The code is as follows:

var grid = $("#maingrid").ligerGrid({
columns: [
{ name: 'id', display: 'serial number', width: 200 },
{ name: 'name', display : 'name', width: 300 }
],
data: { Rows: griddata },
onSelectRow: function (rowdata, rowindex) {
//Row records for data rows
//Row index, starting from 0
alert(rowdata.name);
}
});
grid.bind('SelectRow', function (rowdata, rowindex) {
//this This here all points to the grid

//The row record is for the data row
//The row index starts from 0
alert(rowdata.name);
});

方法
方法 参数 描述
addEditRow (rowdata)
  • 增加一个编辑行
addRow (rowdata, rowParm, isBefore, parentRow)
  • 增加新行
addRows (rowdataArr)
  • 一次性增加多行
appendRow (rowData, targetRow, nearRow, isBefore)
  • 附加新行(树模式)
beginEdit (rowParm)
  • 进入编辑状态
cancelEdit (rowParm)
  • 取消编辑
changeHeaderText (columnparm, headerText)
  • 改变表头文本
changePage (ctype)
  • 改变分页
changeSort (columnName, sortOrder)
  • 改变排序
collapse (targetRow)
  • 收缩(树模式)
collapseDetail (rowParm)
  • 收缩明细
deleteRow (rowParm)
  • 选择行
deleteSelectedRow ()
  • 删除选择的行
demotion (targetRow)
  • 降级(树模式)
endEdit (rowParm)
  • 结束编辑
expand (targetRow)
  • 展开(树模式)
extendDetail (rowParm)
  • 展开明细
formatRecord (record)
  • 格式化数据,删除系统字段
getAdded ()
  • 获取新增的数据
getCheckedRowObjs ()
  • 获取选中的行 DOM对象集合
getCheckedRows ()
  • 获取选中的行数据(复选框)
getChidren (rowParm)
  • 获取子节点数据(树模式)
getColumn (columnpam)
  • 获取列信息
getColumns (columnLevel)
  • 获取指定层级的Columns
getColumnType (columnname)
  • 根据列名获取列类型
getData (status, removeStatus)
  • 获取数据
getDeleted ()
  • 获取删除过的数据
getParent (rowParm)
  • 获取父节点数据(树模式)
getRowObj (rowParm)
  • 行DOM转换为行数据
getSelected ()
  • 获取选中的行数据(同getSelectedRow)
getSelectedRow ()
  • 获取选中的行数据
getSelectedRowObj ()
  • 获取选中的行 DOM对象
getSelectedRowObjs ()
  • 获取选中的行 DOM对象集合
getSelectedRows ()
  • 获取选中的行数据集合(支持Ctrl多选)
getSelecteds ()
  • 获取选中的行数据集合(支持Ctrl多选)(同getSelectedRows)
getUpdated ()
  • 获取修改过的数据
hasChildren (rowParm)
  • 是否包括子节点(树模式)
isLeaf (rowParm)
  • 是否叶节点(树模式)
isTotalSummary ()
  • 是否包含汇总
loadData (loadDataParm)
  • 刷新数据
loadServerData (param, clause)
  • 加载数据(服务器)
reRender (e)
  • 重新加载html
setColumnWidth (columnparm, value)
  • 调整列宽
setOptions (parms)
  • 重新设置参数(同名方法set)
SubmitEdit (rowParm)
  • 提交编辑
toggle (targetRow)
  • 伸展/收缩节点(树模式)
toggleCol (columnparm, visible)
  • 显示/隐藏列
updateCell (cell, value, rowParm)
  • 更新单元格
updateRow (newRowData, rowDom)
  • Update row
upgrade (targetRow)
  • Upgrade (tree mode)
Example
Copy code The code is as follows:

Copy Code The code is as follows:

var grid = $("#maingrid").ligerGrid({
columns: [
{ name: 'id ', display: 'serial number', width: 200 },
{ name: 'name', display: 'name', width: 300 }
]
});

grid .set({ data: { Rows: griddata} });

function selectRow(index) {
grid.select(index);
}
function getSelectRow() {
var rows = grid.getSelecteds();
for (var i in rows) {
alert(rows[i].name);
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn