knockoutjs 中数组更改后 Jquery UI 选项卡初始化

发布于 2024-12-14 11:51:13 字数 1404 浏览 0 评论 0原文

根据我过去所做的(在使用 knockoutjs 之前),每当我更新页面上的视图时,我都会调用 tabs() 函数。我正在尝试在我已经完成的knockoutjs 页面上执行此操作。每次我更改可观察数组时,它都会取消初始化 jquery 选项卡。没关系,我只需调用该函数即可。这似乎没有什么作用。

    var viewModel = {       

    lines: ko.observableArray([]),      
    activeTab: ko.observable(),         
    addLine : function() {  

        var self = this;            
        $.post('/add/line', {customer_id : customer_id } , function(data) {                 
            var line = ko.toJS(data);               
            self.lines.push(new Line(line.id, line.name, line.equipType, line.model, line.height, line.length, line.loadsWeek, line.weight, line.width, line.pallets));

            $("#lineTabs").tabs({ selected: 0 });
            $(".palletTabs").tabs({ selected: 0 });             
        });     
    }
};

由于某种原因,当我初始化页面时它工作正常。

$.getJSON("/json/all/lines", { customer_id : customer_id } , function(data) {       

        var mappedData = ko.utils.arrayMap(data, function(line) {               
            return new Line(line.id, line.name, line.equipType, line.model, line.height, line.length, line.loadsWeek, line.weight, line.width, line.pallets)        
        });

        viewModel.lines(mappedData);        

        $("#lineTabs").tabs({ selected: lineTabIndex });
        $(".palletTabs").tabs({ selected: 0 });

    }); 

我在这里缺少什么吗?感谢您的任何帮助。

From what I have done in past(before using knockoutjs) I would call the tabs() function anytime I updated the view on a page. I'm trying to do this on a knockoutjs page I have done. Everytime I change the observable array it de-initializes the jquery tabs. That's fine I'll just call the function. This doesn't seem to do anything.

    var viewModel = {       

    lines: ko.observableArray([]),      
    activeTab: ko.observable(),         
    addLine : function() {  

        var self = this;            
        $.post('/add/line', {customer_id : customer_id } , function(data) {                 
            var line = ko.toJS(data);               
            self.lines.push(new Line(line.id, line.name, line.equipType, line.model, line.height, line.length, line.loadsWeek, line.weight, line.width, line.pallets));

            $("#lineTabs").tabs({ selected: 0 });
            $(".palletTabs").tabs({ selected: 0 });             
        });     
    }
};

For some reason it works fine when I'm initializing the page.

$.getJSON("/json/all/lines", { customer_id : customer_id } , function(data) {       

        var mappedData = ko.utils.arrayMap(data, function(line) {               
            return new Line(line.id, line.name, line.equipType, line.model, line.height, line.length, line.loadsWeek, line.weight, line.width, line.pallets)        
        });

        viewModel.lines(mappedData);        

        $("#lineTabs").tabs({ selected: lineTabIndex });
        $(".palletTabs").tabs({ selected: 0 });

    }); 

Is there something I'm missing here? Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2024-12-21 11:51:13

jQuery UI 选项卡的路线图表明,他们将添加一个刷新方法,这将使这变得更容易。

当我将 KO 与 jQuery UI 选项卡结合起来时,只要 observableArray 发生更改,我就会销毁并重新初始化选项卡。

我使用自定义绑定来执行此操作,如下所示:

ko.bindingHandlers.jqTabs = {
    update: function(element, valueAccessor, allBindingsAccessor) {
       var dependency = ko.utils.unwrapObservable(valueAccessor()),
           currentIndex = $(element).tabs("option", "selected") || 0,
           config = ko.utils.unwrapObservable(allBindingsAccessor().jqTabOptions) || {};

        //make sure that elements are set from template before calling tabs API
        setTimeout(function() { 
            $(element).tabs("destroy").tabs(config).tabs("option", "selected", currentIndex);
       }, 0);
    }
};

当您使用绑定时,您会将 observableArray 传递给它。或者,您可以在 jqTabOptions 附加绑定中传递选项卡的配置选项。绑定确实会尝试将所选索引重置回重新创建索引时开始的位置。根据您的代码,您可能不想执行此部分。

这是一个示例:
http://jsfiddle.net/rniemeyer/rcqT4/

The roadmap for jQuery UI tabs shows that they are going to add a refresh method that would make this easier.

When I have combined KO with jQuery UI tabs, I have relied on destroying and reinitializing the tabs whenever my observableArray changes.

I use a custom binding to do this like:

ko.bindingHandlers.jqTabs = {
    update: function(element, valueAccessor, allBindingsAccessor) {
       var dependency = ko.utils.unwrapObservable(valueAccessor()),
           currentIndex = $(element).tabs("option", "selected") || 0,
           config = ko.utils.unwrapObservable(allBindingsAccessor().jqTabOptions) || {};

        //make sure that elements are set from template before calling tabs API
        setTimeout(function() { 
            $(element).tabs("destroy").tabs(config).tabs("option", "selected", currentIndex);
       }, 0);
    }
};

When you use the binding, you would pass your observableArray to it. Optionally, you can pass config options for the tabs in the jqTabOptions additional binding. The binding does try to reset the selected index back to where it started when recreating it. Based on your code, you might not want to do this part.

Here is a sample:
http://jsfiddle.net/rniemeyer/rcqT4/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文