【问题标题】:(CAKEPHP) Highlight a row in a Product table for a comparison condition (no hovering needed)(CAKEPHP)突出显示产品表中的一行以获得比较条件(无需悬停)
【发布时间】:2014-02-17 18:52:43
【问题描述】:

您如何找到正确的方法来进行这种比较?我尝试了各种方法。我什至使用了 id,但他们没有回应。如果我做这个“gumboots”字符串检查,它确实有效。 “gumboots”只是存在于表格某处的产品名称的字符串值。这就是我知道我根本不需要 PHP 的原因,尽管在下面的索引视图中的 PHP 中显示了表格。任何想法?我会很感激。

这是javascript

$('#example tbody tr td').each(function() 
{

                 var p_no_in_stock = parseInt(document.getElementById('p_no_in_stock')).value;
                 var p_reorder_quantity = parseInt(document.getElementById('p_reorder_quantity')).value;


//if ($product['Product']['p_no_in_stock'].val() < $product['Product']['p_reorder_quantity'].val())
if ($(this).text() == "gumboots") 
//if ($(this).p_no_in_stock < $(this).p_reorder_quantity)
{
//$("#row_" +" td").effect("highlight", {}, 1500);
$(this).parent().attr('style','background:red');
$(this).parent().css('font-weight','bold');

} 

}); 

这是一个名为 Products.index 的视图中的应用程序

<div class="active">

        <h2><?php echo __('Products'); ?></h2>

        <table cellpadding="0" cellspacing="0" class="table table-striped table-bordered" id ="example">

            <tr>

                <th><?php echo $this->Paginator->sort('p_name', 'Name'); ?></th>
                <th><?php echo $this->Paginator->sort('category_name', 'Category'); ?></th>
                <th><?php echo $this->Paginator->sort('p_no_in_stock','No. in Stock'); ?></th>
                <th><?php echo $this->Paginator->sort('p_reorder_quantity', 'Re-order Quantity'); ?></th>
                <th class="actions"><?php echo __('Actions'); ?></th>
            </tr>
            <tbody>
            <?php foreach ($products as $product): ?>
                <tr>

                    <td><?php echo h($product['Product']['p_name']); ?></td>
                    <td> <?php echo $this->Html->link($product['Category']['category_name'], 
                    array('controller' => 'categories', 'action' => 'view', $product['Category']['id'])); ?>
                    </td>      
                    <td id = "p_no_in_stock" type ="number" ><?php echo h($product['Product']['p_no_in_stock']); ?>&nbsp;</td>
                    <td id ="p_reorder_quantity" type ="number" ><?php echo h($product['Product']['p_reorder_quantity']); ?>&nbsp;</td>
                    <td class="actions">
                        <?php echo $this->Html->link(__('View'), array('action' => 'view', $product['Product']['id']), array('class' => 'btn btn-mini')); ?>
                        <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $product['Product']['id']), array('class' => 'btn btn-mini')); ?>
                        <?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $product['Product']['id']), array('class' => 'btn btn-mini'), __('Are you sure you want to delete # %s?', $product['Product']['id'])); ?>
                    </td>
                </tr>
            <?php endforeach; ?>
                </tbody>
        </table>

提前致谢。

解决的问题

删除类型编号

编辑tr标签为

<tr class ="item-row"> after foreach part.

【问题讨论】:

  • 让我看看我是否明白你在这里想要做什么。所以你正在渲染一个表,并且你想在其中搜索一个特定的字符串。如果您在任何行中找到它,您希望突出显示该行以指示匹配。对吗?
  • 不,我想突出显示产品的行,其中一个名为“p_no_in_stock”的数字的比较小于“p_reorder_quantity”。每个产品都有这些属性。我只是想表明简单的字符串条件与突出显示功能一起使用,并且我希望比较可以使用它。
  • 嗯,好的,知道了。因此,与需要重新订购的产品相比,您希望突出显示库存产品较少的行。我会在下面发布一些内容供您尝试,请稍等。

标签: javascript php css cakephp highlighting


【解决方案1】:

首先,您需要更改几件事:

  1. 从您的 td 标记中删除 type="number" 属性。 type-attribute 仅适用于 input-tags
  2. 用户类代替 ID(用于 p_no_in_stock 和 p_reorder_quantity)。 ID 用于唯一项目(又名:仅在屏幕上出现一次),而类则没有此限制。而且由于您是在循环中生成 td... 您拥有超过 1 个的可能性非常高。

看看这个这个小提琴:http://jsfiddle.net/FTh4m/ 看看一个工作的一些工作代码(它假设你在页面上有 jQuery)。

如果你对 jQuery 不太熟悉,我可以解释一下每一行的作用。


至于为什么你目前拥有的东西不起作用......解释有点长,但我会尽力解释:

您的选择 ($('#example tbody tr td')) 将在屏幕上返回一个包含所有 td 的数组。 而当您执行document.getElementById() 时,它只会获取它在DOM 中找到的该ID 的第一个实例(我认为IE 实际上是自下而上搜索,但这是另一天的故事)。 因此,在每个循环中,您实际上是在浏览页面上的每一个 td,而不仅仅是您关心的 td。

未来的专业提示,当尝试调试这种类型的客户端 JS 时,打开 Chrome Dev Tools(或 FireBug,如果您是 FireFox 类型的人)并开始测试您的 jQuery 选择器在控制台中。

【讨论】:

  • 非常感谢。我对其进行了调整,并且像魅力一样完美地工作。向你致敬。我会编辑我的,以便向其他想知道的人展示在哪里做。
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 2018-01-07
  • 2014-01-06
  • 1970-01-01
相关资源
最近更新 更多