No data available in table

No data available in table

dannyliadannylia Posts: 4Questions: 1Answers: 0

I'm unable to get a joined mysql table to populate. It simply says "No data available in table."

Note that in the js snippet below I've added an alert directly calling 'DelayDate' from the data object returned from the url. That popup displays the expected output.

js:

    $(document).ready(function () {
        $.ajax({
            url:'../ajax/getdelayedmatches.php',
            dataType: 'json',
            success: function (data) {
                alert(data['DelayDate']);
                var table = $('#delayedmatches').DataTable({
                    dom: 'Bfrtip',
                    select: true,
                    columns: [
                        { "data": "DelayDate" }
                    ]
                
                });
                
            }
        });
    });

getdelayedmatches.php:

$sql = $conn->query('select M.Date,
        M.DelayDate,
        M.Id AS matchid,
        G.Id as gameid,
        T.Name as t1name, 
        T2.Name as t2name, 
        M.SeasonId,
        M.LocationId,
        G.IsDelay,
        J.Status
    from matches as M
    join teams as T
        on M.TeamIdOne = T.Id 
    join teams as T2 
        on M.TeamIdTwo = T2.Id
    join games as G
        on G.MatchId = M.Id
    join joinrequests as J
        on J.DataObject = G.Id
    where MatchStatus = 0 and `Date` < CURDATE();');
    

$data = $sql->fetch_array();
echo json_encode($data);

If I navigate to /ajax/getdelayedmatches.php in the browser I get:

{"0":"2020-02-18 00:00:00","Date":"2020-02-18 00:00:00","1":"2020-03-04 00:00:00","DelayDate":"2020-03-04 00:00:00","2":"1892","matchid":"1892","3":"4225","gameid":"4225","4":"team1","t1name":"team1","5":"team2 ","t2name":"team2 ","6":"89","SeasonId":"89","7":"1","LocationId":"1","8":"1","IsDelay":"1","9":"2","Status":"2"}

My thinking here is that my php script is working because I can navigate to the page in the browser and see results of my db query. That data is getting loaded because I can access the object via my alert popup. I'm simply not mapping it to the column in the datatable for some reason.

Answers

  • kthorngrenkthorngren Posts: 21,164Questions: 26Answers: 4,921

    You need to add the data to Datatables. Use data for this.

    Kevin

  • dannyliadannylia Posts: 4Questions: 1Answers: 0

    Whew ok thank you. This worked:

    $(document).ready(function () {
        $.ajax({
            url:'../ajax/getdelayedmatches.php',
            dataType: 'json',
            success: function (data) {
                alert(data['DelayDate']);
                var table = $('#delayedmatches').DataTable({
                    data: [
                    {
                        "DelayDate": data['DelayDate']
                    }
                    ],
                    select: true,
                    columns: [
                        { "data": "DelayDate" }
                    ]
    
                });
    
            }
        });
    });
    
  • dannyliadannylia Posts: 4Questions: 1Answers: 0

    Ok I guess I'm still stuck. This code was apparently only returning one row when there should be several.

    $data = $sql->fetch_array();
    echo json_encode($data);
    

    I changed it to this, and now I get all of the rows returned when viewing /ajax/getdelayedmatches.php in the web browser:

    $rows = array();    
    while($row = mysqli_fetch_array($sql)) {
        $rows[] = $row;
    };
    
    echo json_encode($rows);
    

    But then I was back to getting nothing in the table. If I alert on this I get undefined:

    alert(data['DelayDate']);
    

    But if I alert on this I get a single date:

    alert(data['0']['DelayDate']);
    

    But if I add the '0' to the data option of my ajax call, it goes back to only printing one row.

                    var table = $('#delayedmatches').DataTable({
                        dataSrc: '',
                        data: [
                        {
                            "Date": data['0']['Date'],
                            "DelayDate": data['0']['DelayDate'],
                            "t1name": data['0']['t1name'],
                            "t2name": data['0']['t2name'],
                            "matchid": data['0']['matchid'],
                            "gameid": data['0']['gameid'],
                            "SeasonId": data['0']['SeasonId'],
                            "LocationId": data['0']['LocationId'],
                            "IsDelay": data['0']['IsDelay'],
                            "Status": data['0']['Status']
                        },
                        ],
    
    

    So I'm guessing the '0' is the index or position in the array, and therefore definitely not how I should be accessing it. So how do I map an array of data to unique rows in the datatable? I believe after the latest change my data structure now qualifies as a 'simple array' per this doc:

    https://datatables.net/manual/ajax

    I can't quite identify how what I'm doing is any different from that example. Hope you can help me understand what I'm missing.

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Yep, you're declaring the data incorrectly, see example here.

    Colin

  • dannyliadannylia Posts: 4Questions: 1Answers: 0

    Man after all that it was a really simple solution. After the feedback I received here I technically got it working by doing a complicated function for each column:

                    var table = $('#delayedmatches').DataTable({
                        data: data,
                        dom: 'Bfrtip',
                        columns: [
                        {data: null,
                        render: function(data, type, row, meta) {
                            
                            
                                var Date = row.Date;
                                return Date 
                                
                            }
                        },
                        {data: null,
                        render: function(data, type, row, meta) {
                                
                                var DelayDate = row.DelayDate;
                                return DelayDate 
                                
                            }
                        },
    [...]
    

    This finally got each row to render, but when moving on the the actual editing (using editor object and specifying the 'fields') I struggled for a long time getting it to identify the row. I finally realized that because my tables were joined I had no unique key for the returned data set. After simply specifying the idSrc (which is just a value returned from my join guaranteed to be unique) it all works. Just posting this for anyone else struggling with this concept, as you can see I was able to completely do away with the multiple functions.

    var table = $('#delayedmatches').DataTable({
        data: data,
        idSrc: 'gameid',
        dom: 'Bfrtip',
        columns: [
        {data: data['Date']
        },
        {data: data['DelayDate']
        },
        {data: data['matchid']
        },
        {data: data['gameid']
        },
    [...]
    
This discussion has been closed.