如何从亚格访问父格ID中的jqGrid [英] How to access parent grid id from subgrid in jqgrid

查看:93
本文介绍了如何从亚格访问父格ID中的jqGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在兴建这是工作的一个jqGrid的罚款...电网建设

i'm constructing a jqgrid which is working fine ... grid construction

if (jqent.isReq) {
    sbGrid.Append("subGrid: true,");
    sbGrid.Append("subGridRowExpanded: function (subgrid_id, row_id) {");
    sbGrid.Append(" $('#' + subgrid_id).html(renderhtmlforSubgrid(subgrid_id, row_id));},");
}

我有一个文本区域和次网格将由低于code被渲染的图像控制

i have a subgrid with textarea and an image control that will be rendered by the below code

function renderhtmlforSubgrid(subgrid_id, row_id) {
    var script = '<div style="margin-left:10px; margin-top:10px; margin-bottom:10px;">';
    script += '<textarea class="txtCommentwatermarkOn" id="txtCommentSection" name="txtCommentSection" rows=2 cols=20>Type your comments here</textarea>';
    script += '<input onclick=\"RenderModalPopup(' + row_id + ',' + subgrid_id + ',this);\" type="image" id="image" src="../../Content/Images/commentIcon.png"/>';
    script += '</div>';

    return script;
}

上的图像RenderModalPopup功能的点击都将触发

on click of the image "RenderModalPopup" function will be triggered

function RenderModalPopup(rowid, tableid, event) {
    var dataFromRow = $('#tblArtifact1').jqGrid('getRowData', rowid);
    var PhaseArtifactId = $('#tblArtifact1').jqGrid('getCell', rowid, 'ID');
    ..........................

每一件事情是工作的罚款...现在的问题的是我硬编码格ID为tblArtifact1来获取列值。需要被去除。我能得到电网ID?有没有predefined参数/函数来得到它?

Every thing is working fine... Now the issue is i'm hardcoding the grid id as "tblArtifact1" to fetch the column values. which needs to be removed. Can i get the grid id? is there any predefined parameter/function to get it?

我卡了..请帮助。

推荐答案

在回调函数 subGridRowExpanded 您可以使用这个变量,它被初始化为网格的DOM(即&LT的DOM;表&gt; )。所以,你可以得到 ID this.id 父格。如果你调用 renderhtmlforSubgrid subGridRowExpanded 里面我会建议你使用

Inside of callback function subGridRowExpanded you can use this variable which is initialized to the DOM of the grid (DOM of the <table>). So you can get id of the parent grid by this.id. If you call renderhtmlforSubgrid inside of subGridRowExpanded I would recommend you to use

renderhtmlforSubgrid.call(this, subgrid_id, row_id)

而不是

renderhtmlforSubgrid(subgrid_id, row_id)

在这种情况下这个 renderhtmlforSubgrid 的功能将不会改变全球窗口对象,你可以有这个占据着有用的信息,供各位(如 this.id 为ID或 $(本) jQuery的说唱歌手到父网格)。

In the case this inside of renderhtmlforSubgrid function will be not changed to global window object and you can have this which hold helpful information for you (like this.id for id or $(this) for jQuery rapper to the parent grid).

我会建议你考虑使用在次网格为prevent ID重复 ID preFIX 选项(请参阅the回答获得更多信息)。

I would recommend you to consider to use idPrefix option in the subgrid to prevent id duplicates (see the answer for more information).

此外,我从你code看到你更多的C#/ C ++开发的JavaScript开发。我前2年里发生过同样的问题。 JavaScript有一些结构中这是接近公知的其他语言,但深层语义确实不同。所以,JavaScript开发人员有许多标准的陷阱,他利用现有建筑在错误的道路。

Moreover I see from your code that you are more C#/C++ developer as JavaScript developer. I had the same problems 2 years before. JavaScript has some constructions which are close to well-known other languages, but the deep semantic is really different. So JavaScript developers have many standard traps where he use existing constructions in the wrong way.

有关的例子:你永远不应该使用变量,属性或其中尤其是名称以大写字母开头的函数。目前, RenderModalPopup PhaseArtifactId 违反规则。 JavaScript有没有类,但可以创建一个使用功能紧密结构。里面的功能可分配属性这个。这些功能是类的构造,应该有一个大写字母。要创建类的实例应该使用。例如 VAR =现在新的Date();

For examples: you should never use variables, properties or especially functions which name starts with capital letter. Currently RenderModalPopup and PhaseArtifactId breaks the rule. JavaScript has no classes, but one can create close constructs using functions. Inside of functions you can assign properties to this. Such functions are constructors of the "class" and should have first capital letter. To create instance of the "class" one should use new. For example var now = new Date();.

闭包是在JavaScript中,看起来像函数的函数的另一个例子,但有很多重要的区别。

Closures are another example of functions in JavaScript which looks like functions, but have a lot of important differences.

此外,我会建议你使用更多的匿名函数和函数前pressions( VAR renderhtmlforSubgrid =功能(subgrid_id,ROW_ID){...} ),而不是函数声明(函数renderhtmlforSubgrid(subgrid_id,ROW_ID){...} )和函数体的第一行中定义的所有变量。 JavaScript有无块级变量声明的最喜欢的计算机语言,所以很容易产生错误的code,如果你需要定义的JavaScript code的内部变量。

Additionally I would recommend you to use more anonymous functions and function expressions (var renderhtmlforSubgrid = function (subgrid_id, row_id) {...}) instead of function statement (function renderhtmlforSubgrid (subgrid_id, row_id) {...}) and define all variables in the first line of function body. JavaScript has no block-level variable declaration like the most computer languages, so it's very easy to produce wrong code if you would define variables inside of JavaScript code.

最后的建议是使用 $ C的Kernighan和Ritchie的风格 $ C格式为JavaScript code独立于您使用您最喜爱的计算机语言使用的样式。它将$从自动分号插入·P $ pvent你它可以使严重的错误,这是很难本地化。

The last advice would be to use Kernighan and Ritchie's style of code formatting for JavaScript code independent from the style which you used to use in your favorite computer language. It will prevent you from the Automatic Semicolon Insertion which can make serious errors which are difficult to localize.

这篇关于如何从亚格访问父格ID中的jqGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆