主要功能对DataGrid实现翻页,数据源是DataSet,希望对大家有用
/**
@class detachShowData
@package js
@author JinSong
@version 1.0.0
@method showData(),previousPage(),nextPage(),selectPage(),getCurrentPage(),getTotalPage(),getDataSet(),setDataSet(), getDataGrid(),setDataGrid(),getEveryPageShowDataNum(),SetEveryPageShowDataNum()
@example
import js.detachShowData;
var current_txt; //display current page number.
var total_txt; //display total pages number.
var dsd = new detachShowData(my_DS,my_DGD,null,//if you want to modify column name,you must use array(for example change_arr) in there.
every_page_show_data); //every pages display number of data.
dsd.showData(); //display first page in your DataGrid.
total_txt.text = dsd.getTotalPage(); //display total number of pages.
prev_btn.onRelease = function(){
dsd.previousPage();
my_txt.text = dsd.getCurrentPage();
}
next_btn.onRelease = function(){
dsd.nextPage();
my_txt.text = dsd.getCurrentPage();
}
@description
detachShowData类最简单的功能是用分页的形式把DataSet里的数据显示在DataGrid里,它还有更改显示在DataGrid里的column的名字的功能(把要更改的
column的名字按照顺序放到一个数组里,把这个数组放到构造器里的第3个参数里就可以了)它还具有显示指定column的功能(您需要把不显示的column的序
号放到一个数组里面,然后把这个数组作为showData();的参数放进去就可以了。注意如果你既想改变显示的column又想删除不想要的column时,那么在你的
更改column的数组里面里面是所有的column的名字,包括你要删除的,否则会造成显示的混乱
the best easy of the detachShowData is display data in the DataGrid from DataSet using manner of pagination,
and it alse supply advanced
function.first,it supply the function of change column in the DataGrid(Make the name of column you want to change put in a Array which
according to gradation,put the Array in the third parameters of the constructor).Second,
it supply the function of display the column of
you selected(you could make the number of column that you want to display put in a Array(you_want_to_delete_arr),then write this mothed
(showData(you_want_to_delete_arr);),
but you must note,between you want to change the name of column that you want to display and you also
delete the column you want to display,the array of changing are all of the column name,include to want to delete,
otherwise,there is cause
mistake.Good Luck!
*/
import mx.controls.gridclasses.DataGridColumn;
import mx.data.components.DataSet;
import mx.controls.DataGrid;
class js.detachShowData{
private var _fountain_data:DataSet;
private var _show_data:DataGrid;
private var _every_page_show_data:Number = 0;
private var _current_page:Number = 0;
private var _page_total:Number = 0;
private var _modify_column_arr:Array = new Array();
private var _whether_modify_column:Boolean = false;
private static var _version:String = "1.0.0";
private static var _author:String = "JinSong";
function detachShowData(fountain_data:DataSet,show_data:DataGrid,modify_column_arr:Array,every_page_show_data:Number){
_fountain_data = fountain_data;
_show_data = show_data;
_every_page_show_data = every_page_show_data;
_page_total = figureOutTotalPage();
_current_page = 1;
if(modify_column_arr == null){
insertColumnToDataGrid();
}else{
_modify_column_arr = modify_column_arr;
modifyColumnName();
_whether_modify_column = true;
}
}
private function figureOutTotalPage():Number{
if(_fountain_data.length - _every_page_show_data <= 0){
return 1;
}else{
if(_fountain_data.length%_every_page_show_data == 0){
return _fountain_data.length / _every_page_show_data;
}else{
return Math.floor(_fountain_data.length / _every_page_show_data) + 1;
}
}
}
private function figureOutBeginDataPosition():Number{
var flag:Number;
if(_current_page == 1){
_fountain_data.first();
flag = 0;
}else{
_fountain_data.first();
for(var i=0;i<(_current_page-1)*_every_page_show_data;i++){
_fountain_data.next();
}
flag = (_current_page-1)*_every_page_show_data;
}
return flag;
}
private function insertColumnToDataGrid():Void{
for(var j in _fountain_data.properties){
_show_data.addColumn(new DataGridColumn(j));
}
}
private function insertDataToDataGrid(my_arr:Array,my_obj:Object):Void{
my_arr.push(my_obj);
}
public function Version():String{
return _version;
}
private function showSelectData(remove_select_column_arr:Array):Void{
for(var i=0;i_show_data.removeColumnAt(remove_select_column_arr[i]);
/*-- when delete column,behind columns all decrease 1,
* our must make our data in Array which smaller this decrease 1 too
*--*/
for(var j=0;jif(remove_select_column_arr[j] > remove_select_column_arr[i]){
remove_select_column_arr[j]--;
}
}
}
}
private function showSourceData(remove_select_column_arr:Array):Void{
var result_arr = new Array();
var begin = figureOutBeginDataPosition();
var end;
if(begin+_every_page_show_data >= _fountain_data.length){
end = _fountain_data.length;
}else{
end = begin + _every_page_show_data;
}
for(var i=begin;iinsertDataToDataGrid(result_arr,_fountain_data.currentItem);
if(_fountain_data.hasNext()){
_fountain_data.next();
}
}
_show_data.dataProvider = result_arr;
if(remove_select_column_arr != undefined){
showSelectData(remove_select_column_arr);
}
}
public function author():String{
return "This classs author is " + _author + ",Thank you for using this class.";
}
public function showData(remove_select_column_arr:Array):Void{
if(_whether_modify_column){
showModifyColumnData(remove_select_column_arr);
}else{
showSourceData(remove_select_column_arr);
}
}
private function showModifyColumnData(remove_select_column_arr:Array):Void{
var begin = figureOutBeginDataPosition();
var end;
if(begin+_every_page_show_data >= _fountain_data.length){
end = _fountain_data.length - begin;
}else{
end = _every_page_show_data;
}
_show_data.removeAll();
for(var j=0;j_show_data.addItem();
for(var i=0;i<_fountain_data.schema.childNodes[0].childNodes.length;i++){
_show_data.getItemAt(j)[_show_data.columnNames[i]] = _fountain_data.currentItem[_fountain_data.schema.childNodes[0].childNodes[i].attributes.name];
}
if(_fountain_data.hasNext()){
_fountain_data.next();
}
}
if(remove_select_column_arr != undefined){
showSelectData(remove_select_column_arr);
}
}
private function modifyColumnName():Void{
for(var i=0;i<_modify_column_arr.length;i++){
_show_data.addColumn(new DataGridColumn(_modify_column_arr[i]));
}
}
public function previousPage():Void{
if(_current_page <= 1){
_current_page = 1;
}else{
_current_page--;
}
showData();
}
public function nextPage():Void{
if(_current_page >= _page_total){
_current_page = _page_total;
}else{
_current_page++;
}
showData();
}
public function selectPage(select_page_num:Number):Void{
if(select_page_num <= 1){
_current_page = 1;
}else if(select_page_num >= _page_total){
_current_page = _page_total;
}else{
_current_page = select_page_num;
}
showData();
}
public function getCurrentPage():Number{
return _current_page;
}
public function getTotalPage():Number{
return _page_total;
}
public function getDataSet():DataSet{
return _fountain_data;
}
public function setDataSet(fountain_data:DataSet):Void{
_fountain_data = fountain_data;
}
public function getDataGrid():DataGrid{
return _show_data;
}
public function setDataGrid(show_data:DataGrid):Void{
_show_data = show_data;
}
public function getEveryPageShowDataNum():Number{
return _every_page_show_data;
}
public function setEveryPageShowDataNum(every_page_show_data:Number):Void{
_every_page_show_data = every_page_show_data;
_page_total = figureOutTotalPage();
}
}