今天在Layui官网拿了一个table表格数据展示的源码,研究遇到了很多问题,最后才把数据展示出来,和大家分享下。
源码地址:https://www.layui.com/demo/table/operate.html
下面图片是做出来展示数据的效果
说下遇到的问题:
1.去Layui官网下载框架文件,解压后必须完整的放到项目里然后引用文件,注意必须完整。
2.
这个url链接的地址:是你项目目录下的具体方法,这个方法经测试返回了Layui要求的JSON格式数据
上面的截图里面有一个json.ToJson_LayUI()的方法,这是将数据转换成Layui所需要的JSON格式数据,为了方便不截图了,贴代码如下:
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 Function
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("]")
Return jsonString.ToString()
End Function
这两个方法把数据转换成JSON格式数据。