Using Flexigrid simple FAQ

本文详细介绍了Flexigrid网格框架的高级应用,包括动态列、参数传递到服务器、获取选中行、鼠标事件、多过滤器和格式化单元格等功能。通过实例展示了如何动态调整列、使用参数进行服务器调用、获取并处理选中行数据、实现鼠标事件监听以及应用复杂过滤条件。同时,还介绍了如何格式化和个性化单元格显示。
[size=medium]From:http://code.google.com/p/flexigrid/wiki/FAQ

Frequently Asked Questions
Typical Grid and Debug tools

In these examples, we use a typical Grid:

var myFlex = {
autoload : false,
dataType : 'json',
url : 'grid.php',
usepager : true
};

$('#grid01').flexigrid($.extend({}, myFlex, {
colModel : [
{name: 'name', display: 'NAME', sortable: true},
{name: 'status', display: 'STATUS', sortable: true},
{name: 'sign', display: 'SIGN', sortable: true}
]
}));

For debug we use console.log() more likely than alert(), which is not

always work as expected.
Examples

#Dynamic_columns_and_Post_parameter_to_server
#Get_selected_row
#Mouseclick_and_other_events
#Multiple_filters
#Format_cells

Dynamic columns and Post parameter to server

Colmodel define in variable. Parameters send to server by

.flexOptions() on onSubmit().

var colModel01 = [
{name: 'name', display: 'NAME', sortable: true},
{name: 'status', display: 'STATUS', sortable: true},
{name: 'sign', display: 'SIGN', sortable: true}
];

$('#grid01').flexigrid($.extend({}, myFlex, {
colModel : colModel01,
onSubmit : function(){
$('#grid01').flexOptions({params: [
{name:'colls', value: $.param({colls: $.map(colModel01,

function(elem,id){return elem.name}) }) }
]});
return true;
}
}));

In this code we post to server on load next data (with column names):

page=1&rp=15&sortname=undefined&sortorder=undefined&query=&qtype=&
colls=colls%255B%255D%3Dname%26colls%255B%255D%3Dstatus%26colls%255B
%255D%3Dsign

Get selected row

The difficulty lies in linking: column_name - data. That is why the

order of columns can be changed, and we can not use index in the

array of row cells. In flexigrid-1.1 we have the attribute 'abbr' for

sortable table cells (issue 46).

$('#grid01').click(function(event){
$('.trSelected', this).each( function(){
console.log(
' rowId: ' + $(this).attr('id').substr(3) +
' name: ' + $('td[abbr="name"] >div', this).html() +
' sign: ' + $('td[abbr="sign"] >div', this).html() +
' status: ' + $('td[abbr="status"] >div', this).html()
);
});
});

Mouseclick and other events

As of jQuery 1.7, the .on() method provides all functionality

required for attaching event handlers.

$('.flexigrid').on('mouseenter', 'tr[id*="row"]', function(){
console.log('mouseenter rowId: ' + $(this).attr('id').substr(3));
});

With older jQuery: If we bind the event to the cells, we have to use

the .live() or .delegate(). Because it when receiving data grid

content is recreated.

$('.flexigrid tr[id*="row"]').live('mouseenter', function(){
console.log('mouseenter rowId: ' + $(this).attr('id').substr(3));
});

Multiple filters

Just add a custom filter form

<form id="fmFilter">
<input id="fmFilterSel1" name="fmFilterSel1" type="checkbox" />
<input id="fmFilterSel2" name="fmFilterSel2" type="checkbox" />
<input id="fmFilterSel3" name="fmFilterSel3" type="text" />
</form>
<table id="grid01"><tr><td></td></tr></table>

<script>
$('#grid01').flexigrid($.extend({}, myFlex, {
onSubmit : function(){
$('#grid01').flexOptions({params: [{name:'callId',

value:'grid01'}].concat($('#fmFilter').serializeArray())});
return true;
}
});
</script>

And you send to server the filter condition of any complexity.
Format cells

You can colorize the cells, and even change the output according to

the received data.

function gridFormat() {
var lblStatus = {
'101' : {
css : '',
txt : 'STATUS_OK'
},
'102' : {
css : 'cellDisable',
txt : 'STATUS_LOCK'
},
'103' : {
css : 'cellWarning',
txt : 'STATUS_BAD'
}
};
$('#grid01 tr').each( function(){
var cell = $('td[abbr="status"] >div', this);
$(this).addClass( lblStatus[cell.text()].css );
cell.text( lblStatus[cell.text()].txt );
});
return true;
}

$('#grid01').flexigrid($.extend({}, myFlex, {
buttons : [
{name: 'CLEAR', onpress: function(com,grid){

$('#grid01').flexAddData({rows:[],page:0,total:0}); }},
{name: 'FILL', onpress: function(com,grid){

$('#grid01').flexAddData({rows:[
{id:'id0',cell:['name00',101,'A']},
{id:'id1',cell:['name01',102,'B']},
{id:'id2',cell:['name02',103,'C']}
],page:1,total:3}); }}
],
onSuccess : gridFormat
}); [/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值