How to make CakePHP's HABTM checkboxes alphabetical top to bottom in columns

后端 未结 1 437
再見小時候
再見小時候 2021-01-17 01:29

I have list of restaurant cuisines (HABTM) - when the user adds a restaurant, they choose from all the checkboxes of cuisines.

The checkbox inputs are set to float:l

相关标签:
1条回答
  • 2021-01-17 02:09

    Based on the comments, I've created a hopefully acceptable jQuery solution.

    See: http://jsfiddle.net/svRmL/

    var $element = $('#cuisines');
    var $elementWidth = $element.find(' > .checkbox').outerWidth(true),
        elementCount = $element.find(' > .checkbox').length,
        $boxes = $element.find(' > .checkbox');
    
    /* just for debug */
    $boxes.each(function(i) {
        $(this).find('label').html(i);
    });
    
    //set resize function
    $(window).resize(function() {
        var perRow = Math.floor($element.width() / $elementWidth),
            i, j, $thisColumn, inc;
    
        $boxes.appendTo($element); //move elements out of columns from previous resize
        $('.tempColumn').remove(); //destroy old columns
        for (i = 0; i < perRow; i++) {
            $thisColumn = $('<div class="tempColumn" />').appendTo($element).css({
                width: $elementWidth,
                float: 'left'
            });
            inc = Math.ceil(elementCount / perRow);
            for (j = inc * i; j < inc * (i + 1); j++) {
                $boxes.eq(j).appendTo($thisColumn);
            }
        }
    }).resize(); //trigger resize function immediately
    
    0 讨论(0)
提交回复
热议问题