如果在免费的 jqgrid 列中单击,如何执行 ajax 调用并重定向到其他页面

2022-01-19 00:00:00 jquery javascript ajax jqgrid free-jqgrid

我正在寻找一种方法来执行添加到购物车"ajax 调用,以从单击的行中的其他列传递产品代码(行 ID)和数量,如果在 jqgrid 列中单击,则重定向到购物车页面.

I'm looking for a way to perform "Add to cart" ajax call to pass product code (row id) and quantity from other columns in clicked row and redirect to cart page if clicked in jqgrid column.

根据https://github.com/free-jqgrid/jqGrid/wiki/improvement-of-formatter:-"showlink"

showlink 格式化程序已改进,因此我尝试使用它.

showlink formatter is improved so I tried to use it.

我试过 colmodel

I tried colmodel

{"label":"Add to cart",
"name":"Addtocrt_addtocrt","search":false,"sortable":false,
"viewable":false,"formatter":"showlink","formatoptions":{"showAction":addToCartOnClick
}}

和方法

function addToCartOnClick(rowId, iRow, iCol, cellValue, e) {
    var 
     $quantity = $('#' + $.jgrid.jqID(rowId) + '>td:nth-child(' + (iCol + 1) + ')'),
     quantityVal;
    if (iCol < 0) {
        quantityVal = 1;
    } else
        if ($quantity.find('>input').length === 0) {
            quantityVal = $quantity.text();
        }
        else {
            quantityVal = $quantity.find('>input').val();
        }
    window.location = 'Store/AddToCart?' + $.param({
        id: rowId,
        quantity: quantityVal
    });
}

在 jree jqgrid 中没有调用 addToCartOnClick.

addToCartOnClick is not called in jree jqgrid.

在 jqgrid 4.6 动态链接格式化程序中

In jqgrid 4.6 dynamiclink formatter

onClick=addToCartOnClick 

按照 如果单击超链接,如何将数据从 jqgrid 行传递到 url

在免费的 jqgrid 中 addToCartOnClick 也不会从 dynamicLink 格式化程序中调用.

In free jqgrid addToCartOnClick is also not called from dynamicLink formatter.

如何在免费的jqgrid中调用方法并从点击的行中获取列值?

How to call method and get column values from clicked row in free jqgrid?

推荐答案

showAction只能用于设置URL部分.如果您想使用该选项,则必须使用 javascript: 前缀(请参阅 答案) 来启动 addToCartOnClick ,它被定义为 global 函数.

showAction can be used to set the part of URL only. It you want to use the option then you have to use javascript: prefix (see the answer) to start addToCartOnClick which mast be defined as the global function.

最好在 formatter: "showlink"formatoptions 中使用新选项 onClick.我在阅读您的之后直接对free jqGrid的代码进行了相应的修改问题.您应该从 GitHub 刷新您使用的源.现在你可以使用

The better would be to use new option onClick in formatoptions of formatter: "showlink". I made the corresponding changes of the code of free jqGrid directly after reading of your question. You should refresh the sources which you use from GitHub. Now you can use

{name: "Addtocrt_addtocrt", label: "Add to cart",
    search: false, sortable: false, viewable: false,
    formatter: "showlink",
    formatoptions: {
        onClick: function (options) {
            // object options contains properties, which could be helpful
            //    iCol - index of the column in colModel
            //    iRow - index of the row
            //    rowid
            //    cm - element of colModel
            //    cmName - the same as cm.name
            //    cellValue: the text inside of `<a>`
            //    a - DOM element of clicked <a>
            //    event - Event object of the click event
            location.href = "http://www.google.com/";
            return false; // it's important to suppress the default a action
        }
    }}

相关文章