【问题标题】:Highlight a row of a table with JQuery / Bootstrap using anchors使用锚点使用 JQuery / Bootstrap 突出显示表格的一行
【发布时间】:2013-06-01 19:23:33
【问题描述】:

我正在使用 Bootstrap (Twitter) 和 JQuery。我有一个包含一些数据的表,每一行都有一个 id。我有一个预先输入来在我的表中搜索数据。当我在预输入中选择数据时,我想突出显示正确的行,这就是我使用锚点的原因。但是,我不知道如何突出显示该行。

这是我的 JQuery 代码:

$(document).ready(function() {
    $('#typeahead').change(function() {
        window.location = "#" + $(this).val();
        //highlighting the row...
    });
});

此 HTML 代码仅用于测试:

<a href="#row1">Row1</a>
<a href="#row2">Row2</a>

<table class="table table-hover table-bordered">
    <tr id="row1">
        <td>A</td>
        <td>B</td>
    </tr>
    <tr id="row2">
        <td>C</td>
        <td>D</td>
    </tr>
</table>

这里是预先输入的代码:

<input type="text" id="typeahead" data-provide="typeahead" placeholder="Search a name" data-items="4" data-source='[
<?php for($i = 0 ; $i < count($typeahead) ; $i++) {

if($i+1 == count($typeahead))
echo '"'.$typeahead[$i].'"';
else
echo '"'.$typeahead[$i].'",';
} 

?>]'>

这里是预输入数组的内容:

<input type="text" id="typeahead" data-provide="typeahead" placeholder="Search a name" data-items="4" data-source='["Christophe Chantraine","Toto Tuteur","Henris Michaux","Robert Patinson","Benjamin Brégnard","Jean-Charles Terzis","Ludovic Dermience","Claude Dianga"]'>

这里是一个示例代码来介绍我的问题:http://jsfiddle.net/TK7QP/6/

【问题讨论】:

  • 你能把$typeahead数组的内容贴出来吗?

标签: jquery twitter-bootstrap anchor highlight bootstrap-typeahead


【解决方案1】:

不要在表格行上使用 id 属性,而是将其更改为 data-name。示例:

<tr data-name="Christophe Chantraine">
    <td>A</td>
    <td>B</td>
</tr>

将此 CSS 添加到您的样式表中:

.table-hover tbody tr.selected > td {
  background-color: #f5f5f5;
}

然后将您的 jQuery 代码更改为:

$(document).ready(function() {
    $('#typeahead').change(function() {
        window.location = "#" + $(this).val();
        //highlighting the row...
        $('tr[data-name="' + $(this).val() + '"]').addClass('selected');
    });
});

通过数据属性查找元素比通过 id 需要稍长的时间,但除非您有大量的表格行,否则不会引起注意。使用数据属性是最简单的,因为您必须将名称“slugify”才能将它们用作 id,这意味着删除所有空格、特殊字符等。

----使用 id 属性的替代答案,以便您可以链接到表行----

为此,您需要替换姓名中的空格。下面是一个使用 PHP 的示例:

<table class="table table-hover table-bordered">
    <tr id="<?php echo str_replace(' ', '_', 'Christophe Chantraine');?>">
        <td>A</td>
        <td>B</td>
    </tr>
    <tr id="<?php echo str_replace(' ', '_', 'Benjamin Brégnard');?>">
        <td>C</td>
        <td>D</td>
    </tr>
</table>

当你链接到行时,你的锚点也需要有下划线:

&lt;a href="#Christophe_Chantraine"&gt;Christophe Chantraine&lt;/a&gt;

那么你的 jQuery 应该是这样的:

$(document).ready(function() {

    $('#typeahead').change(function() {
        $('tr').removeClass('selected'); // remove class from other rows
        $('#' + $(this).val().replace(' ', '_')).addClass('selected');
        window.location = "#" +  $(this).val().replace(' ', '_');
    });
});

要添加过渡效果,您可以在 CSS 中执行类似的操作。如果一秒太长,请更改过渡的长度:

.table-hover tbody tr.selected > td {
  background-color: #f5f5f5;
    -webkit-transition: background 1s linear;
    -moz-transition: background 1s linear;
    -ms-transition: background 1s linear;
    -o-transition: background 1s linear;
    transition: background 1s linear;
}

【讨论】:

  • 我试过了,但它不起作用。我添加了我预先输入的代码。感谢您的帮助!
  • 其实我要选择"tr" where id = $(this).val();但我不知道如何...我正在寻找...
  • 如何选择 tr 可能取决于 td 内容是否与 typeahead 结果完全匹配,因此您应该发布 $typeahead 数组
  • 是的,确实,预输入中选择的名称与我表中的 id 完全相同。所以,像这样:...
  • 现在开始明朗了!问题是 id 属性中不能有空格或特殊字符。它们只能有字母、数字、下划线、连字符、冒号和句点,并且必须以字母开头。我将用我认为最简单的解决方案更新我上面的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多