0

現在、複数のサブグリッドで jqGrid を使用するプロジェクトに取り組んでいます。行がクリックまたはダブルクリックされたときに行IDを取得しようとしています(そして行内のデータを取得しようとしています)。最終的に、クリックした行のデータをテキスト ボックスに入力したいと思います。

ここで ondblClickRow と onSelectRow の例を使用していくつかのバリエーションを試しましたが、うまくいきません。本当に単純なものが欠けていると思いますが、何がわかりません。そこで、行のクリックを認識してアラートを表示するように、できるだけ単純化しました。これも私にはうまくいきません。

( jqGridの例に基づいて: ネストされたサブグリッドをローカル データ型でロードする問題) 下部近くの //**************** 部分を探します。

var myData = [
// main grid data
     { id: "1", col1: "11", col2: "12",
         subgrid: [
                // data for subgrid for the id=m1
                    { id: "1", c1: "aa", c2: "ab", c3: "ac",
                        subgrid: [
                            // data for subgrid for the id=m1, subgridId=s1a
                            { id: "1", d1: "2aa", d2: "2ab", d3: "2ac" },
                            { id: "2", d1: "2ba", d2: "2bb", d3: "2bc" },
                            { id: "3", d1: "2ca", d2: "2cb", d3: "2cc" }
                        ]},
                { id: "2", c1: "ba", c2: "bb", c3: "bc" },
                { id: "3", c1: "ca", c2: "cb", c3: "cc" }
         ]},
    { id: "2", col1: "21", col2: "22",
        subgrid: [
            // data for subgrid for the id=m2
            { id: "1", c1: "1xx", c2: "1xy", c3: "1xz",
                subgrid: [
                    // data for subgrid for the id=m2, subgridId=s2a
                    { id: "1", d1: "2xx", d2: "2xy", d3: "2xz" }
                ]}
        ]},
    { id: "3", col1: "31", col2: "32" }
],
removeSubgridIcon = function () {
    var $this = $(this),
        idPrefix = $this.jqGrid("getGridParam", "idPrefix");
    $this.find(">tbody>tr.jqgrow>td.ui-sgcollapsed").filter(function () {
        var rowData = $this.jqGrid("getLocalRow",
                $.jgrid.stripPref(idPrefix, $(this).closest("tr.jqgrow").attr("id")));
        return rowData.subgrid == null;
    }).unbind("click").html("");
},
isHasSubrids = function (data) {
    var l = data.length, i;
    for (i = 0; i < l; i++) {
        if (data[i].subgrid != null) {
            return true;
        }
    }
    return false;
},
specificGridOptions = [
    {
        colNames: ["Column 1", "Column 2"],
        colModel: [
            { name: "col1" },
            { name: "col2" }
        ],
        cmTemplate: { width: 200 },
        sortname: "col1",
        sortorder: "desc",
        idPrefix: "s_",
        pager: "#pager",
        caption: "Demonstrate how to create subgrid from local hierarchical data"
    },
    {
        colNames: ["Colunm1", "Colunm2", "Colunm3"],
        colModel: [
            { name: "c1" },
            { name: "c2" },
            { name: "c3" }
        ],
        cmTemplate: { width: 112 },
        sortname: "c1",
        sortorder: "desc"
    },
    {
        colNames: ["Col 1", "Col 2", "Col 3"],
        colModel: [
            { name: "d1" },
            { name: "d2" },
            { name: "d3" }
        ],
        cmTemplate: { width: 90 },
        sortname: "d1",
        sortorder: "desc"
    }
],
commonGridOptions = {
    datatype: "local",
    gridview: true,
    rownumbers: true,
    autoencode: true,
    height: "100%",
    //***************
    onSelectRow : function () 
    {
        alert('test!');
    },
    //also tried many variation on this
    ondblClickRow: function(rowid) 
    {
        alert(rowid);
    }
    //***************
    loadComplete: function () {
        // one can use loadComplete: removeSubgridIcon, but I included
        // curent implementation of loadComplete only to show how to call
        // removeSubgridIcon from your loadComplete callback handler
    removeSubgridIcon.call(this);
},
subGridRowExpanded: function (subgridDivId, rowId) {
    var $subgrid = $("<table id='" + subgridDivId + "_t'></table>"),
        subgridLevel = $(this).jqGrid("getGridParam", "subgridLevel") + 1,
        parentIdPrefix = $(this).jqGrid("getGridParam", "idPrefix"),
        pureRowId = $.jgrid.stripPref(parentIdPrefix, rowId),
        localRowData = $(this).jqGrid("getLocalRow", pureRowId);
    $subgrid.appendTo("#" + $.jgrid.jqID(subgridDivId));
    $subgrid.jqGrid($.extend(true, {}, commonGridOptions, specificGridOptions       [subgridLevel], {
        data: localRowData.subgrid,
        subGrid: isHasSubrids(localRowData.subgrid),
        subgridLevel: subgridLevel,
        idPrefix: rowId + "_",
        rowNum: 10000 // we use this to have no pager in the subgrids
    }));
}
};

$("#list").jqGrid($.extend(true, {}, commonGridOptions, specificGridOptions[0], {
    data: myData,
    subgridLevel: 0,
    subGrid: isHasSubrids(myData)
}));

行のクリック/ダブルクリックが認識されない理由は誰にもありますか?

4

1 に答える 1

3

サーバーからグリッドのデータを取得することをコメントに書きました。ケースでの の使用はdatatype: "local"最良の選択ではないと思います。同じことを行う方法を説明した答えを見てくださいdatatype: "json"

ここで、主な質問に戻ります。どのテキスト ボックス (HTML 入力要素) に入力したいのか、入力要素がグリッドの内側にあるか外側にあるかがわかりません。それにもかかわらず、おそらく発生する可能性のある唯一の問題は、idPrefixjqGrid のオプションの正しい使用法です。

jqGrid<table>はグリッドの本体を表すために HTML を使用することを理解することは非常に重要です。jqGrid の現在の実装では、 must haveのすべて<tr>の要素に<table>id属性が必要です。したがって、id入力データのプロパティは、要素のid属性の値を割り当てるために使用されます。<tr>ページに複数のグリッドがある場合、またはサブグリッドを持つグリッドがある場合id、HTML または XHTML のすべてのバージョンで許可されていない重複を非常に簡単に受け取ることができます。

追加の潜在的な問題は、数値をid値として使用することです。ほとんどのデータベースは、テーブルのキーとして非常に実用的な自動増分データ型をサポートしています。この場合、データベース テーブルとグリッド行のネイティブid(キー) は整数になります。反対に、使用する HTML/XHTML のバージョンに応じて、いくつかの追加の制限があります。たとえば、HTML5 の仕様は次のように述べています (こちらを参照) 。

値は、要素のホーム サブツリー内のすべての ID の中で一意である必要があり、少なくとも 1 文字が含まれている必要があります。値に空白文字を含めることはできません。

そのため、ほとんどの Web ブラウザでは数値をid属性の値として使用できますが、これは許可されておらず、これを使用すると互換性の問題が発生する可能性があります。

上記の問題をすべて解決するために、jqGrid はidPrefixオプションをサポートしています (ちなみに、これは私の提案に基づいて導入されました)。この場合、属性の値は、入力データのと のid連結として作成されます。たとえば、例のメイン グリッドで使用される値「1」、「2」、「3」の場合、jqGrid はメイン グリッドの要素の属性の値として、、、を割り当てます。すべてのコールバックの は、属性 ( 、 、 ) の値になります。オリジナルを取得する必要がある場合は、プレフィックスを削除するために使用できます。jqGrid によってサーバーに送信されるすべての ID は正規化されます (idPrefixididPrefix: "s_"id"s_1""s_2""s_3"id<tr>rowidid"s_1""s_2""s_3" id$.jgrid.stripPref$.jgrid.stripPrefjqGrid自体によって呼び出されます。

onSelectRowしたがって、データを取得する方法を示すコードondblClickRowは次のようになります

onSelectRow: function (rowid, stat, e) {
    myDebugTrace.call(this, "onSelectRow", rowid);
},
ondblClickRow: function (rowid, iRow, iCol, e) {
    myDebugTrace.call(this, "ondblClickRow", rowid);
    e.stopPropagation();
},
...

myDebugTrace関数は次のように宣言できます

var myDebugTrace = function (startingText, rowid) {
        var $this = $(this), p = $this.jqGrid("getGridParam"), rowData, col1,
            firstCol = (p.rownumbers ? 1 : 0) + (p.subGrid ? 1 : 0);

        rowData = $this.jqGrid("getRowData", rowid);
        col1 = rowData[p.colModel[firstCol].name];
        $("<span>" + startingText + " on " + rowid + " (original id=" +
            $.jgrid.stripPref($(this).jqGrid("getGridParam", "idPrefix"), rowid) +
            "). Data from the first column: \"" + col1 +"\"</span><br/>").appendTo("body");
    };

内部サブグリッドの行をダブルクリックすると、対応するデモが次のように表示されます。

ここに画像の説明を入力

于 2013-01-10T10:45:36.907 に答える