Code optimization

Code optimization

marianidiegomarianidiego Posts: 46Questions: 15Answers: 1

Link to test case:
https://editor.datatables.net/examples/simple/inTableControls.html

I don't know if it is a problem, or if I just don't know how to do it....

But in the above example, I would like to optimize the code:

**Description of problem 1 **:

When I call the editor for example like here:

editor.remove(this.closest('tr'), {
        title: 'Delete record',
        message: 'Are you sure you wish to delete this record?',
        buttons: 'Delete'
    });

Isn't it possible that it automatically loads the default title and button and message?

**Description of problem 1 **:

In the above code, I would like to not put the code I need to add the row change every time...

Is it not possible to use edit datatables.default.js, so that this is inserted automatically?

Or, not possible to create a function that adds a column?

I simply would like to write much less than :

            {
                data: null,
                defaultContent:
                    '<div class="action-buttons">' +
                    '<span class="edit"><i class="fa fa-pencil"></i></span> ' +
                    '<span class="remove"><i class="fa fa-trash"></i></span> ' +
                    '<span class="cancel"></span>' +
                    '</div>',
                className: 'row-edit dt-center',
                orderable: false
            }
    ....


    // Activate an inline edit on click of a table cell
    table.on('click', 'tbody span.edit', function (e) {
        editor.inline(table.cells(this.closest('tr'), '*').nodes(), {
            cancelHtml: '<i class="fa fa-times"></i>',
            cancelTrigger: 'span.cancel',
            submitHtml: '<i class="fa fa-floppy-o"></i>',
            submitTrigger: 'span.edit'
        });
    });

    // Delete row
    table.on('click', 'tbody span.remove', function (e) {
        editor.remove(this.closest('tr'), {
            title: 'Delete record',
            message: 'Are you sure you wish to delete this record?',
            buttons: 'Delete'
        });
    });

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    I'm afraid not - at least not at the moment. The title / message / button elements get reused for each action, so a delete followed by a create needs the various elements to be updated, which is why they need to be specified.

    It is a good point though, and I'll look into how I might provide a way to do a default for each action.

    Regards,
    Allan

  • marianidiegomarianidiego Posts: 46Questions: 15Answers: 1

    Okay, very kind.

    Do you know by any chance de I can put eventListening in datatables.default.js?

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    I don't know what datatables.default.js is I'm afraid. I don't think I publish a file under that name?

    If you can link to a test case, with that file being used, I might be able to answer.

    Allan

Sign In or Register to comment.