المكتبة التقنية ←

تحديث Checkboxes بدون فقد موضعك

English version

A checkbox in a long report should save its change without sending the user back to the top. A reliable implementation needs both a secure callback and deliberate refresh behavior.

Define the update contract

Send the row identifier, requested state, and expected row version to an authorized Ajax callback. On the server, validate the state, enforce access to that specific row using trusted identity, and reject stale versions. Checking the identifier alone is insufficient.

Disable repeated edits while saving. Return explicit success or an actionable failure; restore the old checkbox state when saving fails. Use declarative controls or properly escaped rendering rather than concatenating untrusted values into HTML.

Restore after the region refresh

For a report that emits apexafterrefresh, register restoration before refreshing. This fragment belongs in the successful-save handler; tasks is the region's Static ID:

const y = window.scrollY;
apex.jQuery("#tasks").one("apexafterrefresh", function () {
  requestAnimationFrame(() => window.scrollTo(0, y));
});
apex.region("tasks").refresh();

Serialize saves and refreshes so unrelated refreshes cannot consume this handler. For a scrollable container, capture its scrollTop instead. Reapply status styling after refresh, include text labels alongside colors, and restore keyboard focus where practical.

Editorial correction: the original callback lacked row authorization and concurrency checks. Its scroll functions were not connected to refresh completion, and one global localStorage key could mix unrelated pages.

References: Ajax process API, refresh events, and region refresh.

النسخة العربية

Checkbox داخل تقرير طويل المفروض تحفظ التغيير من غير ما ترجع المستخدم لأول الصفحة. التنفيذ الموثوق محتاج Callback آمنة وسلوك Refresh مقصود.

حدد عقد التحديث

ابعت معرّف السجل، والحالة المطلوبة، ورقم نسخة السجل المتوقعة إلى Ajax Callback محمية بالصلاحيات. على السيرفر، تحقق من الحالة، ومن صلاحية المستخدم على السجل نفسه باستخدام هوية موثوقة، وارفض التعديل لو النسخة قديمة. فحص المعرّف وحده لا يكفي.

امنع التعديل المتكرر أثناء الحفظ. رجّع نجاحًا واضحًا أو خطأ يمكن التعامل معه، وأعد حالة Checkbox القديمة عند الفشل. استخدم Controls تصريحية أو عرضًا بمعالجة Escape صحيحة، بدل تركيب قيم غير موثوقة داخل HTML.

استعد المكان بعد تحديث الـRegion

في تقرير يطلق حدث apexafterrefresh، سجّل استعادة المكان قبل Refresh. الجزء التالي يوضع داخل معالج نجاح الحفظ؛ وtasks هو Static ID للـRegion:

const y = window.scrollY;
apex.jQuery("#tasks").one("apexafterrefresh", function () {
  requestAnimationFrame(() => window.scrollTo(0, y));
});
apex.region("tasks").refresh();

نفّذ عمليات الحفظ والتحديث بالتتابع، حتى لا يستهلك Refresh آخر هذا المعالج. لو التمرير داخل Container، احفظ scrollTop الخاصة به. أعد تطبيق ألوان الحالة بعد التحديث، واستخدم وصفًا نصيًا مع اللون، واستعد تركيز لوحة المفاتيح حيث يمكن.

تصحيح تحريري: Callback الأصلية لم تتحقق من صلاحية السجل أو تعارض التعديلات. ودوال التمرير لم ترتبط بانتهاء التحديث، ومفتاح localStorage عام واحد قد يخلط بين صفحات مختلفة.

المراجع: واجهة Ajax، أحداث التحديث، وتحديث Region.