本文转载,原文:http://blog.csdn.net/chemmuxin1993/article/details/52515131
1. 传送门:js-grid官网
2. 引入css:
<link type='text/css' rel='stylesheet' href='jsgrid.min.css' /><link type='text/css' rel='stylesheet' href='jsgrid-theme.min.css' />
3. 引入Js:
<script type="text/javascript" src="jsgrid.min.js"></script>
4.配置
var clients = [ { "Name": "Otto Clay", "Age": 25, "Country": 1, "Address": "Ap #897-1459 Quam Avenue", "Married": false }, { "Name": "Connor Johnston", "Age": 45, "Country": 2, "Address": "Ap #370-4647 Dis Av.", "Married": true }, { "Name": "Lacey Hess", "Age": 29, "Country": 3, "Address": "Ap #365-8835 Integer St.", "Married": false }, { "Name": "Timothy Henson", "Age": 56, "Country": 1, "Address": "911-5143 Luctus Ave", "Married": true }, { "Name": "Ramona Benton", "Age": 32, "Country": 3, "Address": "Ap #614-689 Vehicula Street", "Married": false }];var countries = [ { Name: "", Id: 0 }, { Name: "United States", Id: 1 }, { Name: "Canada", Id: 2 }, { Name: "United Kingdom", Id: 3 }];$("#jsGrid").jsGrid({ width: "100%", height: "400px", inserting: true, editing: true, sorting: true, paging: true, data: clients, fields: [ { name: "Name", type: "text", width: 150, validate: "required" }, { name: "Age", type: "number", width: 50 }, { name: "Address", type: "text", width: 200 }, { name: "Country", type: "select", items: countries, valueField: "Id", textField: "Name" }, { name: "Married", type: "checkbox", title: "Is Married", sorting: false }, { type: "control" } ]});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
Fields: data: autoload: false, controller:{ loadData: Function, insertItem: Function, uodateItem: Function, deleteItem: Function } width: "auto", height: "auto", heading: true filtering: inserting: editing: selecting: false, sorting: paging: pageLoading: rowClass: function(item, index){ } rowClick: function(args){ args = {item, itemIndex, even} } rowDoubleClick: function(args){ }, noDataContent: confirmDeleting: deleteConfirm: pagerContainer: pageIndex: 1, pageSize: 20, pageButtonCount: 15, pagerFormat: "Pages: {first} {prev} {pages} {next} {last} {pageIndex} of {pageCount}" pagePrevText: "Prev", pageNextText: "Next", pageFirstText: "First", pageLastText: "Last", invalidNotify: function(args){ args = { error[] , item , itemIndex , row } } invalidMessage: "Invalid data enterd!" , updateOnResize: true, rowRenderer: null, headerRowRenderer: null, filterRowRenderer: null, insertRowRenderer: null, editRowRenderer: null
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
Field支持配置的字段:
type: "" //可选 "text"|"number"|"checkbox"|"select"|"textarea"|"control" 更多见扩展2 , name: "" //从data中取得的字段名 , title: "" //显示在表头的字段名 , align: "" //可选 "left"|"center"|"right", width: 100, visible: true //可用, css: "" //表格展示的css样式,需先定义一个class类型的css,将字符串声明在此,将会被拼接进所在列的每个td , headercss: "" //表头css,同上 , filtercss: "" //查找框css,同上 , insertcss: "" //插入时输入框css,同上 , editcss: "" //编辑时css,同上 , filtering: true //可查 , inserting: true //可插入 , editing: true //可编辑 , sorting: //可排序 , sorter: "string" , headTemplate: function(){ } itemTemplate: function(value, item){ return string; } filterTemplate: function(){ }, insertTemplate: function(){ }, editTemplate: function(value, item){ } filterValue: function(){}, insertValue: function(){}, editValue: function(){} cellRenderer: function(value, item){ } validate: string|object|array|function, //数据校验, 支持的有 required 必填, rangeLength 校验长度object --》 {validator: "rangeLength", param: 16} , range 校验范围object --》 {validator: "range", param: [21, 80]}, minLength, maxLength, min, max, pattern. jsGrid.validators.time = { message: "Please enter a valid time", validator: function(value, item){ return /^([01]\d|2[0-3]|[0-9])(:[0-5]\d){1,2}$/.test(value); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
5. 扩展1 关于Controller
如果都返回JQuery promise,表格将支持异步通知状态,例如删除,会在删除ajax回调成功时做出反应。var promise = $.ajax({}); 那么promise会有done方法,done方法在ajax请求完成时得到执行。所谓promise()作为一个对象的活动集合,ajax将直接返回promise的对象,其它支持的类型可以调用诸如$(“div”).promise()的方法。
loadData 查
loadData: function(filter){ return $.ajax({ type: "get", url: "items", data: filter }); }
pageLoading为true时,filter有pageSize和pageIndex两个参数,sorting为true时,filter有sortFiled和sortOrder两个参数,return一个表格加载的data或者当分页启动时,return如下形式:
{ data itemsCount }
insertItem 增
insertItem: function(item){ return $.ajax({ type: "post", url: "items", data: item }); }
updateItem 改
updateItem: function(item){ return $.ajax({ type: "put", url: "items", data: item }); }
返回已更改的item用于更新表数据,否则以提交更改的item作为更新。
deleteItem 删
deleteItem: function(item){ return $.ajax({ type: "delete", url: "items", data: item }); }
6. 扩展2 关于type
text,filed支持多余的以下字段:
readOnly: false, //是否只读,true时,即使编辑也将不可更改。 autosearch: true, //原意应该是搜索时按回车自动定位,但测试貌似没有生效,未知。
number:
sorter: "number", //使用number分类 align: "right", //居右 readOnly: false
select:
{ align: "center", autosearch: true, items: [], //用于生成器的数组 valueField: "", //items中映射data字段的值的字段,参照开头例子 textFiled: "", //data中将被items替换显示的字段 seletedIndex: -1, //默认选择的index valueType: "number|string", 数据类型 readOnly: false }
checkbox:
{ sorter:"number", align:"center", autosearch: true }
control 非表单字段,该列显示各种控制按钮:
{ editButton: true, deleteButton: true, clearFilterButton: true, modeSwitchButton: true, align: "center", width: 50, filtering: false, inserting: false, editing: false, sorting: false, searchModeButtonTooltip: "Switch to searching", //搜索按钮悬停提示 insertModeButtonTooltip: "Switch to insertin", //插入按钮悬停提示 editButtonTooltip: "Edit", ... //各种提示 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
7.自定义字段:
引用官网一个例子:
var MyDateField = function(config) { jsGrid.Field.call(this, config);};MyDateField.prototype = new jsGrid.Field({ css: "date-field", // redefine general property 'css' align: "center", // redefine general property 'align' sorter: function(date1, date2) { return new Date(date1) - new Date(date2); }, itemTemplate: function(value) { return new Date(value).toDateString(); }, insertTemplate: function(value) { return this._insertPicker = $("<input>").datepicker({ defaultDate: new Date() }); }, editTemplate: function(value) { return this._editPicker = $("<input>").datepicker().datepicker("setDate", new Date(value)); }, insertValue: function() { return this._insertPicker.datepicker("getDate").toISOString(); }, editValue: function() { return this._editPicker.datepicker("getDate").toISOString(); }});jsGrid.fields.date = MyDateField;