DataTables not updated?

DataTables not updated?

eformxeformx Posts: 3Questions: 0Answers: 0
edited November 2011 in General
The following returns a valid json but the datatable is not updated. In the alert the json is displayed but then after clicking OK nothing happens. Hope you can help.

[code] var oTable = $('.displayData').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "Data.aspx",
"aoColumns": [
{ "sName": "id", "sTitle": "ID", "sWidth": "5%", "bSortable": "true" },
{ "sName": "engine", "sTitle": "Engine", "sWidth": "10%", "bSortable": "true" },
{ "sName": "browser", "sTitle": "browser", "sWidth": "10%", "bSortable": "true" },
{ "sName": "platform", "sTitle": "platform", "sWidth": "10%", "bSortable": "true" },
{ "sName": "version", "sTitle": "version", "sWidth": "10%", "bSortable": "true" },
{ "sName": "grade", "sTitle": "grade", "sWidth": "10%", "bSortable": "true" }

],
"sPaginationType": "full_numbers",
"aaSorting": [[1,'asc']],
"oLanguage": {
"sLengthMenu": "Page length: _MENU_",
"sSearch": "Filter:",
"sZeroRecords": "No matching records found"
},

"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
url: sSource,
data: aoData,
success: function (msg) {
alert(msg);
fnCallback(msg);
},
error: function (xhr, error, thrown) {
if (error == "parsererror") {
alert("DataTables warning: JSON data from server could not be parsed. " +
"This is caused by a JSON formatting error.");
}
}
});

}
})

[/code]

In the body of the page I have the following table structure.

[code]





ID
Engine
Browser
Platform
Version
Grade




Loading data from server




[/code]

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    include "dataType": 'json'. it's possible your 'msg' was returned but being treated as html text and not a json object... ?

    [code]
    $.ajax( {
    dataType: 'json',
    url: sSource,
    data: aoData,
    success: function (msg) {
    alert(msg);
    fnCallback(msg);
    },
    error: function (xhr, error, thrown) {
    if (error == "parsererror") {
    alert("DataTables warning: JSON data from server could not be parsed. " +
    "This is caused by a JSON formatting error.");
    }
    }
    });[/code]
  • eformxeformx Posts: 3Questions: 0Answers: 0
    @fbas Solved! This is what I ended up with. It now works (ASP.Net VB.net WebForms)

    [code] $('#displayData').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "bAutoWidth": false,
    "aoColumns": [
    { "sName": "id", "sTitle": "ID", "sWidth": "5%", "bSortable": "true" },
    { "sName": "engine", "sTitle": "Engine", "sWidth": "10%", "bSortable": "true" },
    { "sName": "browser", "sTitle": "browser", "sWidth": "10%", "bSortable": "true" },
    { "sName": "platform", "sTitle": "platform", "sWidth": "10%", "bSortable": "true" },
    { "sName": "version", "sTitle": "version", "sWidth": "10%", "bSortable": "true" },
    { "sName": "grade", "sTitle": "grade", "sWidth": "10%", "bSortable": "true" }

    ],
    "sPaginationType": "full_numbers",
    "aaSorting": [[1,'asc']],
    "oLanguage": {
    "sLengthMenu": "Page length: _MENU_",
    "sSearch": "Filter:",
    "sZeroRecords": "No matching records found"
    },
    "sAjaxSource":'Data.aspx'
    });

    [/code]


    End Using
    Catch ex As Exception
    End Try


    End Sub

    [/code]
  • eformxeformx Posts: 3Questions: 0Answers: 0
    edited November 2011
    [code] Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim str As String = "" 'Chaîne JSON en sortie - Will be the output JSON string
    Dim strClassement As String = "" 'Colonne sur laquelle le tableau est trié - Will be the sorted column
    Dim strSens As String = "" 'ASC or DESC
    Dim strSensInverse As String = "" 'DESC or ASC


    'Détection de la colonne de tri
    'Detection of sorted column
    Select Case Request("iSortCol_0")
    Case 0
    strClassement = "engine"
    Case 1
    strClassement = "browser"
    Case 2
    strClassement = "platform"
    Case 3
    strClassement = "version"
    Case 4
    strClassement = "grade"
    Case Else
    strClassement = "engine"
    End Select


    Select Case Request("sSortDir_0")
    Case "asc"
    strSens = " ASC"
    strSensInverse = " DESC"
    Case "desc"
    strSens = " DESC"
    strSensInverse = " ASC"
    End Select


    Try


    Using maConnexion As New SqlConnection(ConfigurationManager.ConnectionStrings("cnn_TTR").ConnectionString)

    maConnexion.Open()

    Dim dataset As New DataSet

    'Création de la requête SQL
    'SQL request creation
    Dim strRequeteA As New String("")
    Dim strRequeteC As New String("")
    Dim strRequeteB_1 As New String("")
    Dim strRequeteB_2 As New String("")
    Dim strRequeteB_3 As New String("")

    'Le requête est bâtie selon une méthode permettant la pagination avec SQL Server
    'The request is built with a method which allow pagination with SQL Server
    ' http://troels.arvin.dk/db/rdbms/
    strRequeteA += "SELECT * FROM ("
    strRequeteA += "SELECT TOP "
    strRequeteA += Request("iDisplayLength").ToString
    strRequeteA += " * FROM ("
    strRequeteB_1 = "SELECT "
    strRequeteB_2 = "TOP " + (Convert.ToInt32(Request("iDisplayStart")) + Convert.ToInt32(Request("iDisplayLength"))).ToString
    strRequeteB_3 += " id, engine, browser, platform, version, grade "
    strRequeteB_3 += "FROM ajax "

    If Request("sSearch") <> "" Then
    If Request("sSearch") <> " " Then
    strRequeteB_3 += "WHERE "
    Dim chaine As String
    For i As Integer = 0 To (Split(Request("sSearch").ToString, " ").Length - 1)
    chaine = Split(Request("sSearch").ToString, " ")(i)
    If chaine <> "" Then
    If i <> 0 Then
    strRequeteB_3 += "AND ("
    Else
    strRequeteB_3 += "("
    End If
    strRequeteB_3 += "engine LIKE '%" + chaine + "%'"
    strRequeteB_3 += " OR "
    strRequeteB_3 += "browser LIKE '%" + chaine + "%'"
    strRequeteB_3 += " OR "
    strRequeteB_3 += "platform LIKE '%" + chaine + "%'"
    strRequeteB_3 += " OR "
    strRequeteB_3 += "version LIKE '%" + chaine + "%'"
    strRequeteB_3 += " OR "
    strRequeteB_3 += "grade LIKE '%" + chaine + "%'"
    strRequeteB_3 += ") "
    End If
    Next
    End If
    End If

    strRequeteC += " ORDER BY "
    strRequeteC += strClassement
    strRequeteC += strSens

    strRequeteC += ") AS foo ORDER BY "
    strRequeteC += strClassement
    strRequeteC += strSensInverse

    strRequeteC += ") AS bar ORDER BY "
    strRequeteC += strClassement
    strRequeteC += strSens


    Dim MaCommande As New SqlCommand(strRequeteA + strRequeteB_1 + strRequeteB_2 + strRequeteB_3 + strRequeteC, maConnexion)
    Dim MonAdapteur As SqlDataAdapter = New SqlDataAdapter(MaCommande)
    MonAdapteur.Fill(dataset)
    MaCommande.Dispose()

    If dataset.Tables.Count > 0 Then
    Dim table As New DataTable
    table = dataset.Tables(0)

    'Ecriture des données JSON
    'JSON data writing
    str = "{"
    str += """sEcho"": " + Request("sEcho") + ","


    strRequeteA = "SELECT COUNT(id) FROM ajax"
    dataset = New DataSet
    MaCommande = New SqlCommand(strRequeteA, maConnexion)
    MonAdapteur = New SqlDataAdapter(MaCommande)
    MonAdapteur.Fill(dataset)
    MaCommande.Dispose()
    str += """iTotalRecords"": " + dataset.Tables(0).Rows(0)(0).ToString + ","

    strRequeteA = "SELECT COUNT(id) FROM (" + strRequeteB_1 + strRequeteB_2 + strRequeteB_3 + ") AS P1"
    dataset = New DataSet
    MaCommande = New SqlCommand("SELECT COUNT(id) FROM (" + strRequeteB_1 + strRequeteB_3 + ") AS P1", maConnexion)
    MonAdapteur = New SqlDataAdapter(MaCommande)
    MonAdapteur.Fill(dataset)
    MaCommande.Dispose()
    str += """iTotalDisplayRecords"": " + dataset.Tables(0).Rows(0)(0).ToString + ","

    str += """aaData"": ["

    Dim nbRowANePasAfficher As Integer = 0
    If (Convert.ToInt32(Request("iDisplayStart")) + Convert.ToInt32(Request("iDisplayLength"))) > dataset.Tables(0).Rows(0)(0) Then
    nbRowANePasAfficher = (Convert.ToInt32(Request("iDisplayStart")) + Convert.ToInt32(Request("iDisplayLength"))) - dataset.Tables(0).Rows(0)(0)
    End If
    If Convert.ToInt32(Request("iDisplayStart")) = 0 Then
    nbRowANePasAfficher = 0
    End If


    Dim autre As Boolean = False
    For Each row As DataRow In table.Rows
    If nbRowANePasAfficher > 0 Then
    nbRowANePasAfficher -= 1
    Continue For
    End If
    If autre = True Then
    str += ","
    End If
    str += "["
    str += """" + row("id").ToString + ""","
    str += """" + row("engine").ToString + ""","
    str += """" + row("browser").ToString + ""","
    str += """" + row("platform").ToString + ""","
    str += """" + row("version").ToString + ""","
    str += """" + row("grade").ToString + """"
    str += "]"
    autre = True
    Next
    End If
    str += "]"
    str += "}"

    Response.Clear()
    Response.ClearHeaders() [/code]
    Response.ClearContent()
    Response.Write(str)
    Response.Flush()
    Response.End()

    maConnexion.Close()
This discussion has been closed.