Chumper / Datatable

This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
https://github.com/Chumper/Datatable
388 stars 154 forks source link

Make custom values available inside the javascript template #301

Open rossanthony opened 9 years ago

rossanthony commented 9 years ago

Need to be able to pass custom values into the javascript template, currently there is a comment in the default javascript template // custom values are available via $values array which implies this is possible, however the values are not passed through. The change in this pull request corrects this.

Basically I want to be able to conditionally trigger the dataTable (RowReordering plugin)[https://code.google.com/p/jquery-datatables-row-reordering/wiki/Index] to run, this has to be chained onto the main call to inititalise the dataTable object. For example:

<script type="text/javascript">
    jQuery(document).ready(function(){
        // dynamic table
        oTable = jQuery('#{{ $id }}').dataTable({

        @foreach ($options as $k => $o)
        {{ json_encode($k) }}: {{ json_encode($o) }},
        @endforeach

        @foreach ($callbacks as $k => $o)
        {{ json_encode($k) }}: {{ $o }},
        @endforeach

     // custom values are available via $values array

     @if(isset($values['sortable_path']))
         }).rowReordering({ sURL: "{{$values['sortable_path']}}", sRequestType: "GET" });
     @else
         });
     @endif

   });
</script>`
timgws commented 9 years ago

@rossanthony have you tried using $table->setOptions(array('key' => 'value'));?

rossanthony commented 9 years ago

@timgws thanks for the reply, $table->setOptions(array('key' => 'value')) works for setting the options which are looped through in the template.

@foreach ($options as $k => $o)
        {{ json_encode($k) }}: {{ json_encode($o) }},
@endforeach

However I wanted to set a custom value to conditionally chain the rowReordering onto the dataTable object, like so:

@if(isset($values['sortable_path']))
         }).rowReordering({ sURL: "{{$values['sortable_path']}}", sRequestType: "GET" });
@else
         });
@endif

with the sortable_path being the endpoint for the server-side reordering script.

If there's a better way to achieve the same result then I'd appreciate any ideas you might have as to how else I could conditionally trigger the setup of the drag-n-drop row reordering for certain dataTables.