生成汇总模式

有了前面的知识,生成汇总模式是很轻松的。

下面是按产品和客户分组,对数量和金额进行统计的HttpRequest事件代码:

Select Case e.Path
    Case "list.htm"
       
'
获取要显示的页
        Dim page As Integer = 0
'
默认page0,显示第一页
        Dim pageRows As Integer = 13
'
每页13
        If e.GetValues.ContainsKey("page") Then 
'
如果地址中有page参数
            Integer.TryParse(e.GetValues("page"), page)
'
提取page参数
        End If
        Dim StartRow As Integer = page * pageRows + 1
'
此页第一行
        Dim EndRow As Integer = (page + 1) * pageRows
'
此页最后一行
       
'
获取该页数据
        Dim cmd As New SQLCommand
        cmd.ConnectionName = "orders"
'
记得设置数据源名称
        cmd.CommandText =
"Select Count(*) From {
订单}"
        Dim Count As Integer = cmd.ExecuteScalar()
'
获取总的行数
        cmd.CommandText =
"Select * From (Select Row_Number() Over(Order by
产品,客户,日期) As [NO.], 产品, 客户, 数量, 单价, 数量 * 单价 as 金额,日期 From 订单) As a "
        cmd.CommandText = cmd.CommandText & "  Where [NO.]>= " & StartRow & " And [NO.] <= " & EndRow
        Dim dt As DataTable = cmd.ExecuteReader
       
'根据此页数据生成网页
       
Dim wb As New WeUI
        With wb.AddTable("","Table1")
            Dim nms() As String
            Dim qty As Integer
            Dim amt As Double
            .Head.AddRow("No.",
"
产品","客户","数量","单价","金额","日期")
            For i As Integer = 0 To dt.DataRows.count - 1
                Dim r As DataRow = dt.DataRows(i)
                If i > 0 Then
                    Dim lr As DataRow = dt.DataRows(i-1)
                    If r(
"
客户") <> lr("客户") Then
                        qty = dt.compute(
"sum(
数量)","产品='" & lr("产品") & "' And 客户= '" & lr("客户") & "'")
                       
amt = dt.compute("sum(金额)","产品='" & lr("产品") & "' And 客户= '" & lr("客户") & "'")
                        With .Body.AddRow()
                            .Attribute = "style='background-color:#F0FFFF'"
                            .AddCell("小计 " & lr("客户"),"colspan='3'")
                            .AddCells(qty,"",amt,"")
                        End With
                    End If
                    If r("产品") <> lr("产品") Then
                        qty = dt.compute("sum(数量)","产品='" & lr("产品") &  "'")
                        amt = dt.compute("sum(金额)","产品='" & lr("产品") & "'")
                        With .Body.AddRow()
                            .Attribute = "style='background-color:#FFFFE0'"
                            .AddCell("小计 " & lr("产品"),"colspan='3'")
                           
.AddCells(qty,"",amt,"")
                        End With
                    End If
                End If
                .Body.AddRow(r("NO."),r(
"
产品"),r("客户"),r("数量"),r("单价"),r("金额"),r("日期"))
            Next
            qty = dt.compute(
"sum(
数量)")
            amt = dt.compute(
"sum(
金额)")
            If EndRow >= Count Then
                .Body.AddRow(
"
总计","","",qty,"",amt,"").Attribute = "style='background-color:#98FB98'"
            End If
        End With
        With wb.AddButtonGroup("","btg2", False)
            If page > 0 Then
                .Add("btnPrev",
"
上一页","","List.htm?page=" & page - 1)
            End If
            If Endrow < count  Then
                .Add("btnNext", "
下一页","","List.htm?page=" & page + 1)
            End If
        End With
        e.WriteString(wb.Build)

End Select

代码逻辑很简单,按产品和客户顺序,分页显示数据,每当产品或客户发生变化时,就插入一个分组行显示该产品或客户的累计数量和金额。
需要注意的是,分组行的背景没有采用外部CSS文件设置,直接使用了内嵌样式。

这是在手机上的显示效果:


本页地址:http://www.foxtable.com/mobilehelp/topics/0135.htm