personnalize data from json

personnalize data from json

gtraxxgtraxx Posts: 2Questions: 0Answers: 0
edited October 2011 in General
Hello, I have a small question
For some time I built my data tables manually as I get to display items contained in the database.
this method is quite useful but somewhat limited anyway when processing a lot of recording.
This method allows me to view items each with a link to go to the issue and a link to delete.
I am interested strongly datatable which seems fine for this sort of thing.
But is it possible to have the same thing?
In the sense that I need for each item to have his little pencil editing that leads to a real page.
I have not seen this kind of example in the documentation but may have you ever used this way
For the moment I do like this:

[code]
$.ajax({
url: 'json.php?list_pages=true',
dataType: 'json',
type: "get",
async: true,
cache:false,
beforeSend: function(){
$('#list_cms_pages').html('');
},
success: function(j) {
$('#list_cms_pages').empty();
var tablecat = ''
+'ID'
+'Langue'
+'Titre'
+'Contenu'
+'Date de création'
+''
+''
+''
+'';
tablecat += '';
$(tablecat).appendTo('#list_cms_pages');
if(j === undefined){
console.log(j);
}
if(j !== null){
$.each(j, function(i,item) {
switch(item.contentpage){
case 1:
var content = '';
break;
case 0:
var content = '';
break;
}
return $(''
+''+item.idpage+''
+''+item.cms_lang+''
+''+item.titlepage+''
+''+content+''
+''+item.date_page+''
+''
+''
+'').appendTo('#tablecontain tbody');
});
}else{
return $(''
+''
+''
+''
+''
+''
+''
+''
+'').appendTo('#tablecontain tbody');
}
}
});
[/code]

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited October 2011
    yes. I recommend using the fnRender function for your pencil column to create the link and icon

    the main modifications you'll need to make are:
    1) your json.php will need to return an object that contains your data (in a member named aaData) and some attributes about the data as specified in http://www.datatables.net/usage/server-side. you can find a php script that runs the back-end server processing at http://www.datatables.net/release-datatables/examples/server_side/server_side.html.

    Use datatable's bServerSide: true to enable server side processing, and specify your json.php in the sAjaxUrl property when initializing datatables. see: http://www.datatables.net/ref#bServerSide

    2) for your pencil and close columns, specify a "fnRender" function (in your aoColumns initialization object) that converts the item.idpage into your links. fnRender simply returns a string that becomes the content of your cell. DataTables will pass in an object to your function that lets you know the column, row, an array of all data in that row, and an object for the settings of your datatable

    see http://www.datatables.net/ref#fnRender
  • gtraxxgtraxx Posts: 2Questions: 0Answers: 0
    edited October 2011
    Thank you, I'll try but right now my JSON looks like this:
    [code]
    [{"idpage":"1","titlepage":"ma page cms","contentpage":1,"cms_lang":"fr","date_page":"2011-06-15 11:26:32"}]
    [/code]
    return json for my script is written above how can I adapted to datatable
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Use mDataProp as described in this post: http://datatables.net/blog/Extended_data_source_options_with_DataTables . You can also tell DataTables to expect only an array by setting sAjaxDataProp to an empty string: http://datatables.net/ref#sAjaxDataProp

    Allan
This discussion has been closed.