3

I'm using Laravel 5.4 and Yajra datatable, below is my code working properly but in the 2nd action I've created, The button is not displaying but instead it display the text itself "<a href="/product/'. $row->id .'/create-price" class="btn btn-primary">Add Price</a>" What am I missing ?

public function getProductDatatable()
    {
        $Product = Product::query();
       return Datatables::eloquent($Product)
        ->addColumn('action', function($row) {
            return '<a href="/product/'. $row->id .'/edit" class="btn btn-primary">Edit</a>';
        })

        ->addColumn('add_price', function($row) {
            return '<a href="/product/'. $row->id .'/create-price" class="btn btn-primary">Add Price</a>';
        })

       ->make(true);
    }

Frontend Part

 <script type="text/javascript">
        $(function() {
            $('#product-table').DataTable({
                processing: true,
                serverSide: true,
                ajax: '{{ url('product/get_product_datatable') }}',
                columns : [
                    {data: 'id', name: 'id'},
                    {data: 'product_code', name: 'product_code'},
                    {data: 'action', searchable: false, orderable: false},
                    {data: 'add_price', searchable: false, orderable: false},
                    {data: 'created_at', name: 'created_at'},
                    {data: 'updated_at', name: 'updated_at'}
                ]
            });
        }); 
        </script>
2
  • how do you render the view in the front end Commented Aug 17, 2017 at 3:46
  • @meda I already edited my question and added the frontend js Commented Aug 17, 2017 at 3:48

2 Answers 2

7

You need to define rawColumns :

public function getProductDatatable()
    {
        $Product = Product::query();
       return Datatables::eloquent($Product)
        ->addColumn('action', function($row) {
            return '<a href="/product/'. $row->id .'/edit" class="btn btn-primary">Edit</a>';
        })

        ->addColumn('add_price', function($row) {
            return '<a href="/product/'. $row->id .'/create-price" class="btn btn-primary">Add Price</a>';
        })
       ->rawColumns(['add_price', 'action'])
       ->make(true);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I found this issue on github, try adding rawColumns

 Datatables::eloquent($Product)
 ->addColumn(..)
 ->rawColumns(['add_price']);

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.