jQuery TablesorTer يحفظ عنصر واجهة مستخدم
-
22-09-2019 - |
سؤال
لقد وجدت هذا الرمز في الإنترنت:
$.tablesorter.addWidget({
id: "memorizeSortOrder",
format: function(table) {
if (!table.config.widgetMemorizeSortOrder.isBinded) { // only bind if not already binded
table.config.widgetMemorizeSortOrder.isBinded = true;
$("thead th:visible",table).click(function() {
var i = $("thead th:visible",table).index(this);
$.get(table.config.widgetMemorizeSortOrder.url+i+'|'+table.config.headerList[i].order);
});
} // fi
}
});
عثر عليه في: http://www.adspeed.org/2008/10/jquery-extend-tablesorter-plugin.html
أود أن أحفظ فرز جداول Ajax الخاصة بي ، لذا في كل تحديث (يتغير الجدول تمامًا بحيث لا يوجد إلحاق) ، يحافظ على فرزه كما كان.
السؤال هو .. كيف يمكن استخدام هذا؟
$("#tablediv").load(
"table.php",
null,
function (responseText, textStatus, req) {
$("#table").trigger("update");
}
);
ما هي التغييرات التي أحتاجها؟
المحلول
إذا كان الجدول لا يحتوي على صفوف الجسم التي تطبق قائمة فرز ، فقد يصبح الاختبار النهائي:
if(sortList.length > 0 && table.tBodies[0].rows.length)
نصائح أخرى
هنالك البرنامج المساعد آخر هذا يفعل هذا. كما أنه يستخدم ملفات تعريف الارتباط بحيث يتم استمرار الأنواع عبر أحمال الصفحة. يتطلب الإصدار المرتبط بملابس JQuery Cookie و JSON.
لقد قمت بتعديل نسختي لاستخدام البرنامج النصي JSON2 من Crockford بدلاً من المكون الإضافي JQuery JSON.
$(document).ready(function() {
$.tablesorter.addWidget({
// give the widget an id
id: "sortPersist",
// format is called in the on init and when a sorting has finished
format: function(table) {
// Cookie info
var cookieName = 'application_name_tablesorts';
var cookie = $.cookie(cookieName);
var options = {path: '/'};
var data = {};
var sortList = table.config.sortList;
var tableId = $(table).attr('id');
var cookieExists = (typeof(cookie) != "undefined" && cookie != null);
// If the existing sortList isn't empty, set it into the cookie and get out
if (sortList.length > 0) {
if (cookieExists) {
data = JSON.parse(cookie);
}
data[tableId] = sortList;
$.cookie(cookieName, JSON.stringify(data), options);
}
// Otherwise...
else {
if (cookieExists) {
// Get the cookie data
var data = JSON.parse($.cookie(cookieName));
// If it exists
if (typeof(data[tableId]) != "undefined" && data[tableId] != null) {
// Get the list
sortList = data[tableId];
// And finally, if the list is NOT empty, trigger the sort with the new list
if (sortList.length > 0) {
$(table).trigger("sorton", [sortList]);
}
}
}
}
}
});
});
لا تنتمي إلى StackOverflow