2

我有一个表格(jqGrid),我想在文档加载时动态添加一个小图标/图像。图像将根据网格的隐藏列值之一(日期)插入;它应该将行的重要性/紧迫性标记到页面/网格查看器。我精通 jQuery,我的逻辑很混乱,但我唯一遇到的问题是应该在 DOM 结构中的哪个位置插入这个元素,因为它不应该是实际网格的一部分。它应该浮动在网格外行的左侧。所以基本上,我要问的是:

1) 这个元素应该插入到 DOM 结构的什么地方?包含实际表的 div 容器?

2)应该如何设置样式(CSS)?

我想出的唯一方法是获取相关行的 y 坐标并将其插入设置为该 y 坐标的 div 容器(包含表格)中。我不确定这是否是最好或正确的方法。

编辑:这里有很多好的方法。我正在考虑将“afterInsertRow”事件与 David 的方法结合起来。谢谢大家的帮助!

4

3 回答 3

2

为什么要在外部添加任何元素table?只需使用隐藏单元格来包含您想要显示的图像/元素和样式以响应:hoverparent tr,给定标记:

<table>
    <thead>
        <tr>
            <th></th>
            <th>Column 'one'</th>
            <th>Column 'two'</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td>row one</td>
            <td>row one</td>
        </tr>
        <tr>
            <td></td>
            <td>row two</td>
            <td>row two</td>
        </tr>
    </tbody>
</table>

和CSS:

tr th:first-child,
tr td:first-child {
    visibility: hidden;
    border: 0 none transparent; /* hide the borders */
    width: 1em; /* define the width, to avoid the visible content */
}               /* causing page-jumps */

tr th:first-child + th,
tr td:first-child + td {
    border-left: 2px solid #000; /* style the cell *after* the first-child */
}                                /* with a border to 'fake' the look of the */
                                 /* new content being appended outside */
td, th {
    visibility: visible;
    border: 1px solid #000;      /* show borders to continue the illusion */
    height: 2em;                 /* or whatever... */
    line-height: 2em;
}

这种方法似乎有效(我认为也应该在 IE 中有效): JS Fiddle demo

于 2012-10-22T17:35:21.193 回答
0

您可以使用 jqGrid 中的 afterInsertRow 事件:

var my_jqGrid = $('#id_table').jqGrid({
   ... //Other configs

   afterInsertRow: function(rowid, rowdata, rowelement){

       console.log(rowdata);
       //Here you code condition to insert in other div
       if(rowdata['key']=='status_ok'){
           $('#div_id').append( '<img src="image/ok.png" />'  );
       }          
   }

});

在这里查看维基

好吧,我附加了一个简单的图像,您可以附加一个 div 容器图像并使用 css 设置高度。

于 2012-10-22T18:10:48.963 回答
0

最简单的解决方案是使用 jQuery 的“my at of”定位:

1)将元素附加到页面的主体。

2) 使用 jQuery "my at of" 定位将元素放置在您尝试标记为紧急的行元素旁边。http://docs.jquery.com/UI/API/1.8/Position

于 2012-10-22T17:35:37.513 回答