Using PHP to query a MySQL DB within the datatable `columns: [`

Using PHP to query a MySQL DB within the datatable `columns: [`

WebCodexWebCodex Posts: 71Questions: 13Answers: 3
edited May 2018 in Free community support

I'm looking for a way to query a database with a function and render data in the column based on that returned data.
It's a simple true / false switch based on some user info.
This has to be done as the table is loaded.

                      { 'data': 'upvote', 
                    "render": function ( data, type, row, meta ) {
                        // Query DB - return true / false - return data
                        if(variableFromFunction == true) {
                            return data;
                        }
                        else {
                            return data + "<i class='upvote fas fa-thumbs-up'></i>";
                        }
                    },
                },

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    edited May 2018 Answer ✓

    Its not recommended to perform ajax requests in columns.render. Ajax is an async process plus it would slow down rendering the table. Can you return the desired data with your original ajax request? You don't need to display it in the table but if your DB query can return the data with the row data you can easily access it in columns.render.

    Kevin

  • WebCodexWebCodex Posts: 71Questions: 13Answers: 3

    Thanks, I should have thought of that! :smile:

This discussion has been closed.