`
DavyJones2010
  • 浏览: 154929 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Flex: How to specify DataGrid sortFunction and disable default sortFunction

    博客分类:
  • Flex
阅读更多

 

Requirements:

    1. Set the first column of the data grid to be checkbox, indicating we can select one or more specific rows.

    2. Once we clicked the header, then the data grid can be sorted by the clicked column, if there are same item in this column indicating that returns 0, we use the id column which is identical as the secondary sort strategy.

    3. Pay attention to the jump bug if we sort by a specific column which have the same value.

 

1. Value Object

package vo
{
	
	public class FlatFileUploadVo
	{
		public var id:int;
		public var isUpload:Boolean;
   	 	public var feedName:String;
   	 	public var reqBy:String;
   	 	   	 	
		public function FlatFileUploadVo()
		{
		}
	}
}

 

2. The item renderer for the first column as check box

 

package components
{
	import com.flexicious.controls.CheckBox;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	
	import vo.FlatFileUploadVo;

	public class CheckBoxColumnRenderer extends CheckBox
	{
		public function CheckBoxColumnRenderer()
		{
		}
		
		override public function set data(value:Object):void
		{
			super.data = value;					//This line is important! Internally set data to the value passed in.
			
			var isUpload:Boolean = value.isUpload;
			if(true == isUpload)
			{
				selected = true;
			}
			else
			{
				selected = false;
			}
		}
		
		override protected function clickHandler(event:MouseEvent):void
		{
			var selectedCheckBox:CheckBoxColumnRenderer = event.currentTarget as CheckBoxColumnRenderer;
			
			(selectedCheckBox.data as FlatFileUploadVo).isUpload = !(selectedCheckBox.data as FlatFileUploadVo).isUpload;
			selectedCheckBox.selected = !selectedCheckBox.selected;
		}
	}
}

 

3. The main application (I am using with flexicious)

 

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   xmlns:columns="com.flexicious.grids.columns.*"
			   minWidth="955" minHeight="600" xmlns:grids="com.flexicious.grids.*"
			   creationComplete="onComplete(event)"
			   >
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.collections.ISort;
			import mx.collections.Sort;
			import mx.collections.SortField;
			import mx.controls.Alert;
			import mx.events.DataGridEvent;
			import mx.messaging.channels.StreamingAMFChannel;
			
			import vo.FlatFileUploadVo;
			
			[Bindable]
			public var queList:ArrayCollection = new ArrayCollection();
			private var sortA:ISort;
			private var sortByFeedName:SortField = new SortField("feedName", false, true);
			private var sortById:SortField = new SortField("id", false, false);
			private var sortByReqBy:SortField = new SortField("reqBy", false, false);
			private var flatFile:FlatFileUploadVo;
			
			private function onComplete(event:Event):void
			{
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = false;
				flatFile.feedName = "feedName01";
				flatFile.reqBy = "system";
				flatFile.id = 1;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = true;
				flatFile.feedName = "feedName02";
				flatFile.reqBy = "system";
				flatFile.id = 2;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = true;
				flatFile.feedName = "feedName03";
				flatFile.reqBy = "system";
				flatFile.id = 3;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = true;
				flatFile.feedName = "feedName04";
				flatFile.reqBy = "system";
				flatFile.id = 4;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = false;
				flatFile.feedName = "feedName05";
				flatFile.reqBy = "system";
				flatFile.id = 5;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = false;
				flatFile.feedName = "feedName06";
				flatFile.reqBy = "system";
				flatFile.id = 6;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = false;
				flatFile.feedName = "feedName07";
				flatFile.reqBy = "system";
				flatFile.id = 7;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = false;
				flatFile.feedName = "feedName08";
				flatFile.reqBy = "system";
				flatFile.id = 8;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = false;
				flatFile.feedName = "feedName09";
				flatFile.reqBy = "system";
				flatFile.id = 9;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = true;
				flatFile.feedName = "feedName10";
				flatFile.reqBy = "system";
				flatFile.id = 10;
				queList.addItem(flatFile);
				
				flatFile = new FlatFileUploadVo();
				flatFile.isUpload = true;
				flatFile.feedName = "feedName02";
				flatFile.reqBy = "system";
				flatFile.id = 11;
				queList.addItem(flatFile);
				
				sortA = new Sort();
				sortA.fields = [sortByFeedName, sortById];
				
				queList.sort = sortA;
				queList.refresh();
			}
			private function feedNameSortCompareFunction(itemA:FlatFileUploadVo, itemB:FlatFileUploadVo):int
			{
				if(itemA.feedName > itemB.feedName)
				{
					return 1;
				}
				else if(itemA.feedName < itemB.feedName)
				{
					return -1;
				}
				else
				{
					return idSortCompareFunction(itemA, itemB);
				}
			}
			private function reqestBySortCompareFunction(itemA:FlatFileUploadVo, itemB:FlatFileUploadVo):int
			{
				if(itemA.reqBy > itemB.reqBy)
				{
					return 1;
				}
				else if(itemA.reqBy < itemB.reqBy)
				{
					return -1;
				}
				else
				{
					return idSortCompareFunction(itemA, itemB);
				}
			}
//			private function onHeaderRelease(event:DataGridEvent):void
//			{
//				if(event.columnIndex == 2)
//				{
//					sortA.fields[0] = sortByFeedName;
//					sortA.fields[1] = sortById;
//					Alert.show("Sort by feedName");
//				}
//				else if(event.columnIndex == 3)
//				{
//					//sortA.fields[0] = sortByReqBy;
//					sortA.fields[0] = sortById;
//					sortA.fields[1] = sortByFeedName;
//					Alert.show("Sort by reqBy");
//				}
//				
//				queList.sort = sortA;
//				queList.refresh();
//				
//				event.preventDefault();
//				Alert.show("Sort by reqBy end");
//			}
			
			// id is identical, so we can be sure that no two rows' id will be the same
			private function idSortCompareFunction(obj1:Object, obj2:Object):int
			{
				if(obj1.id > obj2.id)
				{
					return 1;
				}
				else
				{
					return -1;
				}
			}

		]]>
	</fx:Script>
	
	<grids:ExtendedDataGrid dataProvider="{queList}" width="400">
		<grids:columns>
			<columns:ExtendedDataGridColumn headerText="Initiate?" sortable="false" editable="false" dataField="isUpload" itemRenderer="components.CheckBoxColumnRenderer"/>
			<columns:ExtendedDataGridColumn headerText="ID" sortable="false" editable="false" dataField="id" width="8"/>
			<columns:ExtendedDataGridColumn headerText="Feed Name" sortCompareFunction="feedNameSortCompareFunction" sortable="true" editable="false" textAlign="left" dataField="feedName" width="100"/>
			<columns:ExtendedDataGridColumn headerText="Request By" sortCompareFunction="reqestBySortCompareFunction" sortable="true"  editable="false" textAlign="left" dataField="reqBy" width="100"/>
		</grids:columns>
	</grids:ExtendedDataGrid>
	
</s:Application>

 

 

Problems:

    1. Still have some problems in code refractor: How to define only one compare function to make the code cleaner?

    2. As we can see from the comment, the headerRelease function doesn't work and the reason is still unknown. 

 

分享到:
评论

相关推荐

    linux mount报错:you must specify the filesystem type的解决方法

    mount: you must specify the filesystem type 先执行:mkfs.ext3 /dev/vdb # mkfs.ext3 /dev/vdb mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 ...

    itk,错误:use /Zm to specify a higher limit解决办法

    use /Zm to specify a higher limit ``` 此错误表明编译器内部堆栈空间已达到限制,导致编译无法继续。通常这种问题出现在包含大量头文件或者依赖复杂模板元编程的大型项目中。下面将详细介绍如何解决这一问题。 ...

    C Programming

    - **Array Types and Sizes**: Overview of different types of arrays (one-dimensional, multidimensional) and how to specify their sizes. - **More Than One Dimension**: Explanation of multidimensional ...

    Tensorflow 2.1 报错整合

    文章目录Tensorflow 2.1 报错整合RuntimeError: `loss` passed to Optimizer.compute_gradients should be a function when eager execution is enabled.RuntimeError: Attempting to capture an EagerTensor ...

    dotnetnuke datagrid

    -Paging- Admin can specify whether to use paging on the grid and how many records to show per page. -Export datagrid to an Excel file. -Parameter Substitution: SQL can include [dnn:UserID] or [dnn:...

    Pycharm管理解释器报错:Please specify a different SDK name-解决方法

    Pycharm管理解释器报错:Please specify a different SDK name-解决方法

    UE(官方下载)

    How to enable and disable autocorrect keywords with syntax highlighting Insert Menu Commands UltraEdit includes several special insert functions under the Insert menu. You can use these functions to ...

    tensorflow-1.12支持cuda10.0

    Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. ...

    A Java architecture test library, to specify and assert ar.zip

    这个名为"A Java architecture test library, to specify and assert ar.zip"的压缩包,包含的是ArchUnit的主要源码和资源文件,其核心部分是`ArchUnit-main`。 ArchUnit库的特性与用法: 1. **定义规则**:...

    tensorflow1.12支持cuda10

    Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. ...

    C# to Java 代码转换工具

    Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not require you to insert entire methods or classes. Heuristics are used to convert code fragments wit h ...

    javascirpt权威指南中英版

    of web pages, CSS to specify the presentation of web pages, and JavaScript to specify the behavior of web pages. This book will help you master the language. If you are already familiar with other ...

    Android代码-Termux:Task

    Termux:Task A Termux add-on app ...Edit the configuration to specify the executable in ~/.termux/tasker/ to execute, and if it should be executed in the background (the default) or in a new termi

    gogoLogs:读取日志数据并将其发送到syslog服务器的工具

    -P="UDP": Specify protocol to send data over (default: UDP) -S=0: Specify syslog priority value (default:0) -d="127.0.0.1": Specify destination IP (default: 127.0.0.1) -f="": Specify file to read ...

    php.ini-development

    To disable this feature set this option to empty value ;user_ini.filename = ; TTL for user-defined php.ini files (time-to-live) in seconds. Default is 300 seconds (5 minutes) ;user_ini.cache_ttl = ...

    pycurl-7.43.0.3-cp27-cp27m-win-amd64.whl

    Windos下安装pyspider报错:Please specify --curl-dir=/path/to/built/libcurl Windos下安装pyspider报错:Please specify --curl-dir=/path/to/built/libcurl Windos下安装pyspider报错:Please specify --curl-dir=/...

    kubesphere离线安装打包报错

    在IT行业中,Kubernetes(K8s)是一个广泛使用的容器编排系统,而KubeSphere是在Kubernetes之上构建的企业级开放平台,它提供了一站式的应用全生命周期管理服务。KubeSphere离线安装是为了在没有互联网连接或者网络...

Global site tag (gtag.js) - Google Analytics