Directions to create custom button to autofill a value into all rows in a specific column

Directions to create custom button to autofill a value into all rows in a specific column

YoDavishYoDavish Posts: 123Questions: 46Answers: 3

Can someone point in the direction on how to create a custom button that will automatically set a value of "1" to a specific column for every row in the table?

I tried to use this method to get me started but all it does is select the entire table only.

https://datatables.net/forums/discussion/53206/select-all-via-checkbox

Answers

  • kthorngrenkthorngren Posts: 20,140Questions: 26Answers: 4,735

    One option is to use cells().every() to iterate all the cells of a particular column then use cell().data() to update the cell. For example:
    http://live.datatables.net/picabidi/1/edit

    Kevin

  • YoDavishYoDavish Posts: 123Questions: 46Answers: 3
    edited March 2020

    @kthorngren so it shows in the datatables as updated for my column, however, when I look at the actual database table it's not actually saving to it. When I refresh the page, the columns are set to no. If I manually, select the cell to yes it updates into the database table correctly. Here is the entire code below:

    //GLOBAL VARS
    var tab;
    var editor;
    
    //Ready function
    $(document).ready(function() {
        setupDataTables();
        setupDTSearches();
    })
    
    function setupDataTables() {
        editor = new $.fn.dataTable.Editor( {
            table: '#table',
            ajax: "tableServer-returnMail.php",
            fields: [
                {
                    label: "AssignedTo",
                    name: "AssignedTo",
                    type:  "autoComplete",
                    opts: {
                        source: "autoComplete-returnMail.php"
                    }
                },
                {
                    label: "Note",
                    name: "Note",
                },
                {
                    label: "Completed",
                    name: "Completed",
                    type: "select",
                    options: [
                        { label: "No",      value: 0},
                        { label: "Yes",     value: 1}
                    ]
                }
            ]
        } );
    
        tab = $("#table").DataTable({
            ajax: "tableServer-returnMail.php",
            dom: "Bfrtip",
            columns: [
                { data: "AssignedTo" },
                { data: "Note" },
                { data: "Completed" },
                { data: "originalFileDate" },
                { data: "currentFileName" },
                { data: "currentFilePath" },
                { data: "user" },
                { data: "barcode" },
                { data: "companyName" }
            ],
            autoFill: {
                columns: ':nth-child(2),:nth-child(3),:first-child',
                editor:  editor
            },
            keys: {
                columns: ':nth-child(2),:nth-child(3),:first-child',
                editor:  editor
            },
            select: {
                style:    'os',
                selector: 'td:first-child',
                blurable: true
            },
            buttons: [
                'copy', 'csv', 'excel'
            ],
            "initComplete": function () {
                tab.order([3,"desc"]);
                tab.draw();
            },
            "pageLength": 100
        });
    
            $("#completed").on('click', function () {
                ConfirmDialog('Mark all rows as completed?', tab);
          })
    }
    
    function ConfirmDialog(message, tab) {
        $('<div></div>').appendTo('body')
        .html('<div><h6>' + message + '?</h6></div>')
        .dialog({
            modal: true,
            title: 'Confirmation',
            zIndex: 10000,
            autoOpen: true,
            width: 'auto',
            resizable: false,
            buttons: {
            Yes: function() {
                tab.cells(null, 2).every( function () {
                this.data("1");
                });
                tab.draw();
            $(this).dialog("close");
            },
            No: function() {
                $(this).dialog("close");
            }
        },
        close: function(event, ui) {
            $(this).remove();
        }
        });
    };
    
  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    edited March 2020

    Kevin's answer is correct, if you're not using Editor - you forgot to mention that bit! :)

    With Editor, you would need to use edit() to modify each row, or use multiSet() to edit them all in a single operation (though not sure how it would cope if too many were set).

    Colin

  • kthorngrenkthorngren Posts: 20,140Questions: 26Answers: 4,735

    You didn't mention you were using Editor and that you wanted the Database to be updated. You can use the edit() API. Check the docs for using the row selector and examples,.

    Kevin

This discussion has been closed.