[英]how can I apply different images to an auto filled table cell
I am using pear HTML_TABLE to create a table display from a database. 我正在使用pear HTML_TABLE从数据库创建表显示。 Because there are empty fields I am using the auto fill to fill in empty cells with an image.
因为有空白字段,所以我使用自动填充功能将空白图片填充到单元格中。 However, I need a different image for the alternating columns.
但是,我需要交替列的其他图像。 This is the code that generates the empty cells:
这是生成空单元格的代码:
$attrs = array( 'class' => 'main',
'id' => 'main_id',
'width' => '100%',
'border' => '1',
'cellspacing' => '0',
'cellpadding' => '0');
$table = new HTML_Table($attrs);
$table->setAutoGrow(true);
$table->setAutoFill("<span class='iconImg'></span>");
I have tried using javascript, and jquery to accomplish this, but without success. 我尝试使用javascript和jquery完成此操作,但未成功。 The javascript (jquery) i have tried is:
我尝试过的javascript(jquery)是:
$(".main td:not(.jobCell):not(.tJCell)").each(function(){
var colIndex = $(this).cellPos().left;
var preCol = colIndex % 2;
if (preCol == 0)
{
$(this).text( 'Even');
//$(this).attr( 'src', '../images/buttons/list.png');
}
else
{
$(this).attr( 'src', '../images/buttons/btn_pointer.png');
}
});
The above javascript is fine for placing odd or even as text in each cell. 上面的javascript非常适合在每个单元格中放置奇数或偶数文本。 The problem is that I don't need text, I need an image, as in the else statement.
问题是我不需要文本,我需要图像,就像else语句一样。 I believe the code is correct, but it does not work.
我相信该代码是正确的,但是它不起作用。 I have even tried doing this with css, but the following css doesn't work either.
我什至尝试使用CSS来执行此操作,但是以下CSS也不起作用。
.iconImg
{
background-image:url(../../images/buttons/list.png);
}
All help and/or advice in this matter would be greatly appreciated! 在此问题上的所有帮助和/或建议将不胜感激! I have been stuck on this for too many days now.
我已经被这个问题困扰了太多天了。
In the hopes that this will help others. 希望这对其他人有帮助。 My solution is based on cweiske's answer/suggestion.
我的解决方案基于cweiske的回答/建议。 Since I could not get the css odd/even functionality to work, I used the following jquery script to assign a class to my auto filled cells:
由于我无法使用css的奇/偶功能,因此我使用以下jquery脚本为自动填充的单元格分配了一个类:
$(".main td:not(.jobCell):not(.tJCell)").each(function(){
var colIndex = $(this).cellPos().left;
var preCol = colIndex % 2;
if (preCol == 0)
{
$(this).addClass('evenCell');
}
else
{
$(this).addClass('oddCell');
}
});
The above function is reference code written by nrodic. 上面的功能是由nrodic编写的参考代码。 This was used because I have colspans and rowspans throughout my table (which throws off cell indexing), and the empty cells between are auto filled with using pear HTML_TABLE auto fill like this:
之所以使用它,是因为我在整个表中都具有colspans和rowpans(这会引发单元格索引),并且使用pear HTML_TABLE自动填充来自动填充之间的空白单元格,如下所示:
$attrs = array( 'class' => 'main',
'id' => 'main_id',
'width' => '100%',
'border' => '1',
'cellspacing' => '0',
'cellpadding' => '0');
$table = new HTML_Table($attrs);
$table->setAutoGrow(true);
$table->setAutoFill("");
Then the resulting css code is this: 然后生成的CSS代码是这样的:
.evenCell
{
background:url(../../images/buttons/btn_pointer.png) no-repeat;
background-position:center;
}
.oddCell
{
background:url(../../images/buttons/btn_list.png) no-repeat;
background-position:center;
}
Hope this is helpful to others! 希望这对其他人有帮助!
您可以使用CSS奇数规则将背景图像分配给单元格,只要它们具有某些类即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.