0

I am using the jquery datatables plugin and codeigniter. In one of my views I have:

 <table id="myDataTable" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>

            <?php foreach($keys as $key): ?>
            <th><?php echo $key; ?></th>
            <?php endforeach; ?>


        </tr>

      </thead>      
            <tbody>

        </tbody>
    </table>


<script type="text/javascript">
$(document).ready(function () {
    var oTable = $('#big_table').dataTable({
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": 'datatable_controller/datatable',
        "sPaginationType": "full_numbers",
          "columns": [
                    { "data": "id" },
                    { "data": "message_id" },
                    { "data": "subject" },
                    { "data": "date" }
                  ],

in reference to the columns structure, right now I am hard coding in column field names (id, message-id,subject , date ) . I already have these in an array $keys, which I am using above to dynamically generate the table HTML. Is there a way to pass $keys directly into the "columns" option?

1
  • Any reason not to use json data? Commented Aug 8, 2014 at 20:46

1 Answer 1

1

It looks like you could use the PHP loop inside the "columns" option in your javascript, if I'm following the format correctly. Something like this (untested obviously)?

      "columns": [
               <?php foreach($keys as $key): ?>
                 {"data": "<?php echo $key ?>"},
               <?php endforeach; ?>
              ],
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.