How to call a function in "render"? (inside "data" - "columns")

How to call a function in "render"? (inside "data" - "columns")

dataphpmysqldataphpmysql Posts: 65Questions: 17Answers: 0

Hello,

So I'm trying to call a function on render that's inside the data which is inside the columns option. I'm using a function inside render to change the date format. But since I'm using it a lot in several columns, I would like to call the function instead of typing the whole block every time - in order to make the code cleaner. The function I'm trying to call is xDateFormat which is at the bottom of the script. I think I'm making the call incorrectly, any hints on how to make ti work?

Here is what I have so far:

<script>
        $(document).ready(function () {
            var table = $('#dtBasicExample').DataTable({
                ajax: {
                    method: 'GET',
                    url: 'https://thisiswheretheurlis.amazonaws.com/prod',
                    dataSrc: ''
                },
                columns: [
                    {
                        "className": 'details-control',
                        "orderable": false,
                        "data": null,
                        "defaultContent": ''
                    },
                    { "data": "ID" },
                    { "data": "Title" },
                    {
                        "data": "Date",
                        "render": xDateFormat(data, type, row)
                    }
                ]
            });
            $('.dataTables_length').addClass('bs-select');
            $('#dtBasicExample tbody').on('click', 'td.details-control', function () {
                var tr = $(this).closest('tr');
                var row = table.row(tr);

                if (row.child.isShown()) {
                    row.child.hide();
                    tr.removeClass('shown');
                }
                else {
                    row.child(format(row.data())).show();
                    tr.addClass('shown');
                }
            });
        });

        function xDateFormat(data, type, row) {
            var xformatDate = data.slice(0, 10);
            var xreplaceCharDate = xformatDate.replace(/-/g, "/");
            var dateSplit = xreplaceCharDate.split('/');
            return type === "display" || type === "filter" ?
                dateSplit[1] + '/' + dateSplit[2] + '/' + dateSplit[0] :
                data;
        }
    </script>

This question has an accepted answers - jump to answer

Answers

Sign In or Register to comment.