Datatable icon indicating copy to clipboard operation
Datatable copied to clipboard

Make custom values available inside the javascript template

Open rossanthony opened this issue 10 years ago • 2 comments

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>`

rossanthony avatar Apr 28 '15 15:04 rossanthony

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

timgws avatar Apr 29 '15 01:04 timgws

@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.

rossanthony avatar Apr 29 '15 08:04 rossanthony