如何为codeigniter中的特定列数据赋予颜色?
Posted
技术标签:
【中文标题】如何为codeigniter中的特定列数据赋予颜色?【英文标题】:How to give a color to a perticular column data in codeigniter? 【发布时间】:2020-07-19 12:55:30 【问题描述】:<?
foreach($customer as $customer_details)
?>
<tr id="customer_details_<?=$customer_details['id']?>">
<?
foreach($dyncust_fields as $dyncust_field)
if($dyncust_field['add_to_listing']=='1')
echo "<td style='color:green;'>".$customer_details[$dyncust_field['attribute_name']]."</td>";
?>
</tr>
<?
?>
这里我写了一些代码来显示动态列的数据,这里我想给特定列的特定数据一个颜色。但它不起作用。这里$customer_details[$dyncust_field['attribute_name']]
这一行用于根据动态列获取表记录。这里$customer_details[$dyncust_field['attribute_name']] == 'cname'
我希望单元格为红色,否则显示为绿色。这个怎么做 ?。谁能帮帮我...
【问题讨论】:
发布您的html
【参考方案1】:
<?php
foreach($customer as $customer_details)
?>
<tr id="customer_details_<?=$customer_details['id']?>">
<?
foreach($dyncust_fields as $dyncust_field)
if($dyncust_field['add_to_listing']=='1')
$color = $customer_details[$dyncust_field['attribute_name']] == 'cname' ?'red':'green';
$search = array("color","data");
$replace = array($color,$customer_details[$dyncust_field['attribute_name']] );
$template = "<td style='color:color;'>data</td>";
echo str_replace($search,$replace,$template);
?>
</tr>
<?
?>
这里$template
是表格单元格的模板。$search
数组中的值被替换为$replace
数组的值。这样,您只需要自定义模板,搜索和替换数组。例如,以下是您在评论部分提出的问题的答案。
$template = "<td> <a href='#list-corp-client' class='view-asset-inbox-model m-r-5 text-info' data-from='corporate' data-id='id' data-pk='1' data-toggle='modal'>title</a> </td>";
$search = array("id","title");
$replace = array($customer_details['id'], $asset_details['title']);
echo str_replace($search,$replace,$template);
【讨论】:
最好将$template
和$search
排除在foreach
循环之外。我把它留在那里让你容易理解。【参考方案2】:
<?php
foreach($customer as $customer_details)
?>
<tr id="customer_details_<?=$customer_details['id']?>">
<?
foreach($dyncust_fields as $dyncust_field)
if($dyncust_field['add_to_listing']=='1')
echo "<td style='".$customer_details[$dyncust_field['attribute_name']] == 'cname' ?'color:red':'color:green'."'>".$customer_details[$dyncust_field['attribute_name']]."</td>";
?>
</tr>
<?
?>
或者您可以将类定义为内联样式或外部样式
<style>
.text-red
color:red;
.text-green
color:green;
</style>
<?php
foreach($customer as $customer_details)
?>
<tr id="customer_details_<?=$customer_details['id']?>">
<?
foreach($dyncust_fields as $dyncust_field)
if($dyncust_field['add_to_listing']=='1')
$styleClass = $customer_details[$dyncust_field['attribute_name']] == 'cname' ?'text-red':'text-green'
echo "<td class='$styleClass'>".$customer_details[$dyncust_field['attribute_name']]."</td>";
?>
</tr>
<?
?>
【讨论】:
谢谢您的回复先生。先生,如果我想给出模态的链接如何做到这一点先生..我想像这样给出以上是关于如何为codeigniter中的特定列数据赋予颜色?的主要内容,如果未能解决你的问题,请参考以下文章
如何为特定的 <td> 赋予背景颜色?为此,我必须在第三个文本框中输入位置编号(使用 java 脚本)