【问题标题】:free-jqGrid search button does not work on second clickfree-jqGrid 搜索按钮在第二次单击时不起作用
【发布时间】:2015-10-28 20:53:25
【问题描述】:

搜索按钮在第一次单击时起作用,但是一旦通过单击 X(关闭按钮)或运行搜索(设置为搜索后关闭)关闭它就不会打开,也没有错误控制台,所以我无法确定出了什么问题以及如何解决它。

var previouslySelectedRow = null;
var rowIsSelected = null;
var previousRowIsSelected = null;
var currentRowId;
var currentCount;
var cancelEditing = function(theGrid) {
    var lrid;
    if (typeof previouslySelectedRow !== "undefined") {
        // cancel editing of the previous selected row if it was in editing state.
        // jqGrid hold intern savedRow array inside of jqGrid object,
        // so it is safe to call restoreRow method with any id parameter
        // if jqGrid not in editing state
        theGrid.jqGrid('restoreRow', previouslySelectedRow);

        // now we need to restore the icons in the formatter:"actions"
        lrid = $.jgrid.jqID(previouslySelectedRow);
        $("tr#" + lrid + " div.ui-inline-edit").show();
        $("tr#" + lrid + " div.ui-inline-save, " + "tr#" + lrid + " div.ui-inline-cancel").hide();
    }
};

var parsedResult = JSON.parse(DecodeAscii(result));
ShowDebugNotification("DEBUG INFO(" + ajaxCall + "): <br />" + result, false);
$("#productsTable").jqGrid({
        data: parsedResult,
        datatype: "local",
        loadonce: true,
        height: 'auto',
        marginLeft: 'auto',
        colNames: [
            'Product Id', 'Add', 'Product Name', 'Product Code', 'Customer Price'
        ],
        colModel: [
            { name: 'Id', width: 0, hidden:true },
            { name: "actions", template: "actions", width: 50, formatoptions:{
                delbutton: false,
                editbutton: false
            } },
            { name: 'Name', index: 'Name', width: 550, search: true, searchoptions:{sopt:['eq','bw','bn','cn','nc','ew','en']} },
            { name: 'ProductCode', index: 'ProductCode', width: 150, search: true, searchoptions:{sopt:['eq','bw','bn','cn','nc','ew','en']} },
            { name: 'Price', index: 'Price', width: 100, search: false, formatter: 'currency', formatoptions:{decimalSeparator:".", thousandsSeparator: ",", decimalPlaces: 2, prefix: "$"}}
        ],
        rowNum: 15,
        rowList: [5, 10, 15, 20],
        pager: true,
        gridView: true,
        viewrecords: true,
        iconSet: "jQueryUI",
        sortname: 'Name',
        sortorder: 'asc',
        inlineEditing: { keys: false },
        actionsNavOptions: {
            addToCarticon: "ui-icon-cart",
            addToCarttitle: "Add item to the cart",
            custom: [
                { action: "addToCart", position: "first", onClick: function (options) { 
                    var rowData = $('#productsTable').getRowData(options.rowid);
                    var cartButton = $(".ui-icon", "#jAddToCartButton_"+options.rowid);
                    if(cartButton.hasClass("ui-icon-cancel")){
                        cart.shift(rowData);
                        cartButton.removeClass("ui-icon-cancel");
                        cartButton.addClass("ui-icon-cart");
                    }
                    else if(cartButton.hasClass("ui-icon-cart")){
                        cart.push(rowData);
                        cartButton.removeClass("ui-icon-cart");
                        cartButton.addClass("ui-icon-cancel");
                    }
                }
            }]
        },
        loadComplete: function() {
            $("#add-product-dialog-loading-message").hide();
            $(".spinner").hide();
            $("#add-product-dialog-form").dialog("open");

            //for each object in cart
            //if prodcut ID matches product Id in product 
            //grid then set button to a cancel icon
            if(cart.length !== 0){
                var cartIds = [];
                var jsonCart = JSON.stringify(cart);
                var parsedJsonCart = JSON.parse(jsonCart);
                var productsInCart = $.grep(parsedJsonCart, function(el, i){
                    cartIds.push(el.Id);
                });

                var currentRows = $('#productsTable').getRowData();
                var shownProductsThatAreInCart = $.grep(currentRows, function (el, i) {
                    return $.inArray(el.Id, cartIds) !== -1;
                });

                if(shownProductsThatAreInCart.length > 0){
                    var rowIds = $(this).jqGrid('getDataIDs');
                    $.each(rowIds, function(k, v) {
                        rowData = $('#productsTable').getRowData(v);

                        if($.inArray(rowData['Id'], cartIds) !== -1){
                            alert("Matched Product:\nRowData['id'] = " + rowData['Id'] + "\nto\nProduct in cart: " + cartIds.Id);
                            $(".ui-icon", "#jAddToCartButton_"+v).removeClass("ui-icon-cart");
                            $(".ui-icon", "#jAddToCartButton_"+v).addClass("ui-icon-cancel");
                        }
                    });
                }
            }
        },
        gridComplete: function() {
        }
    });
    $("#productsTable").jqGrid("navGrid", {edit:false,add:false,del:false},
    {},// use default settings for edit
    {},// use default settings for add
    {},// delete instead that del:false we need this
    {multipleSearch:false,overlay:false,ignoreCase:true,closeAfterSearch:true,closeOnEscape:true,showQuery:true});

我不认为这是一个错误,因为我已经看过许多演示它应该如何工作的演示,我猜我的配置有误,请查看我的代码并帮助确定问题。

要记住的一件事是,我通过返回 json 的 ajax 调用获取数据以加载网格,所有操作都在客户端完成,根本没有将数据发送回服务器。

谢谢!

【问题讨论】:

  • 另一个数据点 - 我可以单击按钮打开搜索对话框并立即关闭它,然后尝试再次打开它,它不会打开
  • 您使用哪个版本的免费 jqGrid?您能否准备演示(例如在 JSFiddle 中)来演示问题。
  • 我正在使用最新的 4.10.0 - 我会整理一些东西。
  • 你可以得到一些像jsfiddle.net/OlegKi/qzxwfquq这样的旧demo并修改它。
  • 我从头开始构建它,花了一段时间,但我重复了行为,代码很乱对不起..jsfiddle.net/su7mf5k9/1

标签: javascript jquery jqgrid free-jqgrid


【解决方案1】:

主要问题是您使用的搜索选项的组合:

{
    multipleSearch: false, // it's default and can be removed
    overlay: false, // !!! the option make the problem
    ignoreCase: true, // it's not exist at all
    closeAfterSearch: true,
    closeOnEscape: true,
    showQuery: true
}

选项overlay: false 的使用不好,因为它使另一个选项toTop: true 不起作用,并且搜索对话框将被放置为jQuery UI 对话框的子项。如果您删除该选项,则可以更轻松地使用搜索对话框,并且第二个问题(jqGrid 搜索对话框的位置计算中的错误)将不存在。看修改后的demo:

https://jsfiddle.net/OlegKi/su7mf5k9/3/

更新:modal jQuery UI 对话框中创建 modal jqGrid 对话框似乎有问题。该问题可以通过从外部 jQuery UI 对话框中删除 modal: true 选项来解决。见https://jsfiddle.net/OlegKi/su7mf5k9/3/

一般来说,可以创建混合解决方案,将 jQuery UI 对话框的类型从模态动态更改为非模态,但这可能会很棘手https://jsfiddle.net/OlegKi/su7mf5k9/5/

【讨论】:

  • 我看了看你的小提琴,不再有搜索不打开的问题,但输入搜索词的字段不再有效,我不得不将覆盖设置为 false 这样搜索字段不再被禁用。如果你去你的小提琴并添加 overlay: false 你会看到这将重新启用该字段。 - 再次感谢您的宝贵时间!
  • @Noctane:我现在看到了问题所在。谢谢!我会修复这个错误,并在新代码上架 GitHub 后通知您。
  • @Noctane:我发布了the first fix,这使您的原始代码可以正常工作。请参阅jsfiddle.net/OlegKi/su7mf5k9/4,我使用来自 GitHub 的 URL。如果您使用datatype: "local"jsonReader: { id: "Id" },我建议您包括localReader: { id: "Id"}。您可以删除隐藏的"Id" 列。 jqGrid 将从输入数据的Id 属性分配id 属性。对编辑也有帮助。当我发布第二个错误修复(overlay: false 已删除)时,我会通知您。难度更大。
  • @Noctane:我分析了问题并将结果写在我的答案的 UPDATED 部分。我没有找到其他使用 both modals 来解决问题的简单方法
  • 我正在使用您的第一个修复程序,它运行良好,如果您没有发现这样做的问题,我可能会继续使用它。如果您确实发现它存在问题,那么您认为选项 2 会是更安全的选择吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
相关资源
最近更新 更多