菜单综合示例

本节的任务是分页浏览后台数据,连续点击某个单元格,能弹出一个菜单,通过这个菜单实现订单的编辑、增加和删除:

增加、编辑或删除订单后,都能出现一个提示,例如下图是增加订单成功后的提示:

设计步骤:

1、在"d:\web"目录下,建立一个子目录lib,在这个目录建立一个文本文件,文件名为"table.js",文件内容为:

function edit(){
    location="edit.htm?page=" + table1.pagenumber + "&key=" + table1.primarykey;
}
function del(){
    location="delete.htm?page=" + table1.pagenumber + "&key=" + table1.primarykey;
}
function addnew(){
    location="addnew.htm?page=" + table1.pagenumber;
}

上述js代码定义了三个函数,代码都很简单,将表格的pagenumber(当前页码)和primarykey(选定行主键)用get方式传递给服务器

假定当前页码是3,主键为176,那么执行edit函数,传递给服务器的链接地址就是:

http://127.0.0.1/edit.htm?page=3&key=176

服务器就知道:用户要编辑的订单的主键值是176,编辑结束后,返回第4页。

提示:delete是JavaScript的关键词,不能用作函数名。

2、为例避免HttpRequest事件代码过长,也为了更便于维护,我们将主要功能用自定义函数实现。

本示例包括四个自定义函数,其中List函数用于数据的分页显示,以及菜单的生成和显示,另外三个函数为AddNew、Delete和Edit,分别用于增加、删除和编辑订单。

函数的代码为:

函数名 代码
List

Dim e As RequestEventArgs = args(0)
'
获取要显示的页
Dim
page As Integer = 0 '默认page0,显示第一页
Dim
pageRows As Integer = 10 '每页10
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 日期 desc) As [NO.],[_Identify],产品,客户,数量,单价,日期 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.AddActionSheet("","menu") '设计菜单
    .Add("mnudAdd",
"
增加订单").Attribute="onclick='addnew()'" '调用js函数
    .Add("mnuEdit",
"
编辑订单").Attribute ="onclick='edit()'"
    .Add("mnuDelete",
"
删除订单").Attribute ="onclick='del()'"
    .Add("mnuCancel",
"
取消","",True)
End
With
'
根据此页数据生成网页
With
wb.AddTable("","Table1")
    .PageNumber = page
'
设置页码
    .Primarykey = "_Identify"
'
设置主键
    .ActiveSheet = "menu"
'
设置菜单
    .CreateFromDataTable(dt)

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
wb
.AppendHTML("<script src='./lib/table.js'></script>") '引入脚本文件
e
.WriteString(wb.Build)

AddNew

Dim e As RequestEventArgs = args(0)
Dim
wb As New weui
If
e.PostValues.Count = 0 Then '生成增加订单网页
    wb.AddForm("","form1","addnew.htm")
    With wb.AddInputGroup("form1","ipg1",
"
增加订单")
        .AddInput(
"
产品","产品","text")
        .AddInput(
"
客户","客户","text")
        .AddInput(
"
数量","数量","number")
        .AddInput(
"
单价","单价","number").Step="0.01"
        .AddInput(
"
日期","日期","date")
    End With
    With wb.AddButtonGroup("form1","btg1",True)
        .Add("btn1",
"
确定", "submit")
    End
With

Else
'保存新增的订单
    Dim dr As DataRow = DataTables(
"
订单").AddNew()
    Dim nms() As String =
{"
产品","客户","数量","单价","日期"}
    For Each nm As String In nms
        dr(nm) = e.PostValues(nm)
    Next
    dr.Save()
    With wb.AddMsgPage("","msgpage",
"
增加成功", "好好学习,天天向上") '增加订单成功提示信息
        .AddButton("btn1",
"
继续增加","addnew.htm")
        .AddButton("btn1",
"
返回列表","list.htm")
   
End With
End
If
e
.WriteString(wb.Build) '生成网页

Edit

Dim e As RequestEventArgs = args(0)
Dim
wb As New weui
Dim
PageNumber As Integer = e.GetValues("page")
Dim
PrimaryKey As Integer = e.GetValues("key")
Dim
PageURL = "List.htm?page=" & PageNumber
If
e.PostValues.Count = 0 Then '生成编辑页面
    Dim dr As DataRow = DataTables(
"
订单").SQLFind("[_Identify]=" & PrimaryKey)
    If dr IsNot Nothing Then
        wb.AddForm("","form1","edit.htm?key=" & PrimaryKey &  "&page=" & PageNumber)
        With wb.AddInputGroup("form1","ipg1",
"
编辑订单")
            .AddInput(
"
产品","产品","text").Value = dr("产品")
            .AddInput(
"
客户","客户","text").Value = dr("客户")
            .AddInput(
"
数量","数量","number").Value = dr("数量")
            With .AddInput(
"
单价","单价","number")
                .Step="0.01"
                .Value = dr(
"
单价")
            End With
            .AddInput(
"
日期","日期","date").Value = dr("日期")
        End With
        With wb.AddButtonGroup("form1","btg1",True)
            .Add("btn1",
"
确定", "submit")
        End With
    Else
        With wb.AddMsgPage("","msgpage",
"
编辑失败", "此订单可能已经被删除!") '提示用户此订单不存在.
            .icon= "Warn"
            .AddButton("btn1",
"
返回",PageURL) '生成返回原来页面的按钮
        End With
    End
If

Else
'保存编辑结果
    Dim dr As DataRow = DataTables(
"
订单").SQLFind("[_Identify]=" & PrimaryKey)
    If dr IsNot Nothing Then
        Dim nms() As String =
{"
产品","客户","数量","单价","日期"}
        For Each nm As String In nms
            dr(nm) = e.PostValues(nm)
        Next
        dr.Save()
       
'显示完成提示,2妙手自动返回原来的页面
       
wb.AppendHtml("<meta http-equiv='refresh' content='2; url=/" & PageURL  & "'>",True)
        wb.AddToast("","t1",
"
编辑完成",0).Visible = True
    Else
        With wb.AddMsgPage("","msgpage",
"
保存失败", "此订单可能已经被删除!") '提示用户此订单不存在.
            .icon= "Warn"
            .AddButton("btn1","返回",PageURL) '生成返回原来页面的按钮
        End With
    End
If
End
If
e
.WriteString(wb.Build) '生成网页

Delete

Dim e As RequestEventArgs = args(0)
Dim
wb As New weui
Dim
PageURL = "List.htm?page=" & e.GetValues("page") '合成当前页面链接
DataTables
("订单").SQLDeleteFor("[_Identify] =" & e.GetValues("key")) '根据主键删除行
'显示删除完成提示,2秒后返回原来的页面

wb
.AppendHtml("<meta http-equiv='refresh' content='2; url=/" & PageURL  & "'>",True)
wb
.AddToast("","t1", "订单已删除",0).Visible= True
e
.WriteString(wb.Build)

3、将HttpRequest事件代码设置为:


Select
Case e.Path
    Case "list.htm"
        Functions.Execute("List",e)
    Case "addnew.htm"
        Functions.Execute("AddNew",e)
    Case "edit.htm"
        Functions.Execute("Edit",e)
    Case "delete.htm"
        Functions.Execute("Delete",e)

End
Select


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