今天在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格式數據。