singlequote / Laravel-datatables

This repo contains a Datatable that can render a filterable and sortable table. It aims to be very lightweight and easy to use. It has support for retrieving data asynchronously, pagination and recursive searching in relations.
https://singlequote.github.io/Laravel-datatables/
MIT License
18 stars 6 forks source link

New addition of triggers prevents user defined createdRow from working #48

Closed happymacarts closed 2 years ago

happymacarts commented 2 years ago

With the new ability to add triggers we have lost the ability to define custom createdRow events for example in my table model i used to be able to define

public $createdRow = "function(row, data, dataIndex ) {
    $(row).data('id', data.id);
    if(data.status_id == 6){
        $(row).data('deleted', true);
        $(row).addClass('table-danger');
    }
     console.log(data)
}";

which would add a data-attribute to each row ( i know this example is basic but i use it to create custom interactions based upon the data properties

could the scripts.blade.php file be modified to append instead of replace?

wimurk commented 2 years ago

Hi @happymacarts,

Every file in the vendor get's replaced when a new update is available. This isn't something i can change because of the way composer works. Also i suggest not editing vendor files.

For your issue, you can add data attributes with data and conditions. For example


Label::make('id')->condition('status_id === 6')->data(['deleted' => true]), //when deleted
Label::make('id')->condition('status_id !== 6')->data(['deleted' => false]), //when not deleted

Or add the status to the column by doing:

Label::make('id')->class('status-{status_id}'), //gives something like status-6
happymacarts commented 2 years ago

How would i add a row class? i want to add the "table-danger" class to rows that have a status = deleted (6)

happymacarts commented 2 years ago

Looks like i can just do this

$(document).on('dtrow:render', (event, row, data, table) => {
        $(row).data('id', data.id);
        $(row).data('data', data);
        if( data.status_id == 6){
            $(row).addClass('table-danger');
            $(row).data('deleted', true);
        }
    });
wimurk commented 2 years ago

Yes, you can use the triggers to chekc the data of each row. https://singlequote.github.io/Laravel-datatables/table-models#table-triggers