I need to load some dynamic html when a user click on a link in a jqGrid.
here is my definition
function loadUserAdministrationList() {
jQuery("#userlist").jqGrid({
url: '/Administration/GetData/',
datatype: 'json',
mtype: 'GET',
colNames: ['Username', 'Prénom', 'Nom', 'Courriel'],
colModel: [{ name: 'Username', index: 'Username', width: 300, align: 'left',
edittype: 'select', formatter: 'showlink',
开发者_运维百科 formatoptions: { baseLinkUrl: '/Administration/ModifyUser'} },
{ name: 'Prénom', index: 'Firstname', width: 300, align: 'left' },
{ name: 'Nom', index: 'Lastname', width: 300, align: 'left' },
{ name: 'Courriel', index: 'Email', width: 300, align: 'left'}],
pager: jQuery('#userlistpager'),
rowNum: 20,
rowList: [5, 10, 20, 50],
sortname: 'Firstname',
sortorder: "asc",
height:600,
viewrecords: true,
imgpath: '/Public/css/theme/custom/images',
caption: '',
loadtext: 'Chargement des utilisateurs...'
}).navGrid('#userlistpager',
{ search: true, edit: false, add: false, del: false },
{}, // default settings for edit
{}, // default settings for add
{}, // default settings for delete
{ closeOnEscape: true, multipleSearch: true,
closeAfterSearch: true }, //search options
{}
);
};
as you can see i would like to load a modification form. How can i tell jqGrid to do an ajax call on the click of showLink ? Thanks
A possible solution could looks like following:
- In the 'Username' you remove unneeded
edittype: 'select'
andalign: 'left'
and consider to addtitle: false
instead to remove displaying of the ToolTip on hover a cell with the mouse. - Modify
formatoptions
which containformatter: 'showlink'
to following:formatoptions: { baseLinkUrl: 'javascript:', showAction: "MyBase.GetAndShowUserData(jQuery('#userlist'),'#myDiv','", addParam: "');" }
. jqGrid will constructhref
attribute of<a>
element like following:href="javascript:MyBase.GetAndShowUserData(jQuery('#userlist'),'#userDetails','?id=rowId');"
whererowId
will be the id of the corresponding grid row. - Add a global function (defined on the top level of your JavaScript) for example with the name
MyBase.GetAndShowUserData
like following:
(In the code below we use global MyBase
only for namespacing)
var MyBase = {};
MyBase.GetAndShowUserData = function (grid,tagetDivSelector,param) {
// param will be in the form '?id=rowId'. We need to get rowId
var ar = param.split('=');
if (grid.length > 0 && ar.length === 2 && ar[0] === '?id') {
var rowid = ar[1];
var username = grid.getCell(rowid, 'Username');
var userDetails = jQuery(tagetDivSelector);
userDetails.empty();
jQuery.ajax({
url: '/Administration/ModifyUser',
data: { userId: rowid, userName: username },
type: 'GET',
// optional contentType (depend on your server environment):
// contentType: 'application/json',
dataType: 'json',
success:function(data,st) {
var s = "BlaBla";
// TODO: construct HTML code based on data received from the server
userDetails.html(s);
},
error:function(xhr,st,err){
alert(st + ": " + data.responseText);
}
});
}
};
I suppose here, that on your page there are a <div>
or other element with id="userDetails"
like
<table id="userlist"></table>
<div id="pager"></div>
<div id="userDetails"></div>
and the function MyBase.GetAndShowUserData
will make an ajax call and fill the results inside the <div id="userDetails"></div>
. The code inside of MyBase.GetAndShowUserData
is a raw template only. I wanted only to show how you can access the data from the grid.
精彩评论