单击复选框时,可扩展和可折叠功能正在运行

时间:2016-07-26 08:44:49

标签: html responsive-design datatables html-table

我使用jquery datatable创建了一个表格。我还使用datatable的响应功能来使其响应。代码工作正常,但我面临的问题是表的第一列有一个复选框,当我们将表宽度调整为较小宽度时,可折叠选项将如下所示

enter image description here

现在,当我们点击checkbox可折叠功能正在运行时。

我的代码如下所示

Working Demo

<table id="example" class="table table-striped table-hover dt-responsive display nowrap" cellspacing="0"> 
        <thead>
            <tr>
                <th></th>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox"></td>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
            :
        </tbody>
   </table>

<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/plug-ins/a5734b29083/integration/bootstrap/3/dataTables.bootstrap.js"></script>
<script src="dataTables.responsive.js" type="text/javascript"></script>
<script>
    $(document).ready(function () {
        $('#example')
                .DataTable({
                    "responsive": true,

                    "dom": '<"top"lf>t<"bottom"pi><"clear">'
                });
    });
</script>

当我们点击checkbox

时,有人可以告诉我如何防止折叠和展开吗?

2 个答案:

答案 0 :(得分:6)

在位置0再添加一个td

Here 是其工作演示

答案 1 :(得分:6)

只需替换

$(document).ready(function () {
    $('#example').DataTable({
       "responsive": true,
       "dom": '<"top"lf>t<"bottom"pi><"clear">'
    });
});

通过

$(document).ready(function () {
    $('#example').DataTable({
       "responsive": true,
       "dom": '<"top"lf>t<"bottom"pi><"clear">'
    });

    $('td').on('click mousedown mouseup', function(e) {
        if (e.target.type === 'checkbox') {
           e.stopPropagation();
        }
    });
});

如果您点击复选框

,此部分将停止点击传播
$('td').on('click mousedown mouseup', function(e) {
    if (e.target.type === 'checkbox') {
        e.stopPropagation();
    }
});

plunker:http://plnkr.co/edit/PCHdDM7UGD0BLXoDhhD6?p=preview

编辑:

要删除单元格单击功能并将其仅添加到折叠/展开按钮,我必须创建一个新的html元素并将其放入单元格中:'<div class="clickBtn">+</div>'

为什么呢?因为旧的按钮是一个之前的伪元素,所以它实际上并不在dom中。我需要dom中的一个元素,只允许在单击此特定元素时消耗/崩溃,而不是整个单元格。

然后我给这个按钮提供了旧按钮的css

.clickBtn {
   -webkit-touch-callout: none;
   -webkit-user-select: none; 
   -khtml-user-select: none;
   -moz-user-select: none; 
   -ms-user-select: none; 
   user-select: none;        
   cursor: pointer;
   top: 9px;
   left: 4px;
   height: 14px;
   width: 14px;
   position: absolute;
   color: white;
   border: 2px solid white;
   border-radius: 14px;
   box-shadow: 0 0 3px #444;
   box-sizing: content-box;
   text-align: center;
   font-family: 'Courier New', Courier, monospace;
   line-height: 14px;
   background-color: #337ab7;
}

并删除旧的:

table.dataTable.dtr-inline.collapsed>tbody>tr>td:first-child:before, table.dataTable.dtr-inline.collapsed>tbody>tr>th:first-child:before {
   display: none;
}

现在我需要更改单元格点击行为,只有在您点击折叠/展开按钮时才会发生。

$('td').on('click mousedown mouseup', function(e) {
   if (e.target.className === 'clickBtn showBtn') {
       if (e.target.innerText === '+') {
           e.target.innerText = '-';
           e.target.style.backgroundColor = 'red';
       } else {
           e.target.innerText = '+';
           e.target.style.backgroundColor = '#337ab7';
       }
   } else {
       e.stopPropagation();
   }
});

当您点击该单元格时,如果您点击的目标不是具有&#39; clickBtn&#39;和&#39; showBtn&#39;它停止事件传播。这就是为什么现在只有clickBtn可以消耗/崩溃的原因。

这部分只是重新创建旧的按钮颜色和文本更改:

if (e.target.innerText === '+') {
    e.target.innerText = '-';
    e.target.style.backgroundColor = 'red';
} else {
    e.target.innerText = '+';
    e.target.style.backgroundColor = '#337ab7';
}

编辑2:

要删除蓝色部分,即文本选择,我必须将此css添加到按钮:

-webkit-touch-callout: none;
-webkit-user-select: none; 
-khtml-user-select: none;
-moz-user-select: none; 
-ms-user-select: none; 
user-select: none;