How can I render child row to present markdown?

How can I render child row to present markdown?

koppanmkoppanm Posts: 22Questions: 3Answers: 1

Hi, I have been using DataTables and Editor more and more extensively. Recently I have installed a Table where I need to show large text data using markdown. The child row functions properly. However, I am unable to format it to render data for markdown. I have read all available sources, but I can not compile a proper script. Please, help!
Many thanks!
Miklos

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 65,470Questions: 1Answers: 10,874 Site admin

    Hi Miklos,

    Two options:

    1. Render it as HTML on the server-side through a Markdown library and a get formatter (assuming you are using the Editor PHP, .NET or Node.js libraries).
    2. Do it on the client-side with a library such as this one. You would do that wherever it is that you are creating the child rows.

    If you are able to link to an example, I should be able to give a more specific answer.

    Allan

  • koppanmkoppanm Posts: 22Questions: 3Answers: 1

    Many thanks Allan for your quick reply! I will give it a try and report back.
    I love DataTables and Editor, although I am an absolutely amateur, so it will not be easy for you to guide me through..

  • koppanmkoppanm Posts: 22Questions: 3Answers: 1
    edited June 2025

    Dear Allan,
    Had no time to accomplish so far, just to read the info you have sent. Many thanks.
    Below is my code, maybe this clarifies my trouble. I truly appreciate your help.

    /*
     * Editor client script for DB table opdescript
     * Created by http://editor.datatables.net/generator
     */
    
    
    addEventListener("DOMContentLoaded", function () {
        var editor = new DataTable.Editor( {
            ajax: 'php/table.opdescript.php',
            table: '#opdescript',
            fields: [
                {
                    "label": "created_at:",
                    "name": "created_at",
                    "type": "datetime",
                    "format": "YYYY-MM-DD"
                },
                {
                    "label": "tags:",
                    "name": "tags"
                },
                {
                    "label": "content:",
                    "name": "content",
                    "type": "textarea",
                    "attr": {
            "placeholder": 'Please enter a description here'
        }
                }
            ]
        } );
    
    
    
    
    // Formatting function for row details - modify as you need
    function format(d) {
         //`d` is the original data object for the row
        return (
            '<dl>' +
            '<dt><b>Leírás:</b></dt>' +
            '<dd>' +
            d.content +
            '</dd>' 
            +
            '</dl>'
        );
    }
    
    
    
        var table = new DataTable('#opdescript', {
            ajax: 'php/table.opdescript.php',
            order: [ 1, 'desc' ],
            columns: [
            {
                className: 'dt-control',
                orderable: false,
                data: null,
                defaultContent: ''
            },
            { data: 'created_at' },
            { data: 'tags' }
    
        ],
            // columns: [
                // {
                    //"data": "created_at"
                //},
                //{
                    //"data": "tags"
                //},
                //{
                    //"data": "content"
                //}
            //],
            
            layout: {
                topStart: {
                    buttons: [
                        { extend: 'create', editor: editor },
                        { extend: 'edit', editor: editor },
                        { extend: 'remove', editor: editor }
                    ]
                }
            },
            language: {
            url: '//cdn.datatables.net/plug-ins/2.3.1/i18n/hu.json',
            },
            select: true,
    
        });
    
        // Add event listener for opening and closing details
    $('#opdescript tbody').on('click', 'td.dt-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row(tr);
     
        if (row.child.isShown()) {
            // This row is already open - close it
            row.child.hide();
        }
        else {
            // Open this row
            row.child(format(row.data())).show();
        }
    });
    
    
    
    
    });
    
  • allanallan Posts: 65,470Questions: 1Answers: 10,874 Site admin

    Awesome - thank you. I would suggest that line 43 in the above is where you could do the Markdown formatting - something like:

    markdown(d.content) +
    

    where markdown() is a function from an external library. I've used markdown-it before and found it to be very effective.

    Allan

  • koppanmkoppanm Posts: 22Questions: 3Answers: 1
  • koppanmkoppanm Posts: 22Questions: 3Answers: 1

    Many thanks, I go ahead and give it a try. You are providing an awesome support, thanks. TwoThumbsUp!

  • koppanmkoppanm Posts: 22Questions: 3Answers: 1

    OK, now the child row is frozen. Table loads, but that is it.

  • koppanmkoppanm Posts: 22Questions: 3Answers: 1

    Dear Allan, after reading a lot, I am getting a little bit closer. Which default JS file have you used from the the package author (jsdelivr.com)?

  • koppanmkoppanm Posts: 22Questions: 3Answers: 1
    Answer ✓

    Dear Allan, I have solved it.
    I have found the solution here:
    https://stackoverflow.com/questions/1867669/markdown-live-preview-in-textarea

    It is now working perfect.
    Thank you for your suggestion, it pushed me forward to achieve what I needed.
    I love the Editor and Datatables. Many thanks, Miklos

  • allanallan Posts: 65,470Questions: 1Answers: 10,874 Site admin

    Hi Miklos,

    Awesome - delighted to hear that you've got it working :)

    Allan

Sign In or Register to comment.