简体   繁体   English

同步表调整大小

[英]Synchronize Table Re-sizing

I'm using the JQuery plugin colResizable ( @alvaro-prieto ) to create re-sizable tables in my application and have two individual table objects (same column count). 我正在使用JQuery插件colResizable@ alvaro-prieto )在我的应用程序中创建可重新调整大小的表,并且具有两个单独的表对象(相同的列数)。 However, I am looking for a way to resize the two tables at the same time (eg Updating Table 1's columns also updates Table 2's columns synchronously). 但是,我正在寻找一种方法来同时调整两个表的大小(例如,更新表1的列也会同步更新表2的列)。 See the following JSFiddle for an interactive implementation of the plugin. 有关插件的交互式实现,请参见以下JSFiddle

$("#sample").colResizable({liveDrag:true});

$("#sample2").colResizable({
    liveDrag:true,
    gripInnerHtml:"<div class='grip'></div>", 
    draggingClass:"dragging" });

I've noticed you can specify a callback function such as onDrag that consistently fires when 'dragging' a column. 我注意到您可以指定一个回调函数(例如onDrag ,该函数在“拖动”列时始终会触发。 While this helps me get closer to a solution, I am still unaware of a concrete method to synchronize the tables' updates. 尽管这可以帮助我更接近解决方案,但我仍然不知道同步表更新的具体方法。

I am looking to avoid modifying the source if possible and find a suitable JS/JQ solution. 我希望尽可能避免修改源代码,并找到合适的JS / JQ解决方案。

Unfortunatelly the plugin doesn't seem to have a way to resize the column programatically. 不幸的是,该插件似乎没有以编程方式调整列大小的方法。 Now, you can use the onDrag event to simulate a resize in the other table's columns. 现在,您可以使用onDrag事件在另一个表的列中模拟调整大小。

So far I've been able to do so, but this "hacky" way doesn't seem to work that well. 到目前为止,我已经能够做到这一点,但是这种“ hacky”方式似乎效果不佳。

Here is the function that I call every time a column is dragged: 这是每次拖动列时调用的函数:

function syncTableWidth(e){
    var parent = e.currentTarget;

    $("table").filter(function(){return $(this).attr("id") != $(parent).attr("id")}).each(function(){
        //Match the width
        $("tr th", this).each(function(index){
            $(this).css("width",$("tr th:nth-of-type("+(index+1)+")", parent).css("width"))
        });
        //Match the grip's position
        $(this).prev().find(".JCLRgrip").each(function(index){
            $(this).css("left",$(parent).prev().find(".JCLRgrip:nth-of-type("+(index+1)+")").css("left"));
       });
    }); 
}

Now you can call this function like this: 现在您可以像下面这样调用此函数:

$("#my_table").colResizable({
    liveDrag:true,
    gripInnerHtml:"<div class='grip'></div>", 
    draggingClass:"dragging",
    onDrag: syncTableWidth
});

Here is the JSFiddle I was working on. 这是我正在研究的JSFiddle Hopefully it wil lhelp you or someone else keep improving it. 希望它会对您或其他人有所帮助。

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

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