darrachequesne / spring-data-jpa-datatables

Spring Data JPA extension to work with the great jQuery plugin DataTables (https://datatables.net/)
Apache License 2.0
450 stars 172 forks source link

ReDraw/Update single row #124

Closed sebasira closed 4 years ago

sebasira commented 4 years ago

Hi! Could you guide me on how can I redraw a single row instead of the whole table?

Suppose I have a button in the table that toggles the active estate of the element in that row (there's an active attribute in the object used in the datatable and via AJAX I set it to true or false. Right now in the AJAX response I'm re-drawing the whole table.

For some applications that's fine but now I would like to only refresh/redraw the row in question.

I've read this and this, but I do not figure out how I should return the row from the AJAX response.

Should I use a DataTablesOutput object? Thank you very much!

If you need to, assume the following object to be displayed in the datatable:

@Entity
@Table(name = "users")
@Data
public class User {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "id")
    @JsonView(DataTablesOutput.View.class)
    private long id;

    @Column(name = "active")
    @JsonView(DataTablesOutput.View.class)
    private boolean active;

    @Column(name = "name")
    @JsonView(DataTablesOutput.View.class)
    private String name;
}
sebasira commented 4 years ago

I think what I would need is a @Repository method to get a DataTablesOutput for a single item, like a findOne instead of a findAll.

As a workaround I will try using Specification that will give me a single element, the one I want. I'll keep you posted

sebasira commented 4 years ago

Well, I manage to get what I wanted: return the newly edited row from the server as a DataTablesOutput... but it was all in vain 😢

Further reading make me realize that as I'm using server-side processing every-time I call the draw() function it will re-draw the whole table, or just the page I'm in if I'd like to, but there's no way to only draw the new row.

So what I wanted was to avoid the ajax call to re draw the whole table but it can not be done if server side processing is enabled.