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

标签 javascript jquery ajax jqgrid free-jqgrid

我正在寻找一种方法来执行“添加到购物车”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 

按照 How to pass data to url from jqgrid row if hyperlink is clicked 中的描述工作

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

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

最佳答案

showAction仅可用于设置URL 部分。如果您想使用该选项,则必须使用 javascript:前缀(参见 the answer )开始 addToCartOnClick哪个桅杆被定义为全局函数。

最好使用新选项 onClickformatoptionsformatter: "showlink" .我做了 the corresponding changes阅读您的问题后直接获取免费 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
        }
    }}

关于javascript - 如果在免费的 jqgrid 列中单击,如何执行 ajax 调用并重定向到其他页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29450773/

相关文章:

javascript - 更改按钮 onclick

javascript - Webpack/Babel es2015错误:未捕获语法错误:意外的 token 导入

JavaScript 函数作用于自身以用另一个对象扩展自身

javascript - 为什么这个属性不想被删除

javascript - 我想限制用户在 jquery 中的文本后输入空格,如果在文本后输入空格,将删除该空格?

javascript - 试图将我的 div 的高度设置为窗口高度的 80%

javascript - 我怎样才能去掉签名板上的中间线

javascript - Ajax调用无法正常工作

javascript - AJAX 调用成功

javascript - XHR在onreadystatechange中获取请求URL