我们可以从summaryObjectFunction返回字符串吗?

时间:2020-03-05 18:54:14  来源:igfitidea点击:

在FlexAdvancedDatGrid中,我们进行了大量分组。大多数列对于父母和孩子都是相同的,因此我想显示组的第一个值作为摘要,而不是MAX,MIN或者AVG

这段代码适用于数字值,而不适用于文本值(没有注释行,我们会得到NaN):

private function firstValue(itr:IViewCursor,field:String, str:String=null):Object 
{
  //if(isNaN(itr.current[field])) return 0  //Theory: Only works on Numeric Values?
  return itr.current[field]
}

XML:

(mx:GroupingField name="Offer")
  (mx:summaries)
    (mx:SummaryRow summaryPlacement="group")
      (mx:fields)
        (mx:SummaryField dataField="OfferDescription"   label="OfferDescription" summaryFunction="firstValue"/)
        (mx:SummaryField dataField="OfferID"   label="OfferID" summaryFunction="firstValue"/)
        (/mx:fields)
    (/mx:SummaryRow)
  (/mx:summaries)
(/mx:GroupingField)

OfferID's可以正常工作,'OfferDescription`s不能正常工作。

解决方案

回答

看起来summaryFunction必须返回一个数字。根据Adobe Bug Tracker的说法,它是文档中的错误:

Comment from Sameer Bhatt:
  
  In the documentation it is mentioned that -
  The built-in summary functions for SUM, MIN, MAX, AVG, and COUNT all return a Number containing the summary data.
  
  So people can get an idea but I agree with you that we should clearly mention that the return type should be a Number.
  
  We kept it as an Object so that it'll be easy in the future to add more things in it.

回答

如果需要显示字符串,请使用advancedDataGridColumn上的label函数。这将呈现摘要行。

(mx:AdvancedDataGridColumn headerText =" Comment" width =" 140" dataField =" comment" labelFunction =" formatColumn" /)

private function getNestedItem(item:Object):Object {

                try {
                    if (item.undefined[0]) {
                        item = getNestedItem(item.undefined[0]);
                    }
                } catch (e:Error) {
                    // leave item alone
                }
                return item;
            }           
            private function formatColumn(item:Object, column:AdvancedDataGridColumn):String {

                var output:String;
                // If this is a summary row
                if (item.GroupLabel) {

                    item = getNestedItem(item);
                } 

                switch (column.dataField) {

                    case 'comment':

                        return item.comment;

                }

            }