0
Good afternoon, I wanted to order a column in ascending or descending order I have a table (the photo attached)I was able to order the title field in ascending and descending order but now I wanted to order the column of dates(BEGINNING OF THE CYCLE).
follows in attachment the images of the codes. Obs: the beginning column of the cycle is ordering the dates until a certain line dps scrambles the rest of the dates.
Javascript code Function loadTable () {
var table = $('table#table-planos');
table.DataTable({
"processing": true,
serverSide: true,
ajax: {
url: "/plano/apis/past_cycles_info/",
type: "GET"
},
columns: [
{"data":'id'},
{"data":'progresso'},
{"data":'nome'},
{"data":'responsavel'},
{"data":'inicio_ciclo'},
{"data":'previsao_conclusao'},
{"data":'situacao'},
{"data":'frequencia_execucao'},
{"data":'id'}
],
"aaSorting": [[2,'asc']],
"aaSorting": [[4,'asc']],
"aaSorting": [[5,'asc']],
"aaSorting": [[4,'desc']],
"aaSorting": [[5,'desc']],
language: language_br,
createdRow: function( row, data, dataIndex ) {
if ( data["data_conclusao"]) {
$(row).prop( "class", "color-finalizadas" );
}
},
initComplete: function (data) {
},
columnDefs: [
{
'targets' : 0,
'searchable': false,
'orderable': false,
'className': 'info-link',
'render': function (data, type, full, meta) {
return '<td><a href="/plano/visual/' + full.id + '/"><i class="fa fa-eye"></i></a></td>';
}
},
{
'targets' : 1,
'searchable': false,
'orderable': false,
'className': 'progresso-td',
'render': function (data, type, full, meta) {
return '<div id="' + full.id + '" class="progress progress-xs"><div class="progress-bar" style="width:'+ full.progresso + '%" data-transitiongoal="' + full.progresso + '" aria-valuemin="0" aria-valuemax="100"></div></div>';
}
},
{
'targets' : 4,
'searchable': false,
'orderable': true,
'className': 'inicio_ciclo',
'render': function (data, type, full, meta) {
return '<td> ' + full.inicio_ciclo + '</td>';
}
},
{
'targets' : 6,
'searchable': false,
'orderable': false,
},
{
'targets' : 7,
'searchable': false,
'orderable': false,
},
{
'targets' : 8,
'searchable': false,
'orderable': false,
'className': 'info-link',
'render': function (data, type, full, meta) {
if (full["data_conclusao"]){
var reabrirButton = '\
<td>\
<a onclick="setID('+full.id+')" id="f'+ full.id +'" style="cursor:pointer;" data-toggle="modal" data-target="#reabrir-modal">\
<i class="fa fa-lock"></i>\
</a>\
</td>';
return reabrirButton;
}
else{
var finalizarButton = '\
<td>\
<a onclick="setID('+full.id+')" id="f'+ full.id +'"" style="cursor:pointer;" data-toggle="modal" data-target="#finalizar-modal">\
<i class="fa fa-unlock"></i>\
</a>\
</td>';
return finalizarButton;
}
}
}
]
});
Python code
DATATABLEdicionario_conversao_order_by = { 'name':'name', 'situation':'id', 'inicio_ciclo':'data_inicial', 'previsao_conclusion':'id', 'frequencia_execution':'id', 'responsible':'responsible',
}
class Datatablelistcreateapi(Generics.Listcreateapiview):
pagination_class = DataTablePagination
search_parameters = ()
default_order_by = ''
unfiltered_query_set = None
def get_queryset(self):
self.unfiltered_query_set = query_set = super(DataTableListCreateAPI, self).get_queryset()
order_by_index = int(self.request.query_params.get('order[0][column]', 0))
orderable = bool(self.request.query_params.get('columns[{}][orderable]'.format(order_by_index), 'false'))
if order_by_index == 0 or not orderable:
order_by_index = 1
order_by = self.request.query_params.get('columns[{}][data]'.format(order_by_index),
self.default_order_by).replace('.', '__')
order_by = dicionario_conversao_order_by[order_by]
order_by_dir = self.request.query_params.get('order[0][dir]', 'asc')
if order_by_dir == 'desc':
order_by = '-{}'.format(order_by)
search_queries = [self.request.query_params.get('search[value]', '').strip()]
# search_queries = self.request.query_params.get('search[value]', '').strip().split(' ')
q = Q()
if len(search_queries) > 0 and search_queries[0] != u'':
for params in self.search_parameters:
for query in search_queries:
temp = {
'{}__icontains'.format(params): query,
}
q |= Q(**temp)
query_set = query_set.filter(q).distinct()
if order_by == '':
return query_set
return query_set.order_by(order_by)
def get(self, request, *args, **kwargs):
result = super(DataTableListCreateAPI, self).get(request, *args, **kwargs)
result.data['draw'] = int(request.query_params.get('draw', 0))
result.data['recordsFiltered'] = result.data['count']
result.data['recordsTotal'] = self.unfiltered_query_set.count()
del result.data['count']
result.data['data'] = result.data['results']
del result.data['results']
return result
Put the code as text, read here the why.
– Augusto Vasques