laravel in set data useing datatable with ajax call



Blade File

<div class="table-responsive" style="font-size:12px;">
    <table id="subCategoriesTable" class="table table-striped">
        <thead>
            <tr>
                <th>Sl. No</th>
                <th width="50">Image</th>
                <th>Category Name</th>
                <th>Name</th>
                <th>Description</th>
                <th>Is Active</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

<script type="text/javascript">
    var route_product_sub_categories = "{{ route('getSubCategory') }}";
    jQuery(document).ready(function ($) {
        $('#subCategoriesTable').DataTable({
            "destroy": true,
            "bPaginate": true,
            "bLengthChange": true,
            "bFilter": true,
            "bSort": true,
            "bInfo": false,
            "bAutoWidth": true,
            "bProcessing": true,
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]], // Sets up the amount of records to display            "language": {
                "loadingRecords": '&nbsp;',
                "processing": '<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i><span class="sr-only">Loading..n.</span>',
                "oPaginate": {
                    "sNext": '<i class="fa fa-angle-right"></i>',
                    "sPrevious": '<i class="fa fa-angle-left"></i>',
                    "sFirst": '<i class="fa fa-angle-double-left"></i>',
                    "sLast": '<i class="fa fa-angle-double-right"></i>'                }
            },
            'ajax': {
                "type": "get",
                "url": route_product_sub_categories,
                "data": function (d) {
                    d.status = $("#ch_status").val();
                },
                "dataType": 'json',
                "dataSrc": ""            },
            'columnDefs': [
                {
                    targets: 0,
                    className: "text-right",
                    render: function (data, type, row) {
                        return  row['sub_cat_id'];
                    }
                },
                {
                    targets: 1,
                    orderable: false,
                    render: function (data, type, row) {
                        return '<span class="img-circle groceryImg pull-left"><img src="' + row['image_url'] + '" alt="' + row['sub_cat_name'] + '"></span>';
                    }
                },
                {
                    targets: 2,
                    className: "text-left",
                    render: function (data, type, row) {
                        if (row['cat_is_active'] == 1) {
                            var cat_status = "Active";
                        } else {
                            var cat_status = "InActive";
                        }
                        return  row['cat_name'] + " - " + cat_status;
                    }
                },
                {
                    targets: 3,
                    className: "text-left",
                    render: function (data, type, row) {
                        return  row['sub_cat_name'];
                    }
                },
                {
                    targets: 4,
                    className: "text-left",
                    render: function (data, type, row) {
                        return  row['sub_cat_description'];
                    }
                },
                {
                    targets: 5,
                    className: "text-center",
                    orderable: false,
                    render: function (data, type, row) {

                        if(row['sub_cat_is_active'] === 0) {
                            var is_active = '<span class="label label-danger font-size-90">Inactive</span>';
                        } else {
                            var is_active = '<span class="label label-success font-size-90">Active</span>';
                        }
                        return is_active;
                    }
                },
                {
                    targets: 6,
                    orderable: false,
                    className: "text-center",
                    render: function (data, type, row) {
                        return '<a data-toggle="tooltip" data-original-title="Edit" href="#" id="' + row['sub_cat_id'] + '"  class=" editProductSubCategory" data-toggle="modal" data-target="#modal" data-original-title="Edit">  <i class="fa  fa-pencil text-inverse m-r-10"></i></a>';
                    }
                },
            ],
            "order": [[0, 'asc']]
        });
    });
</script>


Web.php

Route::get('/product/subcategory/get', 'ProductSubCategoryController@getSubCategory')->name('getSubCategory');


Controller

public function getSubCategory(Request $request) {    
    $productsubcategory = $this->getAllSubCategory(['cat_is_enabled' => 1]);        foreach ($productsubcategory as $index => $subCategory) {        $imageURL           = ImageHelper::getThumbnailImage($subCategory->sub_cat_id, '31x40', 'subcategory');        $productsubcategory[$index]->image_url = $imageURL;    }    echo json_encode($productsubcategory);}


Trait(Model)

public function getAllSubCategory($where){    $_objProduct_sub_category_view = ProductSubCategoryView::where($where)->get();    return $_objProduct_sub_category_view;}



























Popular posts from this blog

Ajax Toggle(on/off) on-click Update

Yii Framework In Update Time View Image In Form