找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 455|回復: 1

PHP 事件

[複製鏈接]

257

主題

38

回帖

1138

積分

管理員

積分
1138
發表於 2023-5-25 11:09:11 | 顯示全部樓層 |閱讀模式

Events

When working with complex data, it can often be useful to perform some additional action when Editor updates the database based on information from the client-side. Consider for example the following use case:

  • Modifying the Editor instance or form fields based on that action being taken
  • Deleting files from the file system when rows are removed
  • Logging information to a database about an action that a user has taken.

to name just a few!

To make this possible, the PHP Editor class provides an event listener / callback option similar to the events that the client-side Javascript will trigger.

Listening for events

Events listeners can be added using the Editor->on() method which takes two arguments:

  1. The event name to listen for
  2. A callback function which defines the action to take. The arguments passed to the callback are event dependent. The return value for the pre* named events can be used to cancel actions (see below), while the other event handlers to not use any returned value.

Multiple listeners for the same event can be added simply by calling the Editor->on() method multiple times. The events will be executed in the same sequence that they were added.

Available events

The Editor class will trigger the following events - the arguments passed into the function are also documented:

  • preGet (cancellable) - Triggered immediately prior to reading row data from the database (since 1.6)
    1. $editor - The Editor instance that triggered the event
    2. $id - Row id being selected if only a single row is to be selected (i.e. after an edit). Otherwise this value will be null indicating that all rows will be selected.
  • postGet - Triggered immediately after row information has been read from the database (since 1.6)
    1. $editor - The Editor instance that triggered the event
    2. $data - Data array of rows that have been read from the database
    3. $id - Row id being selected if only a single row is to be selected (i.e. after an edit). Otherwise this value will be null indicating that all rows will be selected.
  • preCreate (cancellable) - Triggered immediately prior to creating a new row
    1. $editor - The Editor instance that triggered the event
    2. $values - The values submitted by the client-side
  • writeCreate - Data has been written to the database, but not yet read back, allowing the database to be updated before the data is gathered to display in the table (since 1.6.2)
    1. $editor - The Editor instance that triggered the event
    2. $id - ID of the row that was created
    3. $values - The values submitted by the client-side
  • postCreate - Triggered immediately after a new row has been created
    1. $editor - The Editor instance that triggered the event
    2. $id - ID of the newly created row
    3. $values - The values submitted by the client-side
    4. $row - The newly created row's data, as read from the database
  • preEdit (cancellable) - Triggered immediately prior to updating an existing row
    1. $editor - The Editor instance that triggered the event
    2. $id - ID of the row to be edited
    3. $values - The values submitted by the client-side
  • writeEdit - Data has been written to the database, but not yet read back, allowing the database to be updated before the data is gathered to display in the table (since 1.6.2)
    1. $editor - The Editor instance that triggered the event
    2. $id - ID of the row to be edited
    3. $values - The values submitted by the client-side
  • postEdit - Triggered immediately after an existing row has been updated
    1. $editor - The Editor instance that triggered the event
    2. $id - ID of the row that has been edited
    3. $values - The values submitted by the client-side
    4. $row - The updated row's data, as read from the database
  • preRemove (cancellable) - Triggered immediately prior to deleting an existing row
    1. $editor - The Editor instance that triggered the event
    2. $id - ID of the row to be deleted
    3. $values - The values submitted by the client-side (i.e. the row's data set)
  • postRemove - Triggered immediately after a row has been deleted
    1. $editor - The Editor instance that triggered the event
    2. $id - ID of the row that has been deleted
    3. $values - The values submitted by the client-side (i.e. the row's data set)
  • preUpload (cancellable) - Triggered immediately prior to a file upload being processed (since 1.6.2)
    1. $editor - The Editor instance that triggered the event
    2. $data - Data submitted by the upload form
  • postUpload - Triggered after a file has been uploaded and information about it read from the database (since 1.6.2)
    1. $editor - The Editor instance that triggered the event
    2. $id - ID of the database record for the uploaded file (or the file's unique name if a database record is not used)
    3. $files - The file information that has been read from the database
    4. $data - Data submitted by the upload form

Please note that for multi-row creation, editing and deletion, the events are triggered once for each row.


257

主題

38

回帖

1138

積分

管理員

積分
1138
 樓主| 發表於 2023-5-25 11:09:25 | 顯示全部樓層

活動

在處理複雜數據時,當Editor根據客戶端的信息更新數據庫時,執行一些額外操作通常很有用。考慮以下用例:

  • 根據正在執行的操作修改編輯器實例或表單字段
  • 刪除行時從文件系統中刪除文件
  • 將信息記錄到數據庫,以了解用戶已執行的操作。

僅舉幾例!

為了實現這一點,PHP Editor類提供了一個事件監聽器/回調選項,類似於客戶端Javascript將觸發的事件

聽取事件

可以使用帶有Editor->on()兩個參數方法添加事件偵聽器

  1. 要偵聽的事件名稱
  2. 一個回調函數,定義要採取的操作。傳遞給回調的參數是依賴於事件的。pre*命名事件的返回值可用於取消操作(參見下文),而其他事件處理程序不使用任何返回值。

只需Editor->on()多次調用該方法即可添加同一事件的多個偵聽器。事件將按照添加它們的順序執行。

可用活動

Editor課程將觸發下列事件-傳遞給函數也記錄的參數:

  • preGet (可取消) - 在從數據庫中讀取行數據之前立即觸發(自1.6起)
    1. $editor - 觸發事件的Editor實例
    2. $id - 如果只選擇一行(即編輯後),則選擇行ID。否則,此值將null指示將選擇所有行。
  • postGet - 從數據庫中讀取行信息後立即觸發(自1.6起)
    1. $editor - 觸發事件的Editor實例
    2. $data - 已從數據庫中讀取的行的數據數組
    3. $id - 如果只選擇一行(即編輯後),則選擇行ID。否則,此值將null指示將選擇所有行。
  • preCreate (可取消) - 在創建新行之前立即觸發
    1. $editor - 觸發事件的Editor實例
    2. $values - 客戶端提交的值
  • writeCreate - 數據已寫入數據庫,但尚未回讀,允許在收集數據之前更新數據庫以顯示在表中(自1.6.2開始)
    1. $editor - 觸發事件的Editor實例
    2. $id - 已創建行的ID
    3. $values - 客戶端提交的值
  • postCreate - 在創建新行後立即觸發
    1. $editor - 觸發事件的Editor實例
    2. $id - 新創建的行的ID
    3. $values - 客戶端提交的值
    4. $row - 從數據庫中讀取的新創建的行的數據
  • preEdit (可取消) - 在更新現有行之前立即觸發
    1. $editor - 觸發事件的Editor實例
    2. $id - 要編輯的行的ID
    3. $values - 客戶端提交的值
  • writeEdit - 數據已寫入數據庫,但尚未回讀,允許在收集數據之前更新數據庫以顯示在表中(自1.6.2開始)
    1. $editor - 觸發事件的Editor實例
    2. $id - 要編輯的行的ID
    3. $values - 客戶端提交的值
  • postEdit - 在更新現有行後立即觸發
    1. $editor - 觸發事件的Editor實例
    2. $id - 已編輯行的ID
    3. $values - 客戶端提交的值
    4. $row - 從數據庫中讀取的更新行的數據
  • preRemove (可取消) - 在刪除現有行之前立即觸發
    1. $editor - 觸發事件的Editor實例
    2. $id - 要刪除的行的ID
    3. $values - 客戶端提交的值(即行的數據集)
  • postRemove - 刪除行後立即觸發
    1. $editor - 觸發事件的Editor實例
    2. $id - 已刪除行的ID
    3. $values - 客戶端提交的值(即行的數據集)
  • preUpload (可取消) - 在處理文件上傳之前觸發(自1.6.2起)
    1. $editor - 觸發事件的Editor實例
    2. $data - 上傳表單提交的數據
  • postUpload - 在上傳文件後觸發,並從數據庫中讀取有關它的信息(自1.6.2起)
    1. $editor - 觸發事件的Editor實例
    2. $id - 上載文件的數據庫記錄的ID(如果未使用數據庫記錄,則為文件的唯一名稱)
    3. $files - 從數據庫中讀取的文件信息
    4. $data - 上傳表單提交的數據

請注意,對於多行創建,編輯和刪除,每行觸發一次事件。

可取消的事件

pre*編輯器1.6開始,這些事件都是可取消的,允許服務器端進程選擇是否應該處理一行。這可以用作數據驗證的一種形式,禁止用戶執行某些操作(儘管請注意,這不是驗證的替代- 如果取消行的處理,則不會向最終用戶顯示錯誤!)。

false從事件處理程序取消對行返回的處理。還將提交任何其他未提交的行,這些行本身不會被取消,並且會正確處理客戶端表。


您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|DoIT 科技論壇

GMT+8, 2025-6-15 21:40 , Processed in 0.021448 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表