JqGrid – 仅当两个数据列表匹配时填充子表格

huangapple go评论100阅读模式
英文:

JqGrid - populate subgrid only when 2 data lists match

问题

Here's the translation of the code and the description:

这是用于主网格的数据:

{'id': 1, 'first_name': 'Greg', 'last_name': 'JEAN'},
{'id': 2, 'first_name': 'Paul', 'last_name': 'Martin'},
{'id': 3, 'first_name': 'Georges', 'last_name': 'Linc'},
{'id': 4, 'first_name': 'Bill', 'last_name': 'Evans'}

这是用于子网格的数据:

{'idref': 1, 'flavour': 'chocolate', 'temp': 'true'},
{'idref': 2, 'flavour': 'vanilla', 'temp': 'false'},
{'idref': 2, 'flavour': 'peanut', 'temp': 'false'},
{'idref': 4, 'flavour': 'mint', 'temp': 'false'}

我尝试的目标是,如果ID和IDREF之间有匹配,只用匹配的数据填充子网格。如果没有匹配,我不希望显示任何子网格(就像我的示例中的ID 3一样)。

预期的结果将如下所示:

JqGrid – 仅当两个数据列表匹配时填充子表格

我知道我可以使用isHasSubGrid,但我不知道如何编写两个数据列表之间的“匹配”条件。

这是我目前的代码:

var available_lessons_json = {{available_lessons|safe}}
var subavailable_lessons_json = {{subavailable_lessons|safe}}

$("#jqGrid").jqGrid({
    // ... 其他设置 ...
    subGrid: true,
    subGridRowExpanded: showChildGrid,
    subGridOptions: {
        plusicon: "ui-icon-triangle-1-e",
        minusicon: "ui-icon-triangle-1-s",
        openicon: "ui-icon-arrowreturn-1-e"
    },
    pager: "#jqGridPager"
});

function showChildGrid(parentRowID, parentRowKey) {
    var childGridID = parentRowID + "_table";
    var childGridPagerID = parentRowID + "_pager";

    var childGridURL = parentRowKey + ".json";

    $('#' + parentRowID).append('<table id=' + childGridID + '></table><div id=' + childGridPagerID + ' class=scroll></div>');

    $("#" + childGridID).jqGrid({
        url: childGridURL,
        datatype: 'local',
        data: subavailable_lessons_json,
        // ... 其他设置 ...
    });
}

希望这有所帮助。如果需要进一步的解释或帮助,请告诉我。

英文:

I have those 2 Json data lists to populate JqGrid and subgrid :

This one for the main grid :

    {&#39;id&#39;: 1, &#39;first_name&#39;: &#39;Greg&#39;, &#39;last_name&#39;: &#39;JEAN&#39;}, 
    {&#39;id&#39;: 2, &#39;first_name&#39;: &#39;Paul&#39;, &#39;last_name&#39;: &#39;Martin&#39;},
    {&#39;id&#39;: 3, &#39;first_name&#39;: &#39;Georges&#39;, &#39;last_name&#39;: &#39;Linc&#39;},
    {&#39;id&#39;: 4, &#39;first_name&#39;: &#39;Bill&#39;, &#39;last_name&#39;: &#39;Evans&#39;}

And this one for the subgrid :

{&#39;idref&#39;: 1, &#39;flavour&#39;: &#39;chocolate&#39;, &#39;temp&#39;: &#39;true&#39;},
{&#39;idref&#39;: 2, &#39;flavour&#39;: &#39;vanilla&#39;, &#39;temp&#39;: &#39;false&#39;},
{&#39;idref&#39;: 2, &#39;flavour&#39;: &#39;peanut&#39;, &#39;temp&#39;: &#39;false&#39;,
{&#39;idref&#39;: 4, &#39;flavour&#39;: &#39;mint&#39;, &#39;temp&#39;: &#39;false&#39;}

What i'm trying to do is, IF there is a match between ID and IDREF, populate the subgrid only with the matching datas.
If there is no match I don't want any subgrid to appear (like in ID 3 in my example)

The expected result would be like :

JqGrid – 仅当两个数据列表匹配时填充子表格

I know i could use isHasSubGrid but i don"t know how to write the "matching" condition between the 2 data lists.

This is how my code look like so far :

var available_lessons_json = {{available_lessons|safe}}
     var subavailable_lessons_json = {{subavailable_lessons|safe}}

       $(&quot;#jqGrid&quot;).jqGrid({

         datatype: &#39;local&#39;,
         data: available_lessons_json,
         colModel: [
            {name: &#39;id&#39;, label : &#39;id&#39;},
            {name: &#39;first_name&#39;, label : &#39;first_name&#39;},
             {name: &#39;last_name&#39;, label : &#39;last_name&#39;}
            
         ],
                loadonce: true,
				viewrecords: true,
                width: 780,
                height: 200,
                rowNum: 20,
				rowList : [20,30,50],
                altRows: true,
                rownumbers: true,
                rownumWidth: 25,

                subGrid: true, 
                subGridRowExpanded: showChildGrid, 
			    subGridOptions : {
					// configure the icons from theme rolloer
					plusicon: &quot;ui-icon-triangle-1-e&quot;,
					minusicon: &quot;ui-icon-triangle-1-s&quot;,
					openicon: &quot;ui-icon-arrowreturn-1-e&quot;
				},
                pager: &quot;#jqGridPager&quot;

        });

       
        function showChildGrid(parentRowID, parentRowKey) {
            var childGridID = parentRowID + &quot;_table&quot;;
            var childGridPagerID = parentRowID + &quot;_pager&quot;;

            var childGridURL = parentRowKey+&quot;.json&quot;;
            

            $(&#39;#&#39; + parentRowID).append(&#39;&lt;table id=&#39; + childGridID + &#39;&gt;&lt;/table&gt;&lt;div id=&#39; + childGridPagerID + &#39; class=scroll&gt;&lt;/div&gt;&#39;);

            $(&quot;#&quot; + childGridID).jqGrid({
                url: childGridURL,
                datatype: &#39;local&#39;,
                data: subavailable_lessons_json,
                page: 1,
                colModel: [
            {name: &#39;idref&#39;, label : &#39;idref&#39;},
            {name: &#39;flavour&#39;, label : &#39;flavour&#39;},
             {name: &#39;temp&#39;, label : &#39;temp&#39;}
         ],
				loadonce: true,
                width: 500,
                height: &#39;100%&#39;,
                pager: &quot;#&quot; + childGridPagerID,


            });

        }

Any help would be welcome.

Thanks in advance

答案1

得分: 0

The provided text appears to be a code-related explanation. Here is the translation:

当 isHasSubGrid 事件传递主行的 id 时,如果此事件返回 true,则构建子网格;如果返回 false,则不构建(绑定)子网格。此事件在主网格的每一行上执行。

也许这是一种可能的解决方案:

isHasSubGrid: function(id) {
var ret = false;
for (var i = 0; i < subavailable_lessons_json.length; i++) {
if (subavailable_lessons_json[i].idref == id) {
ret = true;
break;
}
}
return ret;
}

英文:

То isHasSubGrid event is passed the id of the main row and if this event return true, the subgrid is build and if it return false it is not build (bind). This event is executed on every row of the main grid.

Maybe this is a one possible solution

isHasSubGrid : function( id ) {
    var ret = false;
    for(var i = 0; i &lt; subavailable_lessons_json.length; i++)
    {
        if(subavailable_lessons_json[i].idref == id) {
            ret = true;
            break;
        }
    }
    return ret;
}

huangapple
  • 本文由 发表于 2023年4月19日 19:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定