Search code examples
javascriptlaravel-5datatablestranslationyajra-datatable

Yajra/Laravel-datatables setting alias for table header


I have list of products under some category and if I click on category sorting then it shows same category and product name. It is happening only with the category sorting. Other sorting are working perfectly. I am stuck here and unable to resolve this also I have no Idea why category.name is being used. If I changes this to category_name then category sorting stops working. I've tried everyhting.
How can I use Alias for category name?? And in which file do I need to make changes.

    var table = $('#product-table').DataTable({
                processing: true,
                serverSide: true,
                bStateSave: true,
                ordering: true,
                dom: 'Bfrtip',
                buttons:[],
                ajax: '{{ URL::to('/admin/products.data') }}',
                order: [[1, 'asc']],
                columnDefs: [
                    { orderable: false, targets: 0 }
                ],
                columns: [
                    {data: 'edit', name: '', searchable:false},
                    {data: 'name', name: 'name'},
                    {data: 'product_code', name: 'product_code'},
                    {data: 'category_name', name: 'category.name'},
                    {data: 'impa_code', name: 'impa_code'},
                    {data: 'issa_code', name: 'issa_code'},
                    {data: 'status', name: 'is_active'}
                ],
                "deferRender": true
            });


**This is my blade file**


`<div class="row clearfix">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 m-t-50">
        <div class="card">
            <div class="body">
                <table class="table table-striped table-hover dataTable" id="product-table">
                    <thead>
                    <tr>
                        <th class="col-sm-1"></th>
                        <th>{{ Lang::get('views.name') }}</th>
                        <th>{{ Lang::get('views.shipskart_code') }}</th>
                        <th>{{ Lang::get('views.category_name') }}</th>
                        <th>{{ Lang::get('views.impa_code') }}</th>
                        <th>{{ Lang::get('views.issa_code') }}</th>
                        <th>{{ Lang::get('views.status') }}</th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>`




This is Controller



` ->editColumn('product_code', function ($product) {
                $productCode = $product['product_code'];
                return $productCode;
            })
            ->addColumn('category_name', function ($product) {
                return empty($product->category) ? 'N/A' : $product->category->name ;
            })
            ->editColumn('impa_code', function ($product) {
                $impaCode = $product->impa_code;
                return $impaCode;
            })
            ->editColumn('issa_code', function ($product) {
                $issaCode = $product->issa_code;
                return $issaCode;`

Solution

  • I believe that is Laravel datatable

    If it is a naming issue

    and you want to change just the header name to be category.name or anything else .. in the blade view that is showing the table, you should find the headers and you can change it from there accordingly.

    change this line

    <th>{{ Lang::get('views.category_name') }}</th>
    

    to

    <th>{{ the new name }}</th>
    

    or if you want it to reflect the language code also, you need to amend your language files.

    Go to the Views, in the specific language that you want to change and update the category_name file

    like for example, if you want to change the category_name to CAT title in en language .. go to /resources/lang/en or wherever you are saving your lang/en folder and amend the line

    'category_name' => 'CAT title'

    If it is a data issue

    and it is not reflecting the data that you are expecting, always check the link that is producing this issue and in your case

    /admin/products.data

    from there you can know what exactly are coming through so you can show it in your table accordingly .. you can check the DataTable Manual

    so if you are receiving product array which has a key called name .. that is going to be a product.name in the same way JavaScript is reading the the json.