Click here to Skip to main content
16,017,231 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I have an issue that if I use DataTables Ajax render function for a conditional column it duplicates each row in table.

If I remove the below line then it works fine, but after adding this every row is displayed twice.

JavaScript
{ 
                        "data": null,
 
                render: function(data, type, row) { 
                var disc = row.ispercent == 0 ? row.discount : row.discount+'%';            
                return disc; 
                }
                     },


Please help, thanks in advance.

What I have tried:

JavaScript
 $("#classid").change(function(){
       var sessionid = $("#sessionid").val();
       var classid = $(this).val();
       $.ajax({
               url : "<?php echo site_url('Ajax/getStuDiscByClassID'); ?>",
               method : "POST",
               mydata : {sessionid: sessionid, classid: classid},
               async : true,
               dataType : 'json',
               success: function(mydata)
               {
$('#example').DataTable({
           destroy: true,
           data : data,
           columns: [
                   { data: "id"},
                   {data: "studentname"},
                    { data: 'fathername'},
                    {
                       "data": null,

               render: function(data, type, row) {
               var disc = row.ispercent == 0 ? row.discount : row.discount+'%';
               return disc;
               }
                    },
               ],
               dom: 'Blfrtip',
               buttons: [
               'copyHtml5',
               'excelHtml5',
               'csvHtml5',
               'pdfHtml5',
               ],

               search: true
        });
        }
        });
           return false;
   });
Posted
Updated 22-Jun-22 21:31pm
v4
Comments
[no name] 23-Jun-22 1:49am    
Hello,

the line " data : data, " is init for your data contents,
and " render: " is made for display too.

one error you did : using 'data' keyword as a var name.
you now have problems to debug, because of a mischoosen name of var.

keep in mind some keywords native must be kept as 'protected'. let them for the language you use. ( native already )

one other trick :
var disc = .......
could be only :
return row.ispercent == 0 ? row.discount : row.discount+'%';
nyt1972 23-Jun-22 2:33am    
Thanks dear the problem was in sql query, it was not duplicating in sql but in codeigniter I have to use distinct which resolved the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900