Dynamic column in jqgrid shows "Length of colNames <> colModel!" - php

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.

Related

FancyGrid ajax success Variable is not defined

I'm having a little bit trouble about sending a database data to a client side table. I'am using fancy grid.
Client Side
$.ajax({
url:'function.php?what=listofbookings',
type:'post',
data:{user:user},
success: function(data) {
var clients = data;
}
});
});
$("#jsGrid").jsGrid({
width: "100%",
height: "400px",
inserting: true,
editing: true,
sorting: true,
paging: true,
data: clients,
fields: [
{ name: "people", type: "text", width: 150, validate: "required" },
{ name: "email", type: "number", width: 50 }
]
});
And here is the PHP Code
if(isset($_GET['what'])){
if($_GET['what'] === 'listofbookings'){
$selit = "SELECT * FROM dbdbdb_booking";
$queryit = mysqli_query($conn,$selit);
$arr = array();
while($row = mysqli_fetch_assoc($queryit)){
$arr[] = $row;
}
echo json_encode($arr);
}
}
For some reason the var client is not defined and the data is not passing to clients.
Any thoughts ?
You need to initialize jsGrid inside your AJAX's success :
$.ajax({
url:'function.php?what=listofbookings',
type:'post',
data:{user:user},
success: function(data) {
var clients = data;
$("#jsGrid").jsGrid({
width: "100%",
height: "400px",
inserting: true,
editing: true,
sorting: true,
paging: true,
data: clients,
fields: [
{ name: "people", type: "text", width: 150, validate: "required" },
{ name: "email", type: "number", width: 50 }
]
});
}
});

how to display data in jqgrid using a post ajax request?

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:[...],
.
.
.
});

Refresh JSON from external PHP

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).

JqGrid Delete Function

I have some problem, I want to delete some row using JqGrid plugin
Here's my php delete function, the database colum for id is idms_department
if($oper == 'del'){
$deptid = $_REQUEST['idms_department'];
$del = "DELETE FROM ms_department WHERE idms_department =" . $deptid;
if(mysql_query($del)){
"Delete Successfull";
} else {
die("Error Delete: " .mysql_error()."SQL : " .$del);
}
mysql_close();
}
and my jqgrid setting
<script type="text/javascript">
$(document).ready(function() {
//alert("start");
jQuery("#departments").jqGrid({
mtype:'GET',
url:'functions/get_dept.php',
editurl:'functions/edit_dept.php',
datatype: "JSON",
colNames:['Department ID','Department'],
colModel:[
{name:'idms_department',index:'idms_department', width:150, editable:true,add:true, del:true, key:true},
{name:'department',index:'department', width:800,editable:true, add:true, del:true}
],
loadComplete: function () {
alert("OK");
},
loadError: function (jqXHR, textStatus, errorThrown) {
alert('HTTP status code: ' + jqXHR.status + '\n' +
'textStatus: ' + textStatus + '\n' +
'errorThrown: ' + errorThrown);
alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
},
rowNum:10,
rowList:[5,10,15],
pager: '#pager-departments',
sortname: 'idms_department',
viewrecords: true,
jsonReader: {repeatitems: true, idms_department: "idms_department" },
sortorder: "asc",
caption:"Example Departments"
});
jQuery("#departments").jqGrid('navGrid','#pager-departments',{edit:true,add:true,del:true});
jQuery("#departments").jqGrid('gridResize',{minWidth:350,maxWidth:850,minHeight:80, maxHeight:350});
//alert("end");
});
</script>
How to get value of idms_department, because I see in my firebug, the idms is not recognizing (null) so the sql function is falling.
I am pasting example code for you please refer it, may be it will be help you.
jQuery("#list_requisitos").jqGrid(
{
url:'../ajax/common_form_detail.php?form_id='+form_id,
editurl:'../ajax/common_form_edit.php?form_id='+form_id,
datatype: "json",
colNames:['D','ID','AREA','PIN Transportador','Fecha creacion','Aprob. Gestor Operativo','Aprob. Gestor Administrativo','Usuario que registra','Contratista','Fecha EjecucionDe la Tarea','Periodo','Anexos'],
colModel:[{name:'boton_grupos_tematicos',index:'boton_grupos_tematicos',sortable:false, align:'center',width:'50',search:false},{name:'id',index:'id',editable:false,hidden:true},{name:'object_area',
index:'object_area',
formoptions:{elmsuffix:''},
editable:true,
editrules:{required:true},
hidden:false,
search:true,
edittype:'select',
formatter:'select',
editoptions: {value: ''}
},{name:'pin_transportador',
index:'pin_transportador',
formoptions:{elmsuffix:''},editrules:{required:true},editable:true,
hidden:false,
search:true,
editoptions: {size:80, maxlength: 1000}
},{name:"created_date",
index:"created_date",
formoptions:{elmsuffix:"(aaaa-mm-dd)"},
sorttype:"date", editoptions:{dataInit: function(element) {$(element).datepicker({dateFormat: "yy-mm-dd"})}},
search:true,
type:"text",
searchoptions:
{
dataInit: seleccionarFecha,
attr: {title: "Selecciona una fecha"}
}
},{name:'app_by_codinator',
index:'app_by_codinator',
editable:true,
search:true,
edittype:'select',
formatter:'select',
editoptions: {value: '0:NO;1:SI;-1:RECHAZADO'}},{name:'app_by_manager',
index:'app_by_manager',
editable:true,
search:true,
edittype:'select',
formatter:'select',
editoptions: {value: '0:NO;1:SI;-1:RECHAZADO'}},{name:"user_id",index:"user_id",type:"text",editable:false,hidden:false},{name:"contractor_id",index:"contractor_id",type:"text",editable:false,hidden:true},{name:"actual_task_done_date",index:"actual_task_done_date",type:"text",editable:false,hidden:false,editoptions:{dataInit: function(element) {$(element).datepicker({dateFormat: "yy-mm-dd"})}},},{name:"sumerized_date",index:"sumerized_date",type:"text",editable:false,hidden:false},{name:"attachments",index:"attachments",type:"text",editable:false,hidden:false}],
rowNum:20,
rowList:[20,50,100],
pager: '#pager_requisitos',
sortname: 'id',
viewrecords: true,
multiselect: true,
sortorder: "asc",
autowidth: true,
height: 400,
width: 900,
caption:"RG05 IN340_pin_transportador",
});
jQuery("#list_requisitos").jqGrid('navGrid','#pager_requisitos',{ edit:true,add:true,del:true,search:false},{width:780,recreateForm:true},{width:780,recreateForm:true})
.jqGrid('filterToolbar',{stringResult: false,searchOnEnter : false, autosearch: true})
});
});
The documentation of delGridRow describes what will be sent to the server on Delete operation. The name of rowid is id instead of idms_department which you use in your code (see the line $deptid = $_REQUEST['idms_department'];). You should either use the name 'id' in the statement or add prmNames: {id: "idms_department"} option to jqGrid. It changes the default name of id used in Add/Edit/Delete operation.
Additionally I would recommend you remove non existing properties add:true, del:true from colModel and index properties which value are the same as values of name properties, remove default value repeatitems: true from jsonReader and add gridview: true and autoencode: true to the grid. You can additionally remove {edit:true,add:true,del:true} option (remove the last parameter) of navGrid because all the values are default (see the documentation).
$(".deletethisrecord").live('click',function(e){ e.preventDefault();
toDelete = $(this).parent().parent().attr('id');
$("#list2").jqGrid(
'delGridRow',
toDelete,
{ url: '<?php echo $edit; ?>',
reloadAfterSubmit:false}
);
});
});
use some thing like this. place delete in your record list.

How to update the fullcalendar

I'm a beginner with php and fullcalendar.I need to update the calendar with the user input value ($('#name')). But it doesn´t work.
I have this code.
$(document).ready(function() {
var UsrAux;
$('#name').blur(function(){
UsrAux = $('#name').val() // <-- This is the input
});
$('#calendar').fullCalendar({
draggable: true,
height: 400,
cache: true,
eventSources: [
// your event source
{
url: 'CalendarServer.php',
type: 'POST',
data: {
uno: 'Something',
UsrCdg: UsrAux
},
error: function() {
alert('error!');
},
color: '#e2ebef', // a non-ajax option
textColor: 'black' // a non-ajax option
}
]
});
});
Any help would be greatly appreciated!
The blur event function should be defined before the fullCalendar as it gets the data (the user input) you need.
Hi this is the solution.
$(document).ready(function(){
var UsrAux;
UsrAux=$('#name').val();
$('#name').blur(function(){
UsrAux=$('#name').val();
var source = {
url: 'CalendarServer.php',
type: 'POST',
data: { // Parms
uno: 'Somenthing',
UsrCdg: UsrAux
},
error: function() {
alert('error!');
},
color: '#e2ebef', // a non-ajax option
textColor: 'black' // a non-ajax option
};
$('#calendar').fullCalendar('removeEvents');
$('#calendar').fullCalendar('addEventSource', source);
});
$('#calendar').fullCalendar({
draggable: true,
height: 400,
cache: true,
eventSources: [
// your event source
{
url: 'CalendarServer.php',
type: 'POST',
data: { // Parms
uno: 'something',
UsrCdg: $('#name').val()
},
error: function() {
alert('error!');
},
color: '#e2ebef', // a non-ajax option
textColor: 'black' // a non-ajax option
}
]
});
});

Categories