I am using DataTables and I found a way to add a button in the column that I need, the dilemma is that I cannot concatenate the id that each row brings, I have tried in different ways and it does not work for me, the code is as follows: I make the request by Ajax and I pass the parameters to another function with a foreach
function CargaTareas() {
var xusuario = $('#cbousuarios').val();
var xestado = $('#cboestado').val();
$.ajax({
type: "POST",
url: "<?= base_url();?>index.php/control/cicompras/docargatareas",
dataType: 'json',
dataSrc: '',
data: {
usuario: xusuario,
estado: xestado
}
}).done(function(response){
$.each(response, function(index, value){
Llenar(response, index, value);
});
});
}
Here in the Fill function I receive what I collect...
function Llenar(response, index, value) {
var tablaTv = $('#productos').DataTable({
pageLenght: 5,
lengthMenu: [[5, 10, -1], [5, 10, 'Todos']],
language: {
...
}
},
destroy: true,
data: response,
columns: [
{defaultContent: '<a href="<?= base_url();?>index.php/control/cicompras/detalletarea/'+ ... +'">Ver</a>'},
// {data: "numero"},
{data: "fecha"},
{data: "titulo"},
{data: "glosa"},
{data: "prioridad"},
{data: "ufecha"},
{data: "umensaje"}
]
});
}
In the defaultContent column in "+ ...+ " I need to concatenate what {data: "number"} brings, so it doesn't work for me, it shows me [object Object], I've already parsed it and it doesn't work. I have also concatenated "response[index].number" and in all the rows the id of the last record is concatenated. I have also declared a variable outside the DataTable to concatenate it and it doesn't work for me either... Has it happened to someone else? were you able to fix it?
Hello, there is a datatable function to render within the columns, according to the render datatable Documentation and from my own experience, I would try to do it in the following way:
target
indicate in which column you want the columns to be rendered you indicate them as the indices of an array, for example, [0]=first column ...etc, or -1,-2,-3 if you start from right to left target: -1 because in my example it is the last rowIn this way I render buttons with the datatable, I recommend you first try to render the button first, then with console.log(data) see how you can access your id.
Related documentation
Data rendering data table
Column defs datatable
columns render datatable