【问题标题】:jqgrid select only one row per groupjqgrid每组只选择一行
【发布时间】:2016-02-17 07:25:10
【问题描述】:

我们如何允许用户每组只选择一行?

我有以下代码。

var data = [
           { ActionItemId: "AAZ08702-0001104", StrarTime: "2007-10-01", Category: "General", CategoryDetails: "dummy text of industry. a galley of type ", TargetCategory: "200.00",
            TargetDateCategory: "10.00", ActualCategory: "210.00"}
        ];

        $("#jqGrid").jqGrid({
            data: data,
            datatype: "local",
            colModel: [
                { label: 'Action Item ID', name: 'ActionItemId',  key: true },
                { label: 'Start Time', name: 'StrarTime'},
                { label: 'Category', name: 'Category'},
                { label: 'Details', name: 'CategoryDetails', cellattr: function (rowId, tv, rawObject, cm, rdata) { return 'style="white-space: normal;"' }},
                { label: 'Target <Category>', name: 'TargetCategory' },
                { label: 'Target Date <Category>', name: 'TargetDateCategory'}
            ],
            loadonce: true,
            viewrecords: true,
            //width: 1000,
            height: 400,
            rowNum: 20,
            rowList: [20, 30, 50],
            rownumbers: true,
            rownumWidth: 25,
            multiselect: true,
            shrinkToFit: false,
            pager: "#jqGridPager",
            grouping: true,
            groupingView: {
                groupField: ["Category"],
                groupColumnShow: [true],
                groupText: ["Category: <b>{0}</b>"],
                groupOrder: ["asc"],
                groupSummary: [false],
                groupCollapse: false

            }
        });

我需要禁用每列选择多行的功能。有可能吗?

分组功能中是否有一个设置可以按照 mu 要求启用?还是应该定制开发?

注意:我只添加了一列以避免问题中的代码很长

【问题讨论】:

  • 抱歉,我还不清楚您需要实施哪种行为。让我们用户选择一行。如果已经选择了同一组中的另一行,或者您想取消选择先前选择的行(来自同一组)并选择当前单击的行,是否要拒绝选择?
  • 您提到的第一个选项。例如:用户选择一行,他不能再从同一个组中选择行,但他可以从其他组中选择行。显然用户只能从每个组中选择一行
  • 您可以使用beforeSelectRow 回调来拒绝选择任何行。在beforeSelectRow 回调中,您可以测试任何自定义条件并返回false 以拒绝选择或返回true 以允许选择。取决于您使用的 jqGrid 版本,只有在用户单击复选框直接的情况下,才可能需要取消选中多选复选框。有多种方法可以测试已选择的行(来自selarrrow)是否与当前单击的行位于同一组中。如果使用小页面大小(selarrrow),任何方式都可以。
  • 如何检查当前单击的行在哪个组中,如何禁用仅选择该组?
  • @Oleg 请让我知道是否有办法取消选中当前选定行组的其他行。我还是被困在这里。

标签: jqgrid grouping


【解决方案1】:

其中一个可能的实现可能是添加add 回调,如果已经选择了同一组中的另一行,则返回false。实现示例如下:

beforeSelectRow: function (rowid, e) {
    var selarrrow = $(this).jqGrid("getGridParam", "selarrrow"),
        $tr = $(e.target).closest("tr.jqgrow"),
        otherIdsOfTheGroup;

    if ($tr.length > 0) {
        otherIdsOfTheGroup =
            // get all rows of the group before the current
            $tr.prevUntil("tr.jqgroup")
                // add all rows of the group after the current
                .add($tr.nextUntil("tr.jqgroup"))
                // enum all the rows of the group without the current
                .map(function () {
                    // test whether the rowid is already selected
                    if ($.inArray(this.id, selarrrow) >= 0) {
                        // add the rowid to the array of returned values
                        return this.id;
                    }
                });
        // otherIdsOfTheGroup contains the array of rowids of the rows
        // from the same group, which are already selected
        if (otherIdsOfTheGroup.length > 0) {
            return false; // prevent selection
        }
    }
    return true; // allow selection
}

the demo

更新:可以轻松修改 aboce vode 以取消选择以前选择的行来自同一组。只需为 otherIdsOfTheGroup 数组中的每个 rowid 调用 resetSelection 并从 otherIdsOfTheGroup 返回 true 以允许选择:

beforeSelectRow: function (rowid, e) {
    var $this = $(this),
        selarrrow = $this.jqGrid("getGridParam", "selarrrow"),
        $tr = $(e.target).closest("tr.jqgrow"),
        otherIdsOfTheGroup;

    if ($tr.length > 0) {
        otherIdsOfTheGroup =
            // get all rows of the group before the current
            $tr.prevUntil("tr.jqgroup")
                // add all rows of the group after the current
                .add($tr.nextUntil("tr.jqgroup"))
                // enum all the rows of the group without the current
                .map(function () {
                    // test whether the rowid is already selected
                    if ($.inArray(this.id, selarrrow) >= 0) {
                        // add the rowid to the array of returned values
                        return this.id;
                    }
                });
        // otherIdsOfTheGroup contains the array of rowids of the rows
        // from the same group, which are already selected
        if (otherIdsOfTheGroup.length > 0) {
            $.each(otherIdsOfTheGroup, function () {
                $this.jqGrid("resetSelection", this);
            });
        }
    }
    return true; // allow selection
}

the next demo。我包括隐藏“全选”按钮的列标题只是为了编写更少的代码。您可以实现onSelectAll 回调并允许从每个组中仅选择一个(例如第一行)行。

【讨论】:

  • 非常感谢这段代码。我也尝试使用 beforeSelectRow 方法来解决这个问题。我将发布我的解决方案作为答案。我发现的问题在您的解决方案中,用户无法在我的解决方案中直接从同一组中选择另一行(用户必须取消选中当前行并选择另一行),用户根本无法取消选择。所以它就像单选按钮集一样工作。我想这是我们两种解决方案的缺点
  • @ChathuraSam:不客气!我向您询问了您需要在我对您的问题的第一条评论中实施的确切行为。您回答:“第一个选项”,意思是“如果已经选择了同一组中的另一行,则拒绝选择”。因此,我实现了该行为。无论如何,改变行为都很容易。可以将我的代码的return false; 行替换为对已选择的同一组的每一行调用resetSelection。请参阅我的回答的已更新部分。
  • 哇太棒了。非常感谢您提供的帮助。你是救生员!
  • @ChathuraSam:对不起,我认为你是 jqGrid 的新手。它仅支持在一页上选择行。我在最新版本的免费 jqGrid 中实现了新选项:multiPageSelection: true。如果要在多个页面上保留选择,则需要添加选项。参见the demothe answer。是你需要的吗?
  • @ChathuraSam:见the modified demo
【解决方案2】:

我设法使用以下代码解决了这个问题。

        beforeSelectRow: function (id, e) {
            var rowdata = $("#jqGrid").getRowData(id);
            var category = rowdata.Category;
            var selectedRowTR = $("#jqGrid").find("tr[id='" + id + "']");
            var groupTRs = $("#jqGrid").find("tbody> tr.jqgrow > td[title='" + category + "']").parents("tr");

            var ids = groupTRs.map(function () {
                return this.id;
            }).get();

            var selectedIDs = $("#jqGrid").getGridParam("selarrrow");

            var commonValues = [];
            var i, j;
            var arr1Length = ids.length;
            var arr2Length = selectedIDs.length;

            for (i = 0; i < arr1Length; i++) {
                for (j = 0; j < arr2Length; j++) {
                    if (ids[i] === selectedIDs[j]) {
                        commonValues.push(ids[i]);
                    }
                }
            }

            for (var i = 0; i < commonValues.length; i++) {
                $("#jqGrid").jqGrid('setSelection', commonValues[i], false);

            }

        return true;
    },

行数据.Category;是表被分组的变量。唯一的问题是用户无法取消勾选他/她已经在组中选择的内容。所以它就像一个单选按钮集。但它适用于我的要求。 希望我们可以改进这一点,并在 jqgrid 中引入无线电类型的分组行为。 谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    相关资源
    最近更新 更多