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