英文:
problems with column ordering in JQuery DataTable
问题
I'm new to JQuery so please be patient.
我是新手 JQuery,请耐心等待。
I'm making a select that includes 5 options, including one to return all records.
我正在创建一个选择框,其中包括5个选项,其中一个选项用于返回所有记录。
<select id="sBusqueda" class="form-control fondo" name="sBusqueda" onchange="ModicaCampoTexto($(this));">
<option value="all">Todos</option>
<option value="nomProv">Nombre Proveedor</option>
<option value="nSolicitud">Nº Solicitud</option>
</select>
At the moment everything works fine and if I select the "all" option within my view, it reloads the records correctly, but... in ascending order and what I need is that it returns the data from highest to lowest.
目前一切都正常,如果我在视图中选择“all”选项,它会正确重新加载记录,但是...升序排列,而我需要的是按降序返回数据。
Do you know what I'm doing wrong?
你知道我做错了什么吗?
Here is my code:
以下是我的代码:
$(document).ready(function () {
var tablaSolicitud = $('#TablaSolicitudes').DataTable({
"order": [[0, 'desc']],
"processing": true,
"serverSide": true,
"searching": false,
//"ordering": false,
"language": {
"url": "//cdn.datatables.net/plug-ins/1.10.16/i18n/Spanish.json"
},
"ajax": {
"url": '@Url.Action("CargaSolicitudes", "DPSolicitudes")',
"type": "POST",
"datatype": "json"
},
"columnDefs": [{
"targets": [0],
"visible": false,
"searchable": false
}],
"columns": [
{ "data": "DPSolicitudId", "name": "DPSolicitudId", "autoWidth": true },
{ "data": "NumeroSolicitud", "name": "NumeroSolicitud", "autoWidth": true },
{ "data": "CentroCosto", "name": "CentroCosto", "autoWidth": true },
],
"lengthMenu": [[5, 10, 15, 20, 25, 50, 100, 99999], [5, 10, 15, 20, 25, 50, 100, 'TODOS']],
});
$('#sBusqueda').change(function () {
var opcionSeleccionada = $(this).val();
// Verificar si la opción seleccionada es "Todos"
if (opcionSeleccionada === 'all') {
// Esto funciona
// Here I need your help
var tablaReload = $('#TablaSolicitudes').DataTable();
tablaReload.order([[0, 'desc']]).draw();
}
});
});
I did not put the complete code because it would be very long, but the only thing I need is to know why it doesn't order the records from highest to lowest, thank you very much!
我没有放完整的代码,因为它会非常长,但我唯一需要知道的是为什么它不按降序排序记录,非常感谢!
英文:
I'm new to JQuery so please be patient.
I'm making a select that includes 5 options, including one to return all records.
<select id="sBusqueda" class="form-control fondo" name="sBusqueda" onchange="ModicaCampoTexto($(this));">
<option value="all">Todos</option>
<option value="nomProv">Nombre Proveedor</option>
<option value="nSolicitud">Nº Solicitud</option>
</select>
At the moment everything works fine and if I select the "all" option within my view, it reloads the records correctly, but... in ascending order and what I need is that it returns the data from highest to lowest.
Do you know what I'm doing wrong?
Here is my code:
$(document).ready(function () {
var tablaSolicitud = $('#TablaSolicitudes').DataTable({
"order": [[0, 'desc']],
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"searching": false,
//"ordering": false,
"language": {
"url": "//cdn.datatables.net/plug-ins/1.10.16/i18n/Spanish.json"
},
"ajax": {
"url": '@Url.Action("CargaSolicitudes", "DPSolicitudes")',
"type": "POST",
"datatype": "json"
},
"columnDefs": [{
"targets": [0],
"visible": false,
"searchable": false
}],
"columns": [
{ "data": "DPSolicitudId", "name": "DPSolicitudId", "autoWidth": true },
{ "data": "NumeroSolicitud", "name": "NumeroSolicitud", "autoWidth": true },
{ "data": "CentroCosto", "name": "CentroCosto", "autoWidth": true },
],
"lengthMenu": [[5 ,10, 15, 20, 25, 50, 100, 99999], [5, 10, 15, 20, 25, 50, 100, 'TODOS']],
});
$('#sBusqueda').change(function () {
var opcionSeleccionada = $(this).val();
// Verificar si la opción seleccionada es "Todos"
if (opcionSeleccionada === 'all') {
// Esto funciona
// Here I need your help
var tablaReload = $('#TablaSolicitudes').DataTable();
tablaReload.order([[0, 'desc']]).draw();
}
});
I did not put the complete code because it would be very long, but the only thing I need is to know why it doesn't order the records from highest to lowest, thank you very much!
答案1
得分: 1
Your DataTable is using "serverSide": true
. This means that all paging, filtering, and sorting is delegated to your server. DataTables does not perform any of that logic for you.
Specifically, when you choose your Todos
option, that causes the following line of code to be executed:
tablaReload.order([[0, 'desc']]).draw();
But that does not mean DataTables performs any sorting.
Instead...
-
The
order([[0, 'desc']])
sets the new sort order for the table. -
The
draw()
automatically triggers a request to be sent to your server, using the DataTable'sajax
option. -
That request contains the new sort order. It's in the
POST
request body.
A fragment of that request body may look like this:
{
"order": [
{
"column": 0,
"dir": "desc"
}
]
}
You can see the order([[0, 'desc']])
has been added to the Ajax request's JSON.
It's up to your C# code to parse the request body and then use this sorting instruction (along with the other request fields) to provide the new page of results in the requested sort order.
You haven't shown us any of your C# code. But that is where you need to look.
英文:
Your DataTable is using "serverSide": true
. This means that all paging, filtering, and sorting is delegated to your server. DataTables does not perform any of that logic for you.
Specifically, when you choose your Todos
option, that causes the following line of code to be executed:
tablaReload.order([[0, 'desc']]).draw();
But that does not mean DataTables performs any sorting.
Instead...
-
The
order([[0, 'desc']])
sets the new sort order for the table. -
The
draw()
automatically triggers a request to be sent to your server, using the DataTable'sajax
option. -
That request contains the new sort order. It's in the
POST
request body.
A fragment of that request body may look like this:
{
"order": [
{
"column": 0,
"dir": "desc"
}
]
}
You can see the order([[0, 'desc']])
has been added to the Ajax request's JSON.
It's up to your C# code to parse the request body, and then use this sorting instruction (along with the other request fields) to provide the new page of results - in the requested sort order.
You haven't shown us any of your C# code. But that is where you need to look.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论