从 JqGrid ShowLink click 发送 Ajax 请求

发布于 2024-09-17 12:52:07 字数 1567 浏览 5 评论 0原文

当用户单击 jqGrid 中的链接时,我需要加载一些动态 html。

这是我的定义

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
                {}
    );
};

,您可以看到我想加载修改表单。 我如何告诉 jqGrid 在单击 showLink 时执行 ajax 调用? 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2024-09-24 12:52:07

可能的解决方案如下所示:

  1. 在“用户名”中,删除不需要的 edittype: 'select'align: 'left' 并考虑添加 title: false 而是在鼠标悬停在单元格上时删除工具提示的显示。
  2. 将包含 formatter: 'showlink'formatoptions 修改为以下内容: formatoptions: { baseLinkUrl: 'javascript:', showAction: "MyBase.GetAndShowUserData(jQuery('# userlist'),'#myDiv','", addParam: "');" }。 jqGrid 将构造 元素的 href 属性,如下所示: href="javascript:MyBase.GetAndShowUserData(jQuery('#userlist'),'# userDetails','?id=rowId');" 其中 rowId 将是相应网格行的 id。
  3. 添加一个 global 函数(在 JavaScript 的顶层定义),例如名称为 MyBase.GetAndShowUserData 的函数,如下所示:(

在下面的代码中,我们使用全局 MyBase 仅用于命名空间)

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);
            }
        });
    }
};

我想在这里,您的页面上有一个

或其他带有 id="userDetails" 的元素

<table id="userlist"></table>
<div id="pager"></div>
<div id="userDetails"></div>

,并且函数 MyBase.GetAndShowUserData 将进行 ajax 调用并将结果填充到

中。 MyBase.GetAndShowUserData 内部的代码只是一个原始模板。我只想展示如何从网格访问数据。

A possible solution could looks like following:

  1. In the 'Username' you remove unneeded edittype: 'select' and align: 'left' and consider to add title: false instead to remove displaying of the ToolTip on hover a cell with the mouse.
  2. Modify formatoptions which contain formatter: 'showlink' to following: formatoptions: { baseLinkUrl: 'javascript:', showAction: "MyBase.GetAndShowUserData(jQuery('#userlist'),'#myDiv','", addParam: "');" }. jqGrid will construct href attribute of <a> element like following: href="javascript:MyBase.GetAndShowUserData(jQuery('#userlist'),'#userDetails','?id=rowId');" where rowId will be the id of the corresponding grid row.
  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文