More short coding?

More short coding?

fatihfxfatihfx Posts: 9Questions: 3Answers: 0
edited July 2020 in Free community support

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

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    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 the return 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:

        render: function(data, type, row, meta){
    
              tarih1 = data
              tarih2 = moment(new Date()).format("YYYY-MM-DD HH:mm:ss")          
              var tarih_gecmismi = moment(tarih2).isAfter(tarih1)
              if (tarih_gecmismi)
                return tarih1+"<br>Süresi Geçmiş <i class=\"far fa-exclamation-circle\" style=\"color:red\"></i>";
              else
                return tarih1+"<br>Teklif Verilebilir <i class=\"far fa-check-circle\" style=\"color:green\"></i>";
            },
    

    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

  • fatihfxfatihfx Posts: 9Questions: 3Answers: 0
    edited July 2020

    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.

    {
            "targets": 3,
            render: function (data, type, row, meta) {
                var currentCell = $("#fatura_veri_tablosu").DataTable().cells({"row":meta.row, "column":meta.col}).nodes(0); // mevcut targets  
                //var Cell7 = $("#fatura_veri_tablosu").DataTable().cells({"row":meta.row, "column":7}).nodes(0); // değiştirilecek diğer hücre no
                $.ajax({
                    "type": "GET",
                    url: '../includes/teklif_detay_datatables_ajax.php',
                    data:"okunacak_alan=teklif_toplam_tutar&fatura_id="+row[1],
                }).done(function (data) {
                  if (data>0)
                    //{$(currentCell).html("Verildi  <i class=\"far fa-check-circle\" style=\"color:green\"></i>");}
                    {return ("Verildi  <i class=\"far fa-check-circle\" style=\"color:green\"></i>")}
                    //{$(currentCell).text("Verildi");}
                  else
                    //{$(currentCell).html("Beklemede <i class=\"far fa-exclamation-circle\" style=\"color:red\"></i>");}
                  {return ("Beklemede <i class=\"far fa-exclamation-circle\" style=\"color:red\"></i>")}
                    //$(Cell7).text(data);                               
                });
                return 'veri uyuşmazlığı';
            }
        },
    
  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    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

  • fatihfxfatihfx Posts: 9Questions: 3Answers: 0

    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.

This discussion has been closed.