Layui使用table展现数据

 


今天在Layui官网拿了一个table表格数据展现的源码,研究遇到了不少问题,最后才把数据展现出来,和你们分享下。html

源码地址:https://www.layui.com/demo/table/operate.htmljson

下面图片是作出来展现数据的效果框架

说下遇到的问题:测试

1.去Layui官网下载框架文件,解压后必须完整的放到项目里而后引用文件,注意必须完整。ui

2.url

这个url连接的地址:是你项目目录下的具体方法,这个方法经测试返回了Layui要求的JSON格式数据spa

上面的截图里面有一个json.ToJson_LayUI()的方法,这是将数据转换成Layui所须要的JSON格式数据,为了方便不截图了,贴代码以下:3d

Public Function ToJson_LayUI(ByVal ds As DataSet) As String
'Dim jsonString As String = "{""total"":" & ds.Tables(0).Rows.Count.ToString & ","
Dim jsonString As String = "{""code"":0,""msg"":"""",""count"":" & ds.Tables(0).Rows.Count.ToString & ","
Dim table As DataTable
For Each table In ds.Tables
jsonString += """data"":" + ToJson(table) + ","
Next
jsonString = jsonString.TrimEnd(",")
Return jsonString + "}"
End Functioncode

 

Public Function ToJson(ByVal dt As DataTable) As String
Dim jsonString As System.Text.StringBuilder = New System.Text.StringBuilder()
jsonString.Append("[")
Dim drc As DataRowCollection = dt.Rows
Dim i As Integer = 0
For i = 0 To drc.Count - 1
jsonString.Append("{")
Dim j As Integer = 0
For j = 0 To dt.Columns.Count - 1
Dim strKey As String = dt.Columns(j).ColumnName
Dim strValue As String = drc(i)(j).ToString()
Dim type As Type = dt.Columns(j).DataType
jsonString.Append("""" + strKey + """:")
strValue = StringFormat(strValue, type)
If (j < dt.Columns.Count - 1) Then
jsonString.Append(strValue + ",")
Else
jsonString.Append(strValue)
End If
Next
jsonString.Append("},")
Next
If jsonString.ToString <> "[" Then
jsonString.Remove(jsonString.Length - 1, 1)
End If
jsonString.Append("]")orm

Return jsonString.ToString()
End Function

这两个方法把数据转换成JSON格式数据。

相关文章
相关标签/搜索