so, trying make 1 generic page table being requested user. trying fetch data server , not having hardcoded on clientside.
$(document).ready(function(){ /* add/remove class row when clicked on */ $('#table1 tr').on('click', function() { $(this).toggleclass('row_selected'); } ); var which_table=window.location.pathname; var which_table_data=which_table.substring(0, which_table.length-1)+'/data'; var table_name=which_table.substring(14, which_table.length-1); $('#table1').datatable({ "bprocessing": true, "bserverside": true, "bjqueryui": true, "sajaxsource": which_table_data, "bpaginate": true, "spaginationtype": "full_numbers", "bjqueryui": true, "sscrollx":"100%", "aocolumndefs": [{ "targets" : [0], "visible" : false, "searchable" : false }] }).makeeditable({ "supdateurl": "../update/" + table_name, "saddurl": "../add/" + table_name, "sdeleteurl": "../delete/" + table_name, "aocolumns": $.ajax({ datatype: "json", url: '/get_aocolumns', data: function (table_name) { return { q: table_name }; }, results: function (data) { alert(data); } }); }); });
so in piece based on var which_table=window.location.pathname;
try data corresponding table server in successful. want aocolumns array server. question can send data in same request along aodata, secho , other required fields. think may not render datatable since aocolumns in not part of required json. how aocolumns table validation becomes server side , won`t have design 1 page per table.
this below part doesnt work don`t know correct way of doing it
"aocolumns": $.ajax({ datatype: "json", url: '/get_aocolumns',
edited :-
i tried @garryp`s approach ..
this data server
[{"cssclass": "required", "type": "textarea"}, {"supdateurl": "../update/my_table", "cssclass": "required", "type": "textarea", "loadtype": "post", "submit": "ok"}, {"loadurl": "../data/", "supdateurl": "../update/my_table", "loadtype": "post", "type": "select", "submit": "ok"}]
this code :
$(document).ready(function(){ /* add/remove class row when clicked on */ $('#table1 tr').on('click', function() { $(this).toggleclass('row_selected'); } ); var which_table=window.location.pathname; var which_table_data=which_table.substring(0, which_table.length-1)+'/data'; var table_name=which_table.substring(14, which_table.length-1); if(table_name.indexof('welog')> -1) { $('#table1').datatable({ "bprocessing": true, "bserverside": true, "bjqueryui": true, "sajaxsource": which_table_data, "bpaginate": true, "spaginationtype": "full_numbers", "bjqueryui": true, "sscrollx": "100%" }); $('#formaddnewrow').hide(); } else { $.ajax({ url: '../get_aodata/' + table_name, async: false, success: function (data) { alert(data); $('#table1').datatable({ "bprocessing": true, "bserverside": true, "bjqueryui": true, "sajaxsource": which_table_data, "bpaginate": true, "spaginationtype": "full_numbers", "bjqueryui": true, "sscrollx": "100%", "aocolumndefs": [{ "targets": [0], "visible": false, "searchable": false }] }).makeeditable({ "supdateurl": "../update/" + table_name, "saddurl": "../add/" + table_name, "sdeleteurl": "../delete/" + table_name, "sadddeletetoolbarselector": "#table1_length", "aocolumns" : data /*"aocolumns" : [ { "cssclass": "required", "type":"textarea" }, { "cssclass": "required", "type":"textarea", "submit" : "ok", "supdateurl": "../update/"+table_name, "loadtype" : "post" }, { "loadurl" : "../data/", "type" : "select", "submit": "ok", "supdateurl": "../update/"+table_name, "loadtype" : "post" } ]*/ }); } }); } });
so if see commented out aocolumns in code same output got server, 1 got server doesnt work , 1 commented out if uncommented work. 1 got server if used using aocolumns : data behaves same way if aocolumns parameter wasn
t mentioned @ in makeditable function. mean data not reaching parameter coz don`t errors in console.
solution :-
success : function(data){ var data_str= json.parse(data); });
ok. had convert json string jsobject using parse , worked.
it doesn't work because assigning return value of $.ajax(...)
aocolumns
here (when need assigning array of columns "aocolumns"):
}).makeeditable({ ... "aocolumns": $.ajax({
instead need make ajax call first. then, inside jquery success
function setup datatable.
$.ajax({ url: '/get_aocolumns', ... success : function(data) { // todo: put datatable code in here. // , assign `data` "aocolumns" /** data table code **/ }).makeeditable({ "aocolumns": data /** rest of data table code **/ }
i have tried leave important bits out make key points clear, should understand have gone wrong.
i've set js fiddle here (untested) code sample if doesn't make sense:
Comments
Post a Comment