简体   繁体   English

jQueryUI可调整大小-当您更改列的宽度时-所有其他扬声器抽搐

[英]jQueryUI resizable - when you change the width of a column - all the other speakers twitch

I use jQueryUI resizable for resizing columns in the table. 我使用可调整大小的jQueryUI来调整表中列的大小。 When you change the width of one column, the other columns must remain unchanged. 更改一列的宽度时,其他列必须保持不变。 Should only varies the width of the table 应该只改变桌子的宽度

Why do you change the width of one column, while others twitch? 为什么要改变一列的宽度,而另一些则抽搐呢? If all columns to make the minimum width, and then begin to increase the width of the last column (right column), all the others begin to twitch. 如果所有列都达到最小宽度,然后开始增加最后一列(右列)的宽度,则其他所有列都开始抽动。

<div class="table-wrapper">
    <table id="table">
    <tbody>
        <tr>
            <td class="gray">Zagreb</td>
            <td class="gray">Kinshasa</td>
            <td class="gray">Kishinev</td>
            <td class="gray">Krakow</td>
            <td class="gray">Lima</td>
            <td class="gray">Lisbon</td>
        </tr>
        <tr>
            <td>Zagreb</td>
            <td>Kinshasa</td>
            <td>Kishinev</td>
            <td>Krakow</td>
            <td>Lima</td>
            <td>Lisbon</td>
        </tr>
        <tr>
            <td>Zagreb</td>
            <td>Kinshasa</td>
            <td>Kishinev</td>
            <td>Krakow</td>
            <td>Lima</td>
            <td>Lisbon</td>
        </tr>
        <tr>
            <td>Zagreb</td>
            <td>Kinshasa</td>
            <td>Kishinev</td>
            <td>Krakow</td>
            <td>Lima</td>
            <td>Lisbon</td>
        </tr>
        <tr>
            <td>London</td>
            <td>Los Angeles</td>
            <td>Luxembourg</td>
            <td>Madrid</td>
            <td>Manila</td>
            <td>Mexico</td>
        </tr>
        <tr>
            <td>Milan</td>
            <td>Montreal</td>
            <td>Mumbai</td>
            <td>Nairobi</td>
            <td>Nicosia</td>
            <td>New York</td>
        </tr>
        <tr>
            <td>Osaka</td>
            <td>Oslo</td>
            <td>Ottawa</td>
            <td>Paris</td>
            <td>Prague</td>
            <td>Riga</td>
        </tr>
        <tr>
            <td>Rome</td>
            <td>Rotterdam</td>
            <td>Salvador</td>
            <td>Samarkand</td>
            <td>Sydney</td>
            <td>Singapore</td>
        </tr>
        <tr>
            <td>Sofia</td>
            <td>Istanbul</td>
            <td>Taipei</td>
            <td>Tbilisi</td>
            <td>Zurich</td>
            <td>Chicago</td>
        </tr>
    </tbody>
    </table>
</div>

JS code: JS代码:

$(document).ready(function(e)
{
var $table = $("#table");
var startW = 0;
var startW_neighbor = 0;
var td_index_neighbor = 0;
var ui_minColWidth = 0;

$table.find("tr:first th, tr:first td").resizable(
    {
        handles: 'e',
        minWidth: 30,
        start : function(event,ui)
        {
            ui_minColWidth = $(this).resizable( "option", "minWidth");
            startW = $(this).width();
            startW_neighbor = ui.element.parents('.table-wrapper').find('table#table').width();
        },
        stop: function (event, ui)
        {

        },
        resize: function (event, ui)
        {
            var td_width = ui.element.width();
            var table = ui.element.parents('.table-wrapper').find('table#table');
            var d = Number(td_width) - Number(ui.originalSize.width);

            td_width = Number(startW_neighbor) + Number(d) - 2;
            table.width(td_width);
        }
    });
});

In example: http://jsfiddle.net/djmartini/08p3Lqcm/ 例如: http : //jsfiddle.net/djmartini/08p3Lqcm/

You're at the same time interfering with normal behavior of resizable and relying on it to calculate some values, so at some point it breaks. 同时,您会干扰resizable正常行为,并依靠它来计算一些值,因此有时会中断。 In that case you're calculating the width difference based on resizable behavior and applying it to change the behavior. 在这种情况下,您要根据可调整大小的行为来计算width差,并应用该width差来更改行为。 But when resizable doesn't have any more place to resize, the difference is 0, hence nothing moves anymore. 但是当resizable地方不再需要调整大小时,其差为0,因此不再移动。 If you want to change the behavior of resizable , you could work with mouse position and micro manage the change in width . 如果要更改resizable的行为,则可以使用鼠标位置并微管理width的更改。 In that case you'll need to handle cases where your mouse moves, but nothing gets resized. 在这种情况下,您需要处理鼠标移动但大小没有改变的情况。 This happens when columns cannot reduce size anymore. 当列无法再减小大小时,就会发生这种情况。 Something like this seems to work: 像这样的事情似乎起作用:

  $table.find("tr:first th, tr:first td").resizable({
        handles: 'e',
        minWidth: 30,
        start: function (event, ui) {
            ui_minColWidth = $(this).resizable("option", "minWidth");
            startW = $(this).width();
            td_width = startW;//this to check if width can change
            startW_neighbor = ui.element.parents('.table-wrapper').find('table#table').width();
            startMouseX = event.pageX;//to calculate mouse diffrence
        },
        stop: function (event, ui) {

        },
        resize: function (event, ui) {;
            var mouseDiff = event.pageX - startMouseX;//mouse mouvement
            //If the mouse is negative, it can resize only if column 
            //isn't at min width. When mouse positive resize always.
            if (ui.element.width() != td_width || mouseDiff > 0) {
                td_width = ui.element.width();

                var table = ui.element.parents('.table-wrapper').find('table#table');

                var d = Number(td_width) - Number(ui.originalSize.width);


                table_width = Number(startW_neighbor) + mouseDiff;
                table.width(table_width);

            }
        }
    });

fiddle: http://jsfiddle.net/wLbn5qjk/1/ 小提琴: http : //jsfiddle.net/wLbn5qjk/1/

EDIT: You can push this logic and work with alsoResize, the result is a bit better: 编辑:您可以推送此逻辑并与AlsoResize一起使用,结果要好一些:

$table.find("tr:first th, tr:first td").resizable({
            handles: 'e',
            minWidth: 30,
            alsoResize: 'table',
            start: function (event, ui) {
                ui_minColWidth = $(this).resizable("option", "minWidth");
                startW = $(this).width();
                td_width = ui.element.width();
                startW_neighbor = ui.element.parents('.table-wrapper').find('table#table').width();
                startMouseX = event.pageX;
            },
            stop: function (event, ui) {

            },
            resize: function (event, ui) {;
                var mouseDiff = event.pageX - startMouseX;
                if (ui.element.width() == td_width && mouseDiff < 0) {;
                    $('table').width(table_width);
                }
                td_width = ui.element.width();
                table_width = $('table').width()

            }
        });

http://jsfiddle.net/hrzj68wp/1/ http://jsfiddle.net/hrzj68wp/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM