【问题标题】:checkbox list alignment in to 2 or 3 columns复选框列表对齐到 2 或 3 列
【发布时间】:2014-10-14 21:25:41
【问题描述】:

如何使用 css 或 jquery 将其更改为 2 列或 3 列,目前它在一列中

这里是小提琴的链接。 http://jsfiddle.net/4510f0yx/1/

<table class="items table table-striped table-bordered table-condensed">
    <thead>
        <tr>
            <th id="operations_c0" class="checkbox-column">
                <input type="checkbox" id="operations_c0_all" name="operations_c0_all" value="1" class="select-on-check-all">
            </th>
            <th id="operations_c1">Permissions</th>
        </tr>
    </thead>
    <tbody>
        <tr class="">
            <td colspan="2" class="extrarow"><strong>Auth :: Permission</strong>
            </td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_0" value="oAuthOperationCreate" class="select-on-check">
            </td>
            <td>Create</td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_1" value="oAuthOperationRead" class="select-on-check">
            </td>
            <td>Read</td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_2" value="oAuthOperationUpdate" class="select-on-check">
            </td>
            <td>Update</td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_3" value="oAuthOperationDelete" class="select-on-check">
            </td>
            <td>Delete</td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_4" value="oAuthOperationList" class="select-on-check">
            </td>
            <td>List</td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_5" value="oAuthOperationManage" class="select-on-check">
            </td>
            <td>Manage</td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_6" value="oAuthOperationSearch" class="select-on-check">
            </td>
            <td>Search</td>
        </tr>
        <tr class="">
            <td colspan="2" class="extrarow"><strong>Auth :: Parent</strong>
            </td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_7" value="oAuthParentCreate" class="select-on-check">
            </td>
            <td>Create</td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_8" value="oAuthParentRead" class="select-on-check">
            </td>
            <td>Read</td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_9" value="oAuthParentUpdate" class="select-on-check">
            </td>
            <td>Update</td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_10" value="oAuthParentDelete" class="select-on-check">
            </td>
            <td>Delete</td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_11" value="oAuthParentList" class="select-on-check">
            </td>
            <td>List</td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_12" value="oAuthParentManage" class="select-on-check">
            </td>
            <td>Manage</td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_13" value="oAuthParentSearch" class="select-on-check">
            </td>
            <td>Search</td>
        </tr>
        <tr class="">
            <td colspan="2" class="extrarow"><strong>Auth :: Role</strong>
            </td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_14" value="oAuthRoleCreate" class="select-on-check">
            </td>
            <td>Create</td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_15" value="oAuthRoleRead" class="select-on-check">
            </td>
            <td>Read</td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_16" value="oAuthRoleUpdate" class="select-on-check">
            </td>
            <td>Update</td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_17" value="oAuthRoleDelete" class="select-on-check">
            </td>
            <td>Delete</td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_18" value="oAuthRoleList" class="select-on-check">
            </td>
            <td>List</td>
        </tr>
        <tr class="even">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_19" value="oAuthRoleManage" class="select-on-check">
            </td>
            <td>Manage</td>
        </tr>
        <tr class="odd">
            <td class="checkbox-column">
                <input type="checkbox" name="AuthItem[seltedOperations][]" id="operations_c0_20" value="oAuthRoleSearch" class="select-on-check">
            </td>
            <td>Search</td>
        </tr>
    </tbody>
</table>

【问题讨论】:

  • 所以不能改html?
  • 是的,它基于一个框架,如果我们有一个 css 解决方案会更好,如果不是在最坏的情况下也可以
  • 您不能使用 css 更改 &lt;tr&gt; &lt;td&gt; 的对齐方式。你必须改变你的 HTML sn-p
  • 好的,但我认为使用 jquery 是可能的。
  • 是的,肯定可以使用 Jquery。像这样stackoverflow.com/a/6298066/2236219

标签: jquery html css


【解决方案1】:

我真的认为你需要用框架中的 3 列来构建你的 html,这比用 js 改变表更好。

如果不是……

像这样添加到您的HTML“列分隔符”类中:

<tr class="extra">
   <td colspan="2" class="extrarow"><strong>Auth :: Permission</strong> </td>
</tr>

而你的js 可以是:

$(function () {

    var columns = [];
    $('tr.extra').each(function (i) {

        var curr_col = $(this);   
        var next_col = $('tr.extra').eq(i+1); 

        var column = curr_col.nextUntil(next_col);

        columns[i] = column;        

    });

    columns.reverse();
    var len = columns.length;

    for (i = len-2; i >= 0; i--) {

        columns[i].each( function (index, value){

            var tds = $(this).children('td');
            columns[len-1].eq(index).append(tds);
            $(this).remove();

        }); 

    }

});

从这里开始 - jsfiddle

【讨论】:

    【解决方案2】:

    使用这个:

    table.items {column-count: 2;-moz-column-count: 2;-webkit-column-count: 3;
    width: 300px;height: 272px;display: table-cell;}
    

    但它不适用于 IE

    【讨论】:

    【解决方案3】:

    这里有一些东西可以帮助你开始

    CSS

    table, thead, tbody, tr, th, td {
        display: block;
        text-align: left;
    }
    table, thead, tbody {
        width: 100%;
    }
    tr {
            position: absolute;
    }
    tr:after {
        content:' ';
        display: block;
        clear: both;
    }
    td, th {
        float: left;
    }
    

    jQuery

    $(function () {
        var left = -1;
        var top = 0;
        $('tr').each(function () {
            var extrarow = $('td', this).hasClass('extrarow');
            top += $('td',this).first().height();
            if(extrarow) {
                left += 1;
                top = 0;
            }
            $(this).css({
                width: '33.33%',
                top: top,
                'left': (left * 33.33) + '%'
            });
        });
    });
    

    示例

    开启JSFIDDLE

    它有一些已知的错误,例如没有包含标题,但您应该能够从中获得完整的解决方案。

    【讨论】:

    • 这是与一侧对齐的,因为所有这些都在一行和多列中,这使得水平滚动的时间更长。
    • 这里是其显示方式的图片。 bild.me/bild.php?file=7586680Capture1.PNG
    • position: relative 添加到table 应该会更好一些,但是对于定位元素来说,下推下面的内容可能会很棘手。另一种解决方案是将行放入 2 或 3 个左浮动容器中,并在末尾有明确的固定。
    • 请您更新答案以使其在有 3 列时插入新行。下一列 4,5,6 然后是另一行 7,8,9。所以这将完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2020-05-05
    • 2016-10-23
    相关资源
    最近更新 更多