How can I retrieve the data of the current row in layui?

In layui, you can use table event listeners to retrieve data from the current row. Here are the specific steps:

To begin with, in the definition of the table, attach an event listener to the operation button of each row, for example:

table.on('tool(test)', function(obj){
  var data = obj.data; // 获取当前行的数据
  var layEvent = obj.event; // 获取当前点击的按钮的lay-event属性值
  // 根据layEvent的值进行相应的操作
});

Next, in the definition of the button, add a lay-event attribute to identify the type of operation when the button is clicked, for example:

<button class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>

In event listening, you can access the data of the current row using obj.data and then perform the necessary actions accordingly. For example, you can use data.id to retrieve the id of the current row and then update or delete the data based on the id.

Note: the ‘test’ in the above code is the value of the table’s lay-filter attribute, which needs to be modified according to the actual situation. Additionally, you can also use layui’s custom events to retrieve the current row data, the method is similar, just replace ‘tool’ with ‘event’.

bannerAds