简体   繁体   English

如何将不同的图像应用于自动填充的表格单元格

[英]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.

相关问题 如何根据网站上填写的表单创建具有特定文件的自动回复电子邮件? - How can I create an auto respond to emails with specific files depending on the form filled in on my site? 我如何在控制器内的 Laravel 的不同表中获取自动递增 id 的值作为外键 - how can i get the value of an an auto incremented id as a foreign key in a different table in Laravel inside a controller 如何使用laravel excel将边框应用于特定单元格? - How can I apply borders to a specific cell with laravel excel? 如何使 multiCell 中的文本填满整个表格单元格? - How to make text inside multiCell filled the whole table cell? 如何使用PHP根据宽高比将类应用于图像? - How can I apply classes to images based on their aspect ratio with PHP? 如何获得桌子外的图像? - How can I get the images outside the table? 如何根据其中一个单元格的值更改动态填充的表格行的背景颜色? - how can I change background color of dynamically filled table row based on the value of one of the cells? 如何在条件之间应用带有分区键的 DynamoDB 表? - How can I DynamoDB Table with Partition Key on apply between condition? 如何在mysql表中自动链接上载 - How can I have a upload auto linked in a mysql table 如何在/etc/php.ini中无条件应用auto_prepend? - How can I unconditionally apply an auto_prepend in /etc/php.ini?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM