1

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

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

showlink 格式化程序得到了改进,所以我尝试使用它。

我试过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。

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

onClick=addToCartOnClick 

如果单击超链接,则按照如何将数据从 jqgrid 行传递到 url中所述工作

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

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

4

1 回答 1

2

showAction只能用于设置URL 的一部分。如果您想使用该选项,那么您必须使用javascript:前缀(参见答案)来启动addToCartOnClick将哪个桅杆定义为全局函数。

更好的办法是onClickformatoptionsof中使用新选项formatter: "showlink"。看了你的问题,我直接对free jqGrid的代码做了相应的修改。您应该从 GitHub 刷新您使用的源。现在你可以使用

{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
        }
    }}
于 2015-04-05T10:57:26.603 回答