<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>table</title>
<style type="text/css">
.itemxuanzhong{background-color: red !important;}
table{
border-collapse: collapse;
}
table .xuanzhong{display:table-row}
.dingdan_tr {display: none;}
.disi{
display: inline;
}
.juzhong{
text-align: center;
}
.disb {
display: block;
}
.scgdtbody {
height: 270px;
border: 1px solid #CCC;
border-top: none;
}
.scroolbar {
overflow: scroll;
overflow-x: hidden;
}
.trd tbody tr:nth-child(2n+1) {
background-color: #EDF1EF;
}
.trd td {
border: 1px solid #CCC;
word-break: break-all;
}
.btn{
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
background-image: -webkit-linear-gradient(#F0F0F0, #F0F0F0 38%, #E0E0E0);
border-color: rgba(0, 0, 0, 0.3);
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.12), inset 0 1px 2px rgba(255, 255, 255, 0.95);
color: #000;
border: 1px solid #CCC;
border-radius: 5px;
padding: 6px;
/* width: 80px; */
text-align: center;
cursor: pointer;
}
.input_text{
box-shadow: 0px 0px 1px #8FE7D2;
border: 1px solid #CCC;
border-radius: 5px;
font-size: 14px;
width: 180px;
height: 20px;
line-height: 20px;
color: #333;
padding: 5px 6px;
vertical-align: middle;
outline: none;
resize: none;
}
</style>
</head>
<body>
<div style="width:900px;margin:0px auto;">
<h1 style="text-align: center;">功能性Table</h1>
<div>
<h3>功能选项</h3>
1.<button class="btn" onclick="ischecked()">查看我选中的都有哪些</button><br /><br />
2. <input type="text" style="width:300px;" class="input_text" id="serach_name" placeholder="输入姓名,帮你定位到指定你要的行" />
<button class="btn" onclick="postion_obj()">定位</button><br /><br />
3.拖拽选中,支持滚动拖拽选中;<br /><br />
</div>
<div id="time_indent">
<table class="trd juzhong" style="width:854px;margin-top:10px;" >
<thead class="disi">
<tr style="height:25px;">
<td class="w158"><input type="checkbox" id="allCheck" onclick="checkAll(this)"></td>
<td style="width:103px;">姓名</td>
<td style="width:92px;">性别</td>
<td style="width:150px;">年龄</td>
<td style="width:100px;">居住地址</td>
<td style="width:90px;">身高</td>
<td style="width:90px;">体重</td>
<td style="width:90px;">爱好</td>
<td style="width:90px;">职业</td>
</tr>
</thead>
<tbody class="disb scroolbar scgdtbody" id="tb" style="height:400px;">
</tbody>
</table>
</div>
</div>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<script type="text/javascript">
window.onload=function(){
var tbody = document.getElementById("tb");
var arr=[];
for(var i=3;i<100;i++){
if(i%3==0){
arr.push({name:'小明',sex:"男",age:parseInt(i+15),address:'河北石家庄'+i+"号",height:parseInt(180+i),weight:parseInt(120+i),hobby:"打篮球",work:"后台开发"});
}else if(i%3==1){
arr.push({name:'小红',sex:"女",age:parseInt(i+15),address:'北京'+i+"号",height:parseInt(180+i),weight:parseInt(120+i),hobby:"睡觉",work:"前端开发"});
}else{
arr.push({name:'小刚',sex:"女",age:parseInt(i+15),address:'北京'+i+"号",height:parseInt(180+i),weight:parseInt(120+i),hobby:"吃饭",work:"数据库管理员"});
}
}
var str=""
for(var j=0;j<arr.length;j++){
str+=`<tr id=${j}_change_gstatus onclick="select_indent(this,${j});" gid=${j} class='ant_design'><td class="w158"><input type="checkbox" titles="all_checkboxs" onclick="select_product(this);just_select($(this).parent().parent())" value=${j} /></td><td style="width:103px;">${arr[j].name}</td><td style="width:92px;">${arr[j].sex}</td><td style="width:150px;">${arr[j].age}</td><td style="width:100px;">${arr[j].address}</td><td style="width:90px;">${arr[j].height}</td><td style="width:90px;">${arr[j].weight}</td><td style="width:90px;">${arr[j].hobby}</td><td style="width:70px;">${arr[j].work}</td></tr>`
}
$(tbody).append(str);
}
//下面开始用jq
//点击全选按钮
function checkAll(obj){
$('[titles=all_checkboxs]:checkbox').prop("checked",obj.checked);
}
//多选踩坑:对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
//jq版本问题,也是导致这个问题的关键所在
//根据单选判断是否所有的全选了
function select_product(obj){
var $tmp=$('[titles=all_checkboxs]:checkbox');
$("#allCheck").prop('checked',$tmp.length==$tmp.filter(':checked').length);
event.stopPropagation(); //阻止事件传递 解决连续响应问题
}
//查看选中状态
function ischecked(){
var vo_id="";
$('[titles=all_checkboxs]:checkbox').filter(':checked').each(function(){
vo_id+=$(this).val()+",";
});
//去除最后的逗号
vo_id= vo_id.substr(0,vo_id.length-1);
alert(vo_id);
}
function postion_obj(serach_name){
var serach_name=$("#serach_name").val();
var xz_obj=$("#tb").find(".itemxuanzhong");
var i=-1;
if(xz_obj.length!=0){
var name=xz_obj.find("td:eq(1)").html();
if(name==serach_name){
i=xz_obj.index();
}
}
var xzzz_obj=0;
var jj=0
$("#tb").find("tr").each(function(key,value){
var name =$(this).find("td:eq(1)").html();
console.info()
console.info(name);
if($(this).hasClass('ant_design')&&name==serach_name){
if(xzzz_obj==0){
xzzz_obj=$(this);
jj++;
}
}
if($(this).hasClass('ant_design')&&name==serach_name&&key>i){
xzzz_obj=$(this);
jj++;
return false;
}
})
if(jj==0){
alert("对不起没有这个人");
return false;
}
list_item_postion($("#tb"),xzzz_obj,'tr',1);
just_select(xzzz_obj);
}
function list_item_postion(parent_e,self_e,e,number){
var weizhi=self_e.index();
var heigth=0;
for(var i=0;i<weizhi;i++)
{
heigth+=parent_e.find(""+e+":eq("+i+")").height();
}
heigth/=number;
parent_e.scrollTop(heigth);
}
function just_select(obj){
$(obj).addClass("itemxuanzhong").siblings().removeClass("itemxuanzhong");
}
seletedTable($("#tb")[0],"tr",function(a){
console.info($(a.getElementsByTagName('input')[0]).is(':checked'))
if (!$(a.getElementsByTagName('input')[0]).is(':checked')){
a.getElementsByTagName('input')[0].click();
}else{
a.getElementsByTagName('input')[0].click();
}
})
//拖拽选中,加滚动条
function seletedTable(box,markx ,callBack) {
box.onmousedown = function() {
var boxSocll = box.scrollTop;
var selList = [];
var fileNodes = $(box).find(markx)
for ( var i = 0; i < fileNodes.length; i++) {
selList.push(fileNodes[i]);
}
var isSelect = true;
var evt = window.event || arguments[0];
var startX = (evt.x || evt.clientX);
var startY = (evt.y || evt.clientY)+boxSocll;
var selDiv = document.createElement("div");
selDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#00D500;z-index:1000;filter:alpha(opacity:0);opacity:0;display:none;";
selDiv.id = "selectDiv";
document.body.appendChild(selDiv);
var selDiv2 = document.createElement("div");
selDiv2.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;display:none;";
document.body.appendChild(selDiv2);
selDiv.style.left = startX + "px";
selDiv.style.top = (startY-boxSocll)+"px";
var _x = null;
selDiv2.style.left = startX + "px";
selDiv2.style.top = (startY-boxSocll)+"px";
var _x = null;
var _y = null;
clearEventBubble(evt);
box.onmousemove = function() {
var boxSocll = box.scrollTop;
evt = window.event || arguments[0];
if (isSelect) {
if (selDiv2.style.display == "none") {
selDiv2.style.display = "";
}
if (selDiv.style.display == "none") {
selDiv.style.display = "";
}
_x = (evt.x || evt.clientX);
_y = (evt.y || evt.clientY);
selDiv.style.left = Math.min(_x, startX) + "px";
selDiv.style.top = Math.min(_y, (startY-boxSocll)) + "px";
selDiv.style.width = Math.abs(_x - startX) + "px";
selDiv.style.height = Math.abs(_y - startY+boxSocll) + "px";
selDiv2.style.left = Math.min(_x, startX) + "px";;
selDiv2.style.top = Math.max(Math.min(_y, (startY-boxSocll)),$(box).offset().top) + "px";
selDiv2.style.width = selDiv.style.width;
selDiv2.style.height = Math.min( Math.abs(_y - startY+boxSocll),box.offsetHeight) + "px";
if(Math.min(_y, (startY-boxSocll))<$(box).offset().top)
{
selDiv2.style.height = (Math.abs(_y - startY+boxSocll)- Math.abs(Math.min(_y, (startY-boxSocll))-Math.max(Math.min(_y, (startY-boxSocll)),$(box).offset().top))) + "px";
}else{
if(Math.min(_y, (startY-boxSocll))+ Math.abs(_y - startY+boxSocll)>box.offsetHeight){
selDiv2.style.height =Math.min(Math.abs(_y - startY+boxSocll),( (box.offsetHeight+$(box).offset().top) -Math.max(Math.min(_y, (startY-boxSocll)),$(box).offset().top))) + "px";
}
}
if((_y-$(box).offset().top)>(box.offsetHeight*0.9)){
box.scrollTop+=10
if((_y-$(box).offset().top)>(box.offsetHeight*0.91)){
box.scrollTop+=10
}
if((_y-$(box).offset().top)>(box.offsetHeight*0.92)){
box.scrollTop+=20
}
if((_y-$(box).offset().top)>(box.offsetHeight*0.95)){
box.scrollTop+=30
}
if((_y-$(box).offset().top)>(box.offsetHeight*0.98)){
box.scrollTop+=100
}
}else{
if((_y-$(box).offset().top)<(box.offsetHeight*0.5)){
if(box.scrollTop>0){
box.scrollTop-=10;
if((_y-$(box).offset().top)<(box.offsetHeight*0.3)){
box.scrollTop-=20;
}
if((_y-$(box).offset().top)<(box.offsetHeight*0.2)){
box.scrollTop-=50;
}
if((_y-$(box).offset().top)<(box.offsetHeight*0.1)){
box.scrollTop-=100;
}
if((_y-$(box).offset().top)<(box.offsetHeight*0.05)){
box.scrollTop-=200;
}
if((_y-$(box).offset().top)<(box.offsetHeight*0.02)){
box.scrollTop-=500;
}
}
if(box.scrollTop<0){
box.scrollTop=0;
}
}
}
// ---------------- 关键算法 ---------------------
//var _l = selDiv.offsetLeft, _t = selDiv.offsetTop+boxSocll-$(box).offset().top;
var _l = $(selDiv).offset().left, _t = $(selDiv).offset().top;
var _w = selDiv.offsetWidth, _h = selDiv.offsetHeight;
for ( var i = 0; i < selList.length; i++) {
var sl = selList[i].offsetWidth + $(selList[i]).offset().left;//lList[i].offsetLeft;
var st = selList[i].offsetHeight +$(selList[i]).offset().top;// selList[i].offsetTop;
if (sl > _l && st > _t && $(selList[i]).offset().left < _l + _w && $(selList[i]).offset().top < _t + _h) {
if (selList[i].className.indexOf("selecting") == -1) {
$(selList[i]).addClass('selecting');
}
} else {
if (selList[i].className.indexOf("selecting") != -1) {
$(selList[i]).removeClass('selecting');
}
}
}
}
clearEventBubble(evt);
}
selDiv.onmousemove=box.onmousemove;
selDiv2.onmousemove=box.onmousemove;
document.onmouseup = function() {
isSelect = false;
if (selDiv) {
document.body.removeChild(selDiv);
document.body.removeChild(selDiv2);
showSelDiv(selList,callBack);
}
selList = null, _x = null, _y = null, selDiv = null, startX = null, startY = null, evt = null;
}
}
};
function clearEventBubble(evt) {
if (evt.stopPropagation)
evt.stopPropagation();
else
evt.cancelBubble = true;
if (evt.preventDefault)
evt.preventDefault();
else
evt.returnValue = false;
}
function showSelDiv(arr,callBack) {
var count = 0;
var selInfo = "";
for ( var i = 0; i < arr.length; i++) {
if (arr[i].className.indexOf("selecting") != -1) {
if(callBack!=undefined){
callBack(arr[i])
}
count++;
selInfo +=(i+1)+"\n";
$(arr[i]).removeClass('selecting')
}
}
}
</script>