Datatable wont edit the values in row

Datatable wont edit the values in row

alexisgarci11alexisgarci11 Posts: 7Questions: 2Answers: 0

Hi guys, i have installed and loaded all the files(at least i think) and the datatable is displaying ok but when i try to edit a value in row or in the edit window it wont give me any errors but wont update it on the database, so i edit the value, press ENTER and nothing, the value returns to its original. I get no JS errors and the XHR comes alright. Anyone can help? thanks

    <?php

    $mysqli = new mysqli("127.0.0.1", "root", "", "datatables_demo");

    // Check connection
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        exit();
    }

    /*
     * Example PHP implementation used for the index.html example
     */

    // DataTables PHP library
    include("lib/DataTables.php");

    // Alias Editor classes so they are easy to use
    use DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;

    // Build our Editor instance and process the data coming from _POST

    $editor =  Editor::inst($db, 'datatables_demo')
        ->fields(
            Field::inst('first_name')
                ->validator(Validate::notEmpty(ValidateOptions::inst()
                    ->message('A first name is required')
                )),
            Field::inst('last_name')
                ->validator(Validate::notEmpty(ValidateOptions::inst()
                    ->message('A last name is required')
                )),
            Field::inst('position'),
            Field::inst('email')
                ->validator(Validate::email(ValidateOptions::inst()
                    ->message('Please enter an e-mail address')
                )),
            Field::inst('office'),
            Field::inst('extn'),
            Field::inst('age')
                ->validator(Validate::numeric())
                ->setFormatter(Format::ifEmpty(null)),
            Field::inst('salary')
                ->validator(Validate::numeric())
                ->setFormatter(Format::ifEmpty(null)),
            Field::inst('start_date')
                ->validator(Validate::dateFormat('Y-m-d'))
                ->getFormatter(Format::dateSqlToFormat('Y-m-d'))
                ->setFormatter(Format::dateFormatToSql('Y-m-d'))
        )
        /*->debug(true)*/
       /* ->process($_POST)*/
        ->json();
    ;?>


    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel="stylesheet" href="jquery.dataTables.min.css"/>
        <link rel="stylesheet" href="buttons.dataTables.min.css"/>
        <link rel="stylesheet" href="select.dataTables.min.css"/>
        <link rel="stylesheet" href="dataTables.dateTime.min.css"/>
        <link rel="stylesheet" href="editor.dataTables.min.css"/>
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jqc-1.12.4/dt-1.10.24/b-1.7.0/sl-1.3.3/datatables.min.css"/>
        <link rel="stylesheet" type="text/css" href="Editor-2.0.1/css/editor.dataTables.css">

        <script src="jquery-3.5.1.js"></script>
        <script src="jquery.dataTables.min.js"></script>
        <script src="dataTables.buttons.min.js"></script>
        <script src="dataTables.select.min.js"></script>




    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/jqc-1.12.4/dt-1.10.24/b-1.7.0/sl-1.3.3/datatables.min.js"></script>
    <script type="text/javascript" src="Editor-2.0.1/js/dataTables.editor.js"></script>





</head>
<body class="nav-toggle">

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
    <tr>
        <th></th>
        <th>First name</th>
        <th>Last name</th>
        <th>Position</th>
        <th>Office</th>
        <th width="18%">Start date</th>
        <th>Salary</th>
    </tr>
    </thead>
</table>
<script>
    var editor; // use a global for the submit and return data rendering in the examples

    $(document).ready(function() {
        editor = new $.fn.dataTable.Editor( {
            ajax: "staff.php",
            table: "#example",
            fields: [ {
                label: "First name:",
                name: "first_name"
            }, {
                label: "Last name:",
                name: "last_name"
            }, {
                label: "Position:",
                name: "position"
            }, {
                label: "Office:",
                name: "office"
            }, {
                label: "Extension:",
                name: "extn"
            }, {
                label: "Start date:",
                name: "start_date",
            }, {
                label: "Salary:",
                name: "salary"
            }
            ]
        } );

        // Activate an inline edit on click of a table cell
        $('#example').on( 'click', 'tbody td:not(:first-child)', function (e) {
            editor.inline( this );
        } );

        $('#example').DataTable( {
            dom: "Bfrtip",
            ajax: "staff.php",
            order: [[ 1, 'asc' ]],
            columns: [
                {
                    data: null,
                    defaultContent: '',
                    className: 'select-checkbox',
                    orderable: false
                },
                { data: "first_name" },
                { data: "last_name" },
                { data: "position" },
                { data: "office" },
                { data: "start_date" },
                { data: "salary", render: $.fn.dataTable.render.number( ',', '.', 0, '$' ) }
            ],
            select: {
                style:    'os',
                selector: 'td:first-child'
            },
            buttons: [
                { extend: "create", editor: editor },
                { extend: "edit",   editor: editor },
                { extend: "remove", editor: editor }
            ]
        } );
    } );

</script>
</body>
</html>

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    That looks like it should be behaving. Could you post the XHR to the server, and the response, please, or if possible could you link to the page,

    Colin

  • alexisgarci11alexisgarci11 Posts: 7Questions: 2Answers: 0

    ill leave the link to the files. https://we.tl/t-VP1Son3ezW

  • alexisgarci11alexisgarci11 Posts: 7Questions: 2Answers: 0

    headers
    data[row_5][first_name]: Airisd
    action: edit

    Response
    `{"data":[{"id":"1","first_name":"Tiger","last_name":"Nixon","position":"System Architect","email":"t.nixon@datatables.net","office":"Edinburgh","start_date":"2011-04-25 00:00:00","age":"61","salary":"320800","seq":"2","extn":"5421","DT_RowId":"row_1"},{"id":"2","first_name":"Garrett","last_name":"Winters","position":"Accountant","email":"g.winters@datatables.net","office":"Tokyo","start_date":"2011-07-25 00:00:00","age":"63","salary":"170750","seq":"22","extn":"8422","DT_RowId":"row_2"},{"id":"3","first_name":"Ashton","last_name":"Cox","position":"Junior Technical Author","email":"a.cox@datatables.net","office":"San Francisco","start_date":"2009-01-12 00:00:00","age":"66","salary":"86000","seq":"6","extn":"1562","DT_RowId":"row_3"},{"id":"4","first_name":"Cedric","last_name":"Kelly","position":"Senior Javascript Developer","email":"c.kelly@datatables.net","office":"Edinburgh","start_date":"2012-03-29 00:00:00","age":"22","salary":"433060","seq":"41","extn":"6224","DT_RowId":"row_4"},{"id":"5","first_name":"Airi","last_name":"Satou","position":"Accountant","email":"a.satou@datatables.net","office":"Tokyo","start_date":"2008-11-28 00:00:00","age":"33","salary":"162700","seq":"55","extn":"5407","DT_RowId":"row_5"},{"id":"6","first_name":"Brielle","last_name":"Williamson","position":"Integration Specialist","email":"b.williamson@datatables.net","office":"New York","start_date":"2012-12-02 00:00:00","age":"61","salary":"372000","seq":"21","extn":"4804","DT_RowId":"row_6"},{"id":"7","first_name":"Herrod","last_name":"Chandler","position":"Sales Assistant","email":"h.chandler@datatables.net","office":"San Francisco","start_date":"2012-08-06 00:00:00","age":"59","salary":"137500","seq":"46","extn":"9608","DT_RowId":"row_7"},{"id":"8","first_name":"Rhona","last_name":"Davidson","position":"Integration Specialist","email":"r.davidson@datatables.net","office":"Tokyo","start_date":"2010-10-14 00:00:00","age":"55","salary":"327900","seq":"50","extn":"6200","DT_RowId":"row_8"},{"id":"9","first_name":"Colleen","last_name":"Hurst","position":"Javascript Developer","email":"c.hurst@datatables.net","office":"San Francisco","start_date":"2009-09-15 00:00:00","age":"39","salary":"205500","seq":"26","extn":"2360","DT_RowId":"row_9"},{"id":"10","first_name":"Sonya","last_name":"Frost","position":"Software Engineer","email":"s.frost@datatables.net","office":"Edinburgh","start_date":"2008-12-13 00:00:00","age":"23","salary":"103600","seq":"18","extn":"1667","DT_RowId":"row_10"},{"id":"11","first_name":"Jena","last_name":"Gaines","position":"Office Manager","email":"j.gaines@datatables.net","office":"London","start_date":"2008-12-19 00:00:00","age":"30","salary":"90560","seq":"13","extn":"3814","DT_RowId":"row_11"},{"id":"12","first_name":"Quinn","last_name":"Flynn","position":"Support Lead","email":"q.flynn@datatables.net","office":"Edinburgh","start_date":"2013-03-03 00:00:00","age":"22","salary":"342000","seq":"23","extn":"9497","DT_RowId":"row_12"},{"id":"13","first_name":"Charde","last_name":"Marshall","position":"Regional Director","email":"c.marshall@datatables.net","office":"San Francisco","start_date":"2008-10-16 00:00:00","age":"36","salary":"470600","seq":"14","extn":"6741","DT_RowId":"row_13"},{"id":"14","first_name":"Haley","last_name":"Kennedy","position":"Senior Marketing Designer","email":"h.kennedy@datatables.net","office":"London","start_date":"2012-12-18 00:00:00","age":"43","salary":"313500","seq":"12","extn":"3597","DT_RowId":"row_14"},{"id":"15","first_name":"Tatyana","last_name":"Fitzpatrick","position":"Regional Director","email":"t.fitzpatrick@datatables.net","office":"London","start_date":"2010-03-17 00:00:00","age":"19","salary":"385750","seq":"54","extn":"1965","DT_RowId":"row_15"},{"id":"16","first_name":"Michael","last_name":"Silva","position":"Marketing Designer","email":"m.silva@datatables.net","office":"London","start_date":"2012-11-27 00:00:00","age":"66","salary":"198500","seq":"37","extn":"1581","DT_RowId":"row_16"},{"id":"17","first_name":"Paul","last_name":"Byrd","position":"Chief Financial Officer (CFO)","email":"p.byrd@datatables.net","office":"New York","start_date":"2010-06-09 00:00:00","age":"64","salary":"725000","seq":"32","extn":"3059","DT_RowId":"row_17"},{"id":"18","first_name":"Gloria","last_name":"Little","position":"Systems Administrator","email":"g.little@datatables.net","office":"New York","start_date":"2009-04-10 00:00:00","age":"59","salary":"237500","seq":"35","extn":"1721","DT_RowId":"row_18"},{"id":"19","first_name":"Bradley","last_name":"Greer","position":"Software Engineer","email":"b.greer@datatables.net","office":"London","start_date":"2012-10-13 00:00:00","age":"41","salary":"132000","seq":"48","extn":"2558","DT_RowId":"row_19"},{"id":"20","first_name":"Dai","last_name":"Rios","position":"Personnel Lead","email":"d.rios@datatables.net","office":"Edinburgh","start_date":"2012-09-26 00:00:00","age":"35","salary":"217500","seq":"45","extn":"2290","DT_RowId":"row_20"},{"id":"21","first_name":"Jenette","last_name":"Caldwell","position":"Development Lead","email":"j.caldwell@datatables.net","office":"New York","start_date":"2011-09-03 00:00:00","age":"30","salary":"345000","seq":"17","extn":"1937","DT_RowId":"row_21"},{"id":"22","first_name":"Yuri","last_name":"Berry","position":"Chief Marketing Officer (CMO)","email":"y.berry@datatables.net","office":"New York","start_date":"2009-06-25 00:00:00","age":"40","salary":"675000","seq":"57","extn":"6154","DT_RowId":"row_22"},{"id":"23","first_name":"Caesar","last_name":"Vance","position":"Pre-Sales Support","email":"c.vance@datatables.net","office":"New York","start_date":"2011-12-12 00:00:00","age":"21","salary":"106450","seq":"29","extn":"8330","DT_RowId":"row_23"},{"id":"24","first_name":"Doris","last_name":"Wilder","position":"Sales Assistant","email":"d.wilder@datatables.net","office":"Sydney","start_date":"2010-09-20 00:00:00","age":"23","salary":"85600","seq":"56","extn":"3023","DT_RowId":"row_24"},{"id":"25","first_name":"Angelica","last_name":"Ramos","position":"Chief Executive Officer (CEO)","email":"a.ramos@datatables.net","office":"London","start_date":"2009-10-09 00:00:00","age":"47","salary":"1200000","seq":"36","extn":"5797","DT_RowId":"row_25"},{"id":"26","first_name":"Gavin","last_name":"Joyce","position":"Developer","email":"g.joyce@datatables.net","office":"Edinburgh","start_date":"2010-12-22 00:00:00","age":"42","salary":"92575","seq":"5","extn":"8822","DT_RowId":"row_26"},{"id":"27","first_name":"Jennifer","last_name":"Chang","position":"Regional Director","email":"j.chang@datatables.net","office":"Singapore","start_date":"2010-11-14 00:00:00","age":"28","salary":"357650","seq":"51","extn":"9239","DT_RowId":"row_27"},{"id":"28","first_name":"Brenden","last_name":"Wagner","position":"Software Engineer","email":"b.wagner@datatables.net","office":"San Francisco","start_date":"2011-06-07 00:00:00","age":"28","salary":"206850","seq":"20","extn":"1314","DT_RowId":"row_28"},{"id":"29","first_name":"Fiona","last_name":"Green","position":"Chief Operating Officer (COO)","email":"f.green@datatables.net","office":"San Francisco","start_date":"2010-03-11 00:00:00","age":"48","salary":"850000","seq":"7","extn":"2947","DT_RowId":"row_29"},{"id":"30","first_name":"Shou","last_name":"Itou","position":"Regional Marketing","email":"s.itou@datatables.net","office":"Tokyo","start_date":"2011-08-14 00:00:00","age":"20","salary":"163000","seq":"1","extn":"8899","DT_RowId":"row_30"},.......

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    Thanks for the files. Which file am I looking at specifically here? There is a staff.php file but that is just a SELECT from the database. server.php is using the Editor server-side libraries, but it doesn't appear to be getting called anywhere.

    Allan

  • alexisgarci11alexisgarci11 Posts: 7Questions: 2Answers: 0

    Oh sorry i forgot to tell you, i guess everything is in the index.php.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Thanks. You want to split that into two files - one for the HTML and one for the PHP. The HTML one will display the table (and include the needed Javascript, etc). The other file will do the Ajax processing and should contain the Editor::inst(...) code.

    The other point to make is that you don't need to do new mysqli(...) if you have correctly configured the config.php file. Indeed the new mysqli(...) won't work, since our libraries use PDO, not Mysqli.

    I'd suggest trying Generator which can build all of the files that you need and put them into the right places.

    Allan

  • alexisgarci11alexisgarci11 Posts: 7Questions: 2Answers: 0

    that msqli is there but its not to work with the datatables is for other things, i have the config file set up with the database. Ill split it into diferente files and give it a try. thanks dude!

This discussion has been closed.