I am using datatable to display a list of products on my page. Each product have their own sequence value which is an integer. Some products do not have sequence value so they are assigned null by default. What I am trying to do is to display the products based on seq. For example, I have a product with seq 1 , another product with seq 2 and the others are null so it should display from smallest number to largest then followed by the null sequences.
What I am currently getting on my display is seq 2 then seq 1 and then null sequences. I want to display it based on priority (1 has higher priority than 2).
This is my code for the datatable:
var productTable = $('#datatable-product').DataTable({
processing: true,
serverSide: true,
searching: false,
bInfo: false,
bLengthChange: false,
autoWidth: false,
ajax: {
url: '/products/datatable',
data: function (d) {
d.product_name = $('input[name="product_name"]').val();
d.category_id = $('select#filter_category_id').find(":selected").val();
}
},
order: [[4, 'asc_nulls_last']],
columns: [
{ data: 'cover_image', name: 'cover_image', orderable: false },
{ data: 'product_name', name: 'product_name', defaultContent: '', className: 'product__name' },
{ data: 'category_name', name: 'category_name', defaultContent: '', className: 'product__category-name' },
{ data: 'status', name: 'status', className: 'text-center' },
{ data: 'seq', name: 'seq', className: 'text-center'},
{
data: 'updated_at',
name: 'updated_at',
className: 'text-center',
render: function (data, type, row, meta) {
if (row) {
return dateTimeDatatable(row.updated_at);
}
return '';
}
},
{ data: 'action', orderable: false, className: 'text-center' },
],
language: {
paginate: {
previous: '<',
next: '>'
}
}
});
My backend code for reference:
public function indexDatatable(GetProductRequest $request)
{
$parameters = $request->validated();
return $this->productService->productDatatable($parameters);
}
public function productDatatable(array $parameters)
{
$productList = $this->productRepository->productList($parameters);
return DataTables::of($productList)
->orderColumns(['product_name', 'category_name', 'status', 'seq', 'updated_at'],
':column ')
->addColumn('cover_image', function ($data) {
$coverImage = null;
$productImage = $data->managementFiles()->where(
'status',
ManagementFile::ACTIVATED
)->first();
if ($productImage) {
$coverImage = Common::getImageAzureBlobLink($productImage->url);
}
return view('admin.products.partials.common.image', compact('data', 'coverImage'))
->render();
})
->addColumn('status', function ($data) {
return view('admin.products.partials.common.badge', compact('data'))->render();
})
->addColumn('action', function ($data) use ($parameters) {
if (isset($parameters['is_top']) &&
$parameters['is_top'] === config('vip.product.is_top_product.yes')) {
return view('admin.top_product.partials.common.actions', compact('data'))
->render();
}
return view('admin.products.partials.common.actions', compact('data'))
->render();
})
->rawColumns(['status', 'action', 'cover_image'])
->make(true);
}
public function productList(array $parameters)
{
$model = $this->model
->select('products.*', 'product_lang.text as product_name', 'category_lang.text as category_name')
->leftJoin('languageables as product_lang', function ($join) {
$join->on('products.id', '=', 'product_lang.languageable_id')
->where('product_lang.languageable_type', Product::class)
->where('product_lang.type', Product::PRODUCT_NAME)
->where('product_lang.language_id', Language::EN);
})
->leftJoin('languageables as category_lang', function ($join) {
$join->on('products.category_id', '=', 'category_lang.languageable_id')
->where('category_lang.languageable_type', Category::class)
->where('category_lang.language_id', Language::EN);
})
->where(function ($query) use ($parameters) {
if (!empty($parameters['category_id'])) {
$query->where('products.category_id', $parameters['category_id']);
}
if (!empty($parameters['product_name'])) {
$query->where('product_lang.text', 'like', '%' . $parameters['product_name'] . '%');
}
if (isset($parameters['is_top'])) {
$query->where('products.is_top', $parameters['is_top']);
}
});
$model = $model->with([
'managementFiles',
]);
$this->resetModel();
return $model;
}
I am not sure why my datatable does not sort to what i want. How do I fix this issue?