5

我正在尝试将 tablesorter 与 savesort 小部件一起使用。该表由 knokout 填充。

第一次填充表格时,我得到了正确数量的行,并且它记得排序。然后,我使用 ajax 并绑定到敲除数组,用数据重新填充表。然后数据加倍,但淘汰赛(正确)仅跟踪一半数据。

到目前为止,我已经追踪到当我在此方法中应用保存的排序时:

self.tablesorter = function () {
                        $('table.tablesorterTranslation').trigger("update");
                        setTimeout(function () {
                            var sl = $.tablesorter.storage($('table.tablesorterTranslation'), 'tablesorter-savesort');
                            var sortList = (sl && sl.hasOwnProperty('sortList') && $.isArray(sl.sortList)) ? sl.sortList : '';
                            if (sortList && sortList.length > 0) {
                                // update sort change
                                $('table.tablesorterTranslation').trigger("sorton", [sortList]);
                            }
                        }, 1000);
                        return false;
                    };

此方法由敲除后操作绑定调用。

在调用该方法之前,我为敲除数组中的每个项目都有一个表行,在 postaction 之后,我有两个,一个由敲除跟踪,一个似乎是由 tablesorter 添加的。

这是由于 tablesorter 缓存值,有没有办法清理它?

的HTML:

    <table class="tablesorterTranslation" data-bind="visible: contents().length > 0">
                <colgroup>
                    <col class="referencenumber"/>
                    <col class="title"/>
                </colgroup>
                <thead>
                    <tr>
                        <th class="referencenumber">Ref number</th>
                        <th class="title">Title</th>
                    </tr>
                </thead>
                <tbody data-bind="foreach: contents, postAction: tablesorter(), visible: contents().length > 0">
                    <tr>
                        <td class="referencenumber">
                            <span data-bind="text: contentReferenceNumber"></span>
                        </td>
                        <td class="title">
                            <a data-bind="attr: {href: previewUrl, title: previewTitle}, click: previewpopup" class="preview_translation"><%: AdminResources.Menu_Preview %></a>
                      </td>
                </tr>
            </tbody>
        </table>

重新填充表的 ajax:

self.setContentList = function () {
                        if ($('#LanguageIdNameValuePairs option').length > 0) {
                            self.contents.removeAll();
                            self.loading(true);
                            $.ajax('<%= Url.Action("GetContentList", "TranslateContentMenu") %>',
                                {
                                    dataType: 'json',
                                    data: {
                                        languageId: $('#LanguageIdNameValuePairs').val(),
                                        page: self.page
                                    },
                                    success: function (allData) {
                                        var mappedContent = $.map(allData, function (item) { return new ContentViewModel(item); });
                                        self.loading(false);
                                        self.contents(mappedContent);
                                    }
                                });
                        } else {
                            self.contents(new Array());
                        }
                    };

内容视图模型:

(function (ko) {
    PODIUM.translation.ContentViewModel = function(data) {
        this.contentReferenceId = ko.observable(data.contentReferenceId);
        this.contentName = data.contentName;
        this.previewUrl = ko.observable(data.previewUrl);
        this.previewTitle = ko.observable(data.previewTitle);
        this.publishTitle = ko.observable(data.publishTitle);
        this.contentReferenceNumber = ko.observable(data.contentReferenceNumber);
    };
}(ko));

最后,使用 savesort 填充表并定义 tablesort(我认为 savesort 是问题)。

       $('#OptionsMenu').change(function () {
            viewModel.setContentList();
        });
        $('table.tablesorterTranslation').tablesorter({
            widgets: ["saveSort"],
            widgetOptions: {}
        });

我有一个带有一些值的选择列表,当它发生变化时,我从服务器获取新值并重新填充表。这是它记住旧值的地方,但我想“清理”表格并重新开始。

4

1 回答 1

2

我自己找到了答案,但不是我想要的。

基本上,由于我使用的是淘汰赛,我不应该对视图本身进行更改,而是更改视图模型。所以,我看了一下对数组进行排序,然后让淘汰赛来完成剩下的工作。

我首先删除了 tablesorter 的所有痕迹(除了桌子上的 css 类,因为我已经在样式表中使用了它)。

然后我添加了这个方法:

            self.sortContents = function (element, sortProperty) {
                var elem = $(element);
                var direction = (elem.hasClass('headerSortDown')) ? 'headerSortUp' : 'headerSortDown';
                var selector = $.trim($(element).attr('class').replace('header', '').replace('headerSortUp', '').replace('headerSortDown', ''));
                if (direction == 'headerSortUp') {
                    self.filterStorage.updateSortFilter(selector, sortProperty);
                    self.contents.sort(function(left, right) {
                        return left[sortProperty] == right[sortProperty] ? 0 : (left[sortProperty] > right[sortProperty] ? -1 : 1);
                    });
                    elem.removeClass('headerSortDown');
                    elem.addClass('headerSortUp');
                } else {
                    self.filterStorage.updateSortFilter(selector, sortProperty);
                    self.contents.sort(function (left, right) {
                        return left[sortProperty] == right[sortProperty] ? 0 : (left[sortProperty] < right[sortProperty] ? -1 : 1);
                    });
                    elem.removeClass('headerSortUp');
                    elem.addClass('headerSortDown');
                }
            };

请注意,我使用与 tablesort 插件相同的 css 类,这是因为 tablesorter 已经在应用程序的其他地方使用,并且这些类已经存在 css。

我将表头更改为:

<th class="title header" data-bind="click: function(data, event){ return sortContents($(event.target), 'contentName')}"><%: AdminResources.ContentOverview_Title %></th>

然后,由于我在这个烂摊子中起床,试图保存排序状态,我不得不为此找出一些东西。所以我创建了一个带有 to 方法、更新和获取的新类。这保存了我排序的标题和方向。

function getSortFilter() {
    var cookie = $.cookie(tableSortedByCookie);
    return JSON.parse(cookie);
}
function updateSortFilter(selector, property) {
    $.cookie(tableSortedByCookie, null);
    $.cookie(tableSortedByCookie, JSON.stringify({ selector: selector, property: property }), { path: '/' });
}

您可以在 sortcontents 方法中查看更新的用法。(此方法在包含表格的页面的视图模型上)。

然后我将它添加到 ajax 成功方法的底部:

var tableSortCookie = self.filterStorage.getSortFilter();
if (tableSortCookie != null && tableSortCookie.selector != null) {
   var header = $('.tablesorterTranslation th.' + tableSortCookie.selector);
   self.sortContents(header, tableSortCookie.property);
}

然后,我能够使用敲除对数据进行排序,以及在更改数据和刷新页面时存储状态并加载状态。

于 2013-01-04T12:19:10.583 回答