More short coding?
More short coding?

I have this code, its working. but I want write less code if possible. How can I write short more currentcell variable ? Can I use field name directly ?
{
"targets": 5,
"className": "text-nowrap",
// add in any other column options you want too
render: function(data, type, row, meta){
var currentCell = $("#fatura_veri_tablosu").DataTable().cells({"row":meta.row, "column":meta.col}).nodes(0);
tarih1 = data
tarih2 = moment(new Date()).format("YYYY-MM-DD HH:mm:ss")
var tarih_gecmismi = moment(tarih2).isAfter(tarih1)
if (tarih_gecmismi)
{$(currentCell).html(tarih1+"<br>Süresi Geçmiş <i class=\"far fa-exclamation-circle\" style=\"color:red\"></i>");}
else
{$(currentCell).html(tarih1+"<br>Teklif Verilebilir <i class=\"far fa-check-circle\" style=\"color:green\"></i>");}
return null;
},
},
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
This discussion has been closed.
Answers
It is not expected that you access the HTML directly from within
columns.render
, ie, this statement:var currentCell = $("#fatura_veri_tablosu").DataTable().cells({"row":meta.row, "column":meta.col}).nodes(0);`
The
data
parameter contains the cell data. The key is to use thereturn
statement to return the data you want in the cell instead of writing it directly to the DOM, ie,{$(currentCell).html()
. You will want something more like this:Didn't test this but should get you close to what you need. If you need further help please build a simple test case replicating the data so we can give more specific help.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Thank you. Your code is working. I did not try because already not working in ajax.
My code is server side, so I can not provide test case.
This code not work with return, only work with $(currentCell).html.
Its not recommended to use an ajax request in columns render. You will be sending one or more requests per row for each table draw. It would be best to fetch all of the data in one ajax request then in the
success
function of that ajax request save the data in an global object variable and initialize your Datatable.Then in
columns.render
just access the object by the ID to render the column.This example is for dynamically creating a Datatable from Ajax data which that process doesn't apply here but it should give you an idea of what I'm describing.
http://live.datatables.net/huyexejo/1/edit
Kevin
My table data's getting from server side. Each table have 1000+ record, some table 10000+. Also some table columns need to read data from another database with complex SQL. so I can solve only this way.
If I solve return problem I will be very happy.