how do you add custom code in a cell?

how do you add custom code in a cell?

koniahinkoniahin Posts: 186Questions: 39Answers: 7

Say I have a table with 4 or 5 fields:

id, title, preview (a link), publish, code

The code fields needs to display some shortcode based on the $id, something like:

{code%get-gallery.php%6%2}

i.e.

$arg1 = is code
$arg2 = name of code to execute
$arg3 = limit (# of images to display)
$arg4 = # bootstrap columns

In a simple situation the arguments are static. In a more complex version the arguments will vary based on the $id and some other fields associated with that table.

q1) how would you set up the static version?
q2) how do you set up the complex version?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    Hi @koniahin ,

    For both, you can use columns.render - that will allow you to define in real-time what that column will display. You could build that string exactly as you like.

    I'm not sure what you mean by "code to execute", you won't be able to execute code in that table, but it will respond to standard jQuery events as in this example,

    Cheers,

    Colin

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7

    code to execute - misnomer my bad. the goal is to display a shortcode to the administrator which he/she can copy into an article. the shortcode/code is to be dynamically created by some information associated with the listed item. it will most likely require on running a subquery against that item to get some potential arguments.

    at this time i'm shooting from the hip; will check into columns.render

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Ideally you would want to include that information in the data returned to the client-side on initial request rather than firing off an Ajax request for every cell. For starters DataTables' render method doesn't handle async cases, although you could use cell().data() to update a cell's data once the Ajax returns, but secondly you'd quickly DDoS your own server!

    Allan

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7
    Answer ✓

    columns.render worked; looked at several examples, came up with the following:

      { data: null, render: function ( data, type, row ) {
        return '{gallery%get-gallery.php%'+data.id+'%$arg3%$arg4}';
        }
      }
    

    I also had to add the id in the list of columns and an ID column in the table display

    { data: 'id' },
    

    and also add added an the php processing script.

    Field::inst( 'id' ),
    

    I'm using % as the field separator. Pretty happy camper. :)

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7

    I'm figuring this stuff out, slowly, methodically. Regarding the ID I found the option to hide the ID in the table display extending columndefs:

    columnDefs: [
      {
        orderable: false, targets: [ 1,2,3 ]
      },
      {
        "targets": [ 1 ],
        "visible": false
        ,"searchable": false
      }
    ],
    
This discussion has been closed.