Forum


17:58

14/11/2012

I have jqGrid working, but I'm trying to load a image into a cell. The way jqGrid get initialized is that we get the jqGrid configuration from the server in JSON format. What I'm having trouble is getting the "formatter" to initialize properly from the JSON data.
I know some of the code has been removed such as the localization strings, trying to keep it as simple as I can. Below you will see the first index to colModel is the formatter that I'm trying to get jqGrid to initalize that I want it to call the function to insert the html img tag, but jqGrid will not do it. Is there something speical that I need to do for formatter to work properly when loading from JSON data?
PHP Code to return JSON data:
$results = array(); $results['rowNum'] = 100; $results['rowList'] = array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100); $results['sortname'] = 'lastname'; $results['sortorder'] = 'asc'; $results['caption'] = array(htmlspecialchars($caption)); $results['addtext'] = array(htmlspecialchars($addtext)); $results['edittext'] = array(htmlspecialchars($edittext)); $results['deltext'] = array(htmlspecialchars($deltext)); $results['searchtext'] = array(htmlspecialchars($searchtext)); $results['refreshtext'] = array(htmlspecialchars($refreshtext)); $results['viewtext'] = array(htmlspecialchars($viewtext)); $results['emptyrecords']= array(htmlspecialchars($emptyrecords)); $results['loadtext'] = array(htmlspecialchars($loadtext)); $results['colNames'] = array(htmlspecialchars($type), htmlspecialchars($firstname), htmlspecialchars($lastname), htmlspecialchars($company), htmlspecialchars($email)); $types = array('contact:' . htmlspecialchars($contact), 'group:' . htmlspecialchars($group)); $results['colModel'][] = array( 'name' => 'type', 'index' => 'type', 'width' => '16', 'editable' => true, 'edittype' => 'image', 'formatter' => 'js:contactTypeImage'); $results['colModel'][] = array( 'name' => 'firstname', 'index' => 'firstname', 'width' => '80', 'editable' => true); $results['colModel'][] = array( 'name' => 'lastname', 'index' => 'lastname asc, firstname', 'width' => '80', 'editable' => true); $results['colModel'][] = array( 'name' => 'companyname', 'index' => 'companyname', 'width' => '80', 'editable' => true); $results['colModel'][] = array( 'name' => 'email1address', 'index' => 'email1address', 'width' => '80', 'editable' => true); echo json_encode($results);
Javascript Code:
$.ajax({ type: 'GET', url:'/contacts/initgrid', data: '', datatype: 'json', success: function(response) { var caption = response.caption; var addtext = response.addtext; var edittext = response.edittext; var deltext = response.deltext; var searchtext = response.searchtext; var refreshtext = response.refreshtext; var viewtext = response.viewtext; var emptyrecords = response.emptyrecords; var loadtext = response.loadtext; var rowNum = response.rowNum; var rowList = response.rowList; var sortname= response.sortname; var sortorder = response.sortorder; var columnNames = response.colNames; var columnModel = response.colModel; $("#contact_list").jqGrid( { url:'/contacts/list', datatype: "json", colNames: columnNames, colModel: columnModel, autowidth: true, height: 'auto', rowNum: rowNum, rowList: rowList, toppager: true, pager: '#contact_list_pager', sortname: sortname, viewrecords: true, sortorder: sortorder, emptyrecords: emptyrecords, loadtext: loadtext, caption: caption, loadComplete : function() { // Remove the close button on the toolbar $('.ui-jqgrid-titlebar-close').remove(); } } ).jqGrid('navGrid','#contact_list_toppager', { edit: true, add: true, del: true, refresh: true, view: true, addtext: addtext, edittext: edittext, deltext: deltext, searchtext: searchtext, refreshtext: refreshtext, viewtext: viewtext, }, { recreateForm: true, beforeInitData: function () { alert("hello beforeInitData - 1"); } }, { url: '/contacts/modify', closeAfterEdit: true, }, { url: '/contacts/create', closeAfterAdd: true }, { url: '/contacts/delete' }, { // search options } ).jqGrid('navGrid','#contact_list_pager', { edit: false, add: false, del: false, refresh: false }, { recreateForm: true, beforeInitData: function () { alert("hello beforeInitData - 2"); } } ); } }); }); function contactTypeImage(cellValue, options, rowObject) { alert(">>> in contactTypeImage"); var imageHtml = "[Image Can Not Be Found]"; return imageHtml; }
Most Users Ever Online: 715
Currently Online:
38 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66