Use multiple indexes in the same array on server side

Use multiple indexes in the same array on server side

XmLmX2XmLmX2 Posts: 4Questions: 2Answers: 0
edited January 2015 in Free community support

Hello guys. So I'm using DataTables and I love it, but I have a problem on server-side processing. I have this code:

$columns = array(
    array(
        'db'        => 'user_id',
        'dt'        => 0,
        'formatter' => function( $d, $row ) {
            return "<a href='user.php?id=" . $d . "'> USERNAME </a>";
        }
    )
);

Instead of USERNAME I want to select from database the column "username". How can I do that?

This question has accepted answers - jump to:

Answers

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited January 2015 Answer ✓

    I'm not sure if there is a better way on the server-side portion to do this with the stock ssp file. Instead of using the formatter you could also select the username column, then do something like this in the datatables JS portion to build the value of the cells to what you are looking for. Then you just need to hide the username column on the front end. I think you can just not even call it in the columns to accomplish that but still use the data from it.

    var table = $('#mytable').DataTable( {
        columns: [
            { data: 0,
                fnCreatedCell: function (nTd, sData, oData, iRow, iCol) {
                    $(nTd).html("<a href='user.php?id="+oData[0]+"'>"+oData[1]+"</a>");
                }
            }
        ]
    });
    
    $columns = array(
        array(
            'db'        => 'user_id',
            'dt'        => 0
        ),
        array(
            'db'        => 'username',
            'dt'        => 1
        )
    );
    

    You can also edit the ssp.class.php file if you need it to be more flexible. Here is an example of mine.

  • XmLmX2XmLmX2 Posts: 4Questions: 2Answers: 0

    Thank you, this works just fine, but what if I have more columns and I want them to display normally?

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited January 2015 Answer ✓

    Should be the same way, you'd just skip over the column you didn't want to display which is 'dt'=>1 in this example. The fnCreatedCell is optional.

    var table = $('#mytable').DataTable( {
        columns: [
            { data: 0,
                fnCreatedCell: function (nTd, sData, oData, iRow, iCol) {
                    $(nTd).html("<a href='user.php?id="+oData.0+"'>"+oData.1+"</a>");
                }
            },
            { data: 2 },
            { data: 3 },
            { data: 4 }
        ]
    });
    
    $columns = array(
        array(
            'db'        => 'user_id',
            'dt'        => 0
        ),
        array(
            'db'        => 'username',
            'dt'        => 1
        ),
        array(
            'db'        => 'first_name',
            'dt'        => 2
        ),
        array(
            'db'        => 'last_name',
            'dt'        => 3
        ),
        array(
            'db'        => 'address',
            'dt'        => 4
        )
    );
    
  • XmLmX2XmLmX2 Posts: 4Questions: 2Answers: 0

    Perfect! Thank you!

This discussion has been closed.