I cant seem to find a way to display the data using post ajax
When i predefined the json data inside a variable it does read. But when i retrieve the data from the server using post ajax and display it in jqgrid it doesnt seem to work.
Heres my code:
var grid_selector = "#grid-table";
var pager_selector = "#grid-pager";
$.post(pathFile+"loadPaymentDetails",{id:id,action:'loadPaymentDetails'},function(response,status){
var result = [{"0":"3","id":"3","1":"82","payment_id":"82","2":"0000-00-00 00:00:00","payment_issue_date":"0000-00-00 00:00:00","3":"100","payment_ref_number":"100","4":"0","payment_mode_id":"0","5":"121212","payment_amount":"121212","6":"","payment_description":""}];
if (status == "success") {
var grid_data = response
$(grid_selector).jqGrid({
data: grid_data,
datatype: "local",
height: 250,
colNames:[' ', 'payment_issue_date','payment_ref_number'],
colModel:[
{name:'myac',index:'', width:80, fixed:true, sortable:false, resize:false
},
{name:'payment_issue_date',index:'payment_issue_date', width:60, sorttype:"int", editable: true},
{name:'payment_ref_number',index:'payment_ref_number',width:90, editable:true, sorttype:"date"},
],
viewrecords : true,
rowNum:10,
rowList:[10,20,30],
pager : pager_selector,
altRows: true,
//toppager: true,
multiselect: true,
//multikey: "ctrlKey",
multiboxonly: true,
//editurl: $path_base+"/dummy.html",//nothing is saved
caption: "jqGrid with inline editing",
autowidth: true
});
}
})
I can display the data in jqgrid using the result variable but when i use the response from the ajax call it doesnt seem to work although there output is the same.
I use:
$("#tab_vsebine").jqGrid({
mtype: 'POST',
url:<your ajax url call>,
postData: {param1:value, param2:value,...},
datatype: "json",
colNames:[...],
colModel:[...],
.
.
.
});
Related
Please anyone help me in fixing this. I am trying add dynamic column names and column models information from the php script named simple8.php and the data is obtaining from the simple7.php.
<script type="text/javascript">
jQuery(function($) {
var seq= $('#sim').text();
var seq= seq.replace(/[\“\”\″]/ig,'\"');
var seq = jQuery.trim(seq);
var title = $('#title_for_table').text();
var title = jQuery.trim(title);
$.ajax({
url:"simple8.php"+seq,
type: "POST",
data: "",
dataType: "json",
success: function (data) {
var ColN = data.colNames;
var ColM = data.colModel;
jQuery("#tableid").jqGrid({
url: "simple7.php"+seq,
datatype: "jsonstring",
mtype: "POST",
colNames: ColN,
colModel: ColM,
pager: '#pager',
rowNum:50,
rowList:[25,50,75,100],
sortname: 'r1',
sortorder: "asc",
viewrecords: true,
gridview: true,
height: '100%',
width: '100%',
caption:title+" Search Results",
loadComplete: function(reload) {
jQuery("#tableid").trigger("reloadGrid");
},
});
jQuery("#tableid").jqGrid('navGrid','#pager',{add:false,edit:false,del:false});
jQuery("#tableid").jqGrid('navButtonAdd','#pager',{caption: "Export",onClickButton: function () { //exportExcel($(this));
ExportJQGridDataToExcel($(this),'file1.xls');
},position:"last"
});
},
error: function () {
alert("Error with AJAX callback");
}
});
});
</script>
the output of simple8.php is:
{"colNames":"['col','r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10', 'r11']","colModel":"[{name:'col', index:'col', width:120},{name:'r1', index:'r1', width:120},{name:'r2', index:'r2', width:120},{name:'r3', index:'r3', width:120},{name:'r4', index:'r4', width:120},{name:'r5', index:'r5', width:120},{name:'r6', index:'r6', width:120},{name:'r7', index:'r7', width:120},{name:'r8', index:'r8', width:120},{name:'r9', index:'r9', width:120},{name:'r10', index:'r10', width:120},{name:'r11', index:'r11', width:120}]"}
and simple7.php gives:
{"total":1,"page":1,"records":4,"rows":[{"id":null,"cell":["Chaffa","R","R","C","A","C","A","A","T","T","G","G"]},{"id":null,"cell":["CSG8962","R","R","Y","A","C","A","A","T","T","G","S"]},{"id":null,"cell":["DCP92_3","G","A","C","M","C","A","A","T","W","G","G"]},{"id":null,"cell":["DigVijay","R","A","C","A","C","A","A","T","T","G","S"]}]}
Please let me know where i am wrong.
The value of colNames is a string instead of array of items. The same one can say about colModel. You should change the data returned from the server to use only " instead of ' and then use colNames: $.parseJSON(ColN) and colModel: $.parseJSON(ColM) instead of colNames: ColN, colModel: ColM. Without replacing ' with " you will have error in parsing of JSON in $.parseJSON.
As of now I am passing parameter along with URL in ajax call of data table.
But I want to pass it as POST method, please anyone one help me regarding parameter passing in post method, here's my trial code:
// Sending through GET
var $table = $('#example').dataTable(
"processing": true,
"serverSide": true,
"bDestroy": true,
"bJQueryUI": true,
"ajax": 'getResult.php?formName=afscpMcn&action=search&mcn_no='+mcnNum+'&cust_nm='+cust_num+'&emp_id='+emp+''
});
Just pass it like a normal jQuery ajax in POST fashion.
The structure should look like this:
ajax: { type: 'POST', url: <path>, data: { your desired data } }
Example:
var $table = $('#example').dataTable(
"processing": true,
"serverSide": true,
"bDestroy": true,
"bJQueryUI": true,
"ajax": {
'type': 'POST',
'url': 'getResult.php',
'data': {
formName: 'afscpMcn',
action: 'search',
// etc..
},
}
});
In PHP, just access the POST indices as usual (just the straightforward approach):
getResult.php
$form_name = $_POST['formName'];
// the rest of your values ...
DataTables manual entry
You can try this way:
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"data": function ( d ) {
d.extra_search = $('#extra').val();
}
}
});
https://datatables.net/reference/option/ajax.data
$("#tbl").dataTable({
oLanguage: {
sProcessing: '<div id="loader"></div>'
},
bProcessing: true,
"bServerSide": true,
"iDisplayLength": pageSize,
"sAjaxSource": " /PurchaseOrder/AddVendorItems", // url getData.php etc
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
aoData.push({ "name": "where", "value": ID +" AND ISNULL(IsFinal,0) = "+ ($("#chkFinal").bootstrapSwitch('state') == true ? 1 : 0) });
aoData.push({"name": "PackIDFK", "value": $("#PackIDFK").val()}) //pushing custom parameters
oSettings.jqXHR = $.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
} });
This is real time example.The aoData contains all the parameters which is required on server side and you can also push your own custom parameters
$(document).ready(function() {
$("#myDataList").jqGrid({
url: "<?= base_url(); ?>getmydata/getCategory",
datatype: "json",
autowidth: true,
mtype: "GET",
colNames: ["Category", "Description", ""],
colModel: [
{name: "c_category", editable: true, width: 1},
{name: "c_description", editable: true, width: 4, edittype: 'textarea'},
{name: "categoryid", key: true, editable: true, hidden: true, editrules: {edithidden: false}}
],
pager: "#myDataListPager",
rowNum: 10,
rowList: [10, 20, 30],
sortname: "c_category",
sortorder: "asc",
height: 'auto',
viewrecords: true,
gridview: true,
caption: "",
editurl: "<?= base_url(); ?>getmydata/c_action"
});
jQuery("#myDataList").jqGrid('navGrid', '#myDataListPager', {add: true, edit: true, del: true, search: true},
{closeAfterEdit: true}, {closeAfterAdd: true}, {
beforeShowForm: function($form) {
$("td.delmsg", $form[0]).html("Do you want to delete the row with <b>id=" +
$("#myDataList").jqGrid('getGridParam', 'selrow') + "</b>?");
},
afterSubmit: function(response) {
response = $.parseJSON(response.responseText);
alert(response.error);
if (response.error == 0) {
$.openDialog("info", "Successfully deleted ");
} else {
showDialog($('#Dialog'), response.msg, "An error occured");
}
}}
);
});
Above is my grid code, When I click delete the id is the rowid instead of the categoryid, how can I set the id to categoryid instead of row id. I would like to show a dialog message if the response.error is true. When I alert the response.error, I can get the correct result, but I don't know how to call the dialog to display.
http://mymoney.webege.com/getmydata/getCategory?_search=false&nd=1408865724728&rows=10&page=1&sidx=c_category&sord=asc <-- this is the URL I get the json data.
Above is my return json, and I check when click the delete the id is the row id in sequence.
You use key: true for the column "categoryid". So the values of rowid and categoryid have to be identical. You should post an example of testdata which can be used to reproduce the problem or you can debug the code yourself to understand why rowid and categoryid are different in your case.
You second problem with afterSubmit is easy to solve. You should don't show any dialogs inside of afterSubmit. Instead of that you should just return the required information in the correct format. afterSubmit of form editing have to return array of up to 3 items. In case of Delete operation if could be up to 2 items. If no error take place then you should return [true]. In case of error you should return [false, "error description"]. If your case it would be something like below
afterSubmit: function (response) {
var data = $.parseJSON(response.responseText);
return (data != null && data.error !== 0) ? [false, data.msg] : [true];
}
I want to refresh my data, obtained from an external PHP page, every second. The PHP sends JSON data, retrieved with an AJAX call. With this code, I see the table correctly, but I need to refresh my browser to see the new data.
$(document).ready(function() {
$("#list").jqGrid({
url: 'get_data.php',
datatype: 'json',
mtype: 'GET',
jsonReader: {
repeatitems : false,
},
colNames: [.............],
colModel: [.............],
autowidth: true,
height: 'auto',
loadonce: true,
key: true,
altRows: true,
altclass: 'odd',
rowNum: 100,
viewrecords: true,
gridview: true,
gridComplete: function(){
if(this.x == undefined){
var j = 0;
this.x = 1;
while(j < mydata2.length){
jQuery("#list").addRowData(mydata2[j].id, mydata2[j]);
j++;
}
}
return true;
}
})
});
To update the data I've already tried this:
var $grid = $("#list"), timer;
timer = setInterval(function () {
$grid.trigger('reloadGrid', [{current: true, datatype: 'json', url: 'get_data.php'}]);
}, 1000);
And this:
var refreshId = setInterval(function() {
// ... jqGrid function ...
}, 1000);
but neither worked.
The main error is the usage datatype: 'json', url: 'get_data.php' as an option of reloadGrid. The method reloadGrid understand only two options: current and page (see the answer). What you need is to reset datatype option of jqGrid because of usage loadonce: true. So you need replace the line
$grid.trigger('reloadGrid',
[{current: true, datatype: 'json', url: 'get_data.php'}]);
called inside of setInterval to the following
$grid.jqGrid("setGridParam", {datatype: "json"})
.trigger("reloadGrid", [{current: true}]);
It should solve you main problem with refreshing of data. Additionally I would recommend you to examine the code of gridComplete which seems me suspected. I don't full understand it. You should take in consideration that addRowData calls internal updatepager method which calls gridComplete and can follows to recursion (see the line of code and the answer).
I'm trying to load an array in my javascript. I need to send this array in some format to a PHP script that I'm going to call. In the example below, gSelectedMeds is my array. The value count will tell the PHP script how many meds items to expect to receive. I'm having trouble getting the data from the array into a format that I can send via the data option of $.ajax. Any help would be greatly appreciated!!
The part of the code below that is giving me grief at the moment is the data option:
$('#export').click(function() {
$.ajax({
url:'ajax-exportMeds.php',
data:
{"number":gSelectedMeds.length},
$.each(gSelectedMeds,
function(intIndex, objValue){
{"med"+intIndex:objValue},
}
),
type: "GET",
//dataType: "text",
success: function(data){
$('p#allMeds').text('');
$('a.bank').text('');
//clear array, bank and storedList divs
$(this).text('');
gSelectedMeds[] = '';
//$('ul#storedList').fadeOut('fast');
$('ul#storedList').text('');
return false;
},
}),
});
You should send the data as json. Then you can read it using json_decode() in php >= 5.2.0
I ended up stringing out the array and sending a count at the end of the url that I called:
$('#export').click(function() {
$.each(gSelectedMeds,
function(intIndex, objValue) {
i=intIndex + 1;
if(i>1) {string+='&';}
string+='med'+i+'="'+objValue+'"';
}
)
string += "&count="+i;
$.ajax({
url: 'ajax-exportMeds.php?'+string,
type: "GET",
dataType: "text",
success: function(data){
$('#dialog_layer').dialog({
autoOpen: true,
bgiframe: true,
modal: true,
closeOnEscape: true,
buttons: {
"OK": function() { $(this).dialog("close"); }
}
})
}
})
});