这是我第一次使用list.js,由于某种原因它无法使用。
这是一个实例。 http://hartslogmuseum.com/bookhjr10/test.php 这是它应该做的 http://listjs.com/examples
这是我的代码。我只想搜索名字。
<div class="table-responsive">
<div id="catalog">
<input class="search" placeholder="Search" />
<button class="sort" data-sort="name">
Sort Name
</button>
<button class="sort" data-sort="cat">
Sort Category
</button>
<h2> Catalog </h2>
<table class="list table-bordered table-striped">
<tr>
<td> <h2 class="name">Item Name</h2> </td>
<td><h2 class="cat">Item Category</h2></td>
<td> <h2>Thumbnail</h2></td>
<td> <h2 class="descr">Item Desc</h2> </td>
<td> <h2 class="time">Time Frame</h2> </td>
<td> <h2 class="donor">Donor</h2> </td>
</tr>
$resultSet = mysqli_query($conn,"SELECT * FROM bookhjr10_items");
While($row = mysqli_fetch_array($resultSet))
{
$i=0;
echo "<tr>";
echo "<td class=\"name\">".$row['name']. "</td>";
echo "<td class=\"cat\">".$row['category']. "</td>";
echo "<td><a href=\"fullimage.php?id=".$row['id']."\" data-lightbox=\pic\"><img src=\"image.php?id=".$row['id']."\" alt=\"thumb-1\" /></a></td>";
echo "<td class=\"descr\">". $row['descr'] . "</td>";
echo "<td class=\"time\">". $row['time'] . "</td>";
echo "<td class=\"donor\">". $row['donor'] . "</td>";
echo "</tr>";
$i++;
}
?>
</table>
</div>
</div>
<script type="text/javascript">
var options = {
valueNames: [ 'name' ]
};
var hackerList = new List('catalog', options);
</script>
答案 0 :(得分:15)
您可能忘记了最低要求之一。
这似乎是最低要求:
list.js
tbody
必须包含班级list
td
中定义,而不是在th
- 虽然您也可以将其添加到th
)。< / LI>
sort
data-sort
,其值与列的名称相匹配以下是一些使用表格的list.js codepen示例(来自http://listjs.com/examples/add-get-remove):
最小代码示例:
<!-- for a table with a parent div of id "my-cool-sortable-table-wrapper", -->
<!-- and columns of class names "gender", "age" & "city" -->
<div id="my-cool-sortable-table-wrapper">
<table>
<thead>
<th class="sort" data-sort="gender">Gender</th>
<th class="sort" data-sort="age">Age</th>
<th class="sort" data-sort="city">City</th>
</thead>
<tbody class="list">
<tr>
<td class="gender">male</td>
<td class="age">18</td>
<td class="city">Berlin</td>
</tr>
<tr>
<td class="gender">female</td>
<td class="age">46</td>
<td class="city">Reykjavik</td>
</tr>
<tr>
<td class="gender">female</td>
<td class="age">20</td>
<td class="city">Lisboa</td>
</tr>
<!-- and so on ...-->
</tbody>
</table>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.1.1/list.min.js"></script>
<script type="text/javascript">
var options = {
valueNames: [ 'gender', 'age', 'city' ]
};
var contactList = new List('my-cool-sortable-table-wrapper', options);
</script>
注意:如果排序行为奇怪 - 即。排序不正确 - 可能是因为您缺少一个基本要求。如果你挣扎,那就做一个jsfiddle&amp;在stackoverflow上询问你的问题,并附上一个链接。
如果您想避免在包装元素中使用此ID,并使用自定义选择器,则可以替换:
var contactList = new List('my-cool-sortable-table-wrapper', options);
由此:
var wrapperElement = $('.my .custom .selector');
var contactList = new List(wrapperElement[0], options);
请参阅:
如果您想要检测由List.js
&amp;触发的更改事件相应地行动(这里我们相应地更新行类名称)
contactList.on("updated", function(){
$('#my-cool-sortable-table-wrapper tr').removeClass('odd').filter(':visible:odd').addClass("odd");
})
List.js
处理不同的事件类型。请参阅this page http://listjs.com/docs/list-api
答案 1 :(得分:2)
将list.js与表格一起使用时,您应将class="list"
添加到<tbody>
代码而不是<table>
代码。
因此,在您的代码中更改此部分:
<table class="list table-bordered table-striped">
<tr>
到
<table class="table-bordered table-striped">
<tbody class="list">
<tr>
不要忘记添加</tbody>
。
答案 2 :(得分:-1)
***Add php tag before you start php code in an html content.***
//You can refer to below example:
<div class="table-responsive">
<div id="catalog">
<input class="search" placeholder="Search" />
<button class="sort" data-sort="name">
Sort Name
</button>
<button class="sort" data-sort="cat">
Sort Category
</button>
<h2> Catalog </h2>
<table class="list table-bordered table-striped">
<tr>
<td> <h2 class="name">Name</h2> </td>
</tr>
<?php
$sql="SELECT * FROM country";
$resultSet = mysql_query($sql,$conn);
while($row = mysql_fetch_array($resultSet))
{?>
<tbody >
<tr><td class="name">
<?php
$i=0;
echo $row['cn_name'];
$i++;
}?>
</td></tr>
</tbody>
</table>
</div>
</div>
//Javascript
<script src="http://listjs.com/no-cdn/list.js"></script>
<script type="text/javascript">
var options = {
valueNames: [ 'name' ]//you can specify more columns by adding ,
};
var hackerList = new List('catalog', options);
</script>