Get cell reference inside render function

Get cell reference inside render function

LoLo2207LoLo2207 Posts: 3Questions: 1Answers: 0

Hi, I'm a newbie with JS, JQuery and DataTables, and I have an assignment where after filling a table, I have to make a QR code in one of the columns.

I'm using this JS library http://jeromeetienne.github.io/jquery-qrcode/

It needs a reference to where to insert the qr code

jquery('#whereToInsert').qrcode("What to encode");

How can i get the reference to the current cell inside the render function?

Answers

  • LoLo2207LoLo2207 Posts: 3Questions: 1Answers: 0

    Ok i've managed to render the code using aoColumDefs and fnCreatedCell, but feels like a sloppy way of doing it.

    Any better solution?

    Also I need to try to make it prettier by showing a link and then the code. Any ideas?

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    edited May 2018

    With a quick look at the plugin, I would have done something like:

    ...
        columns: [{ data: "id" },
            { data: "name" },
            { data: "encryption_key", render: function (dataItem) { return "<div data-qrcode='" + dataItem + "' class='qrcode'></div>" } },
            { data: "version" },
            { data: "description" }
        ],
        createdRow: function (row) { $(".qrcode", row).qrcode($(".qrcode", row).data("qrcode")); }
    ...
    
  • LoLo2207LoLo2207 Posts: 3Questions: 1Answers: 0

    The div is created succesfully, but the createdRow gives me this error

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    but feels like a sloppy way of doing it.

    Why? It looks okay to me! You need to wait for DataTables to have created the cell and columns.createdCell or createdRow is how that can be done.

    Allan

This discussion has been closed.