what causes the remove function on my code to not work?

what causes the remove function on my code to not work?

odydaharyodydahary Posts: 19Questions: 3Answers: 0

hello all,
please help me, i'm still a beginner and i'm from indonesia.
Why is my remove button not working properly? what is wrong with my coding?

this my java

var editor; // use a global for the submit and return data rendering in the examples
 
    $(document).ready(function() {
        editor = new $.fn.dataTable.Editor( {
            ajax: "../control/controllers/joinSelf.php",
            table: "#example",
            fields: [ {
                    label: "Nama Siswa:",
                    name: "n.id_siswa",
                    type: "select"
                }, {
                    label: "Mata Pelajaran:",
                    name: "n.id_mapel",
                    type: "select"
                }, {
                    label: "Tahun Ajaran:",
                    name: "n.id_ta",
                    type: "select"
                }, {
                    label: "PH1",
                    name: "n.ph1"
                }, {
                    label: "PH2",
                    name: "n.ph2"
                }, {
                    label: "PH3",
                    name: "n.ph3"
                }, {
                    label: "PH4",
                    name: "n.ph4"
                }, {
                    label: "PH5",
                    name: "n.ph5"
                }, {
                    label: "PH6",
                    name: "n.ph6"
                }, {
                    label: "PH7",
                    name: "n.ph7"
                }, {
                    label: "PH8",
                    name: "n.ph8"
                }
            ],
            i18n: { //fungsi mengubah nama dan isi modal tambah data
                create: {
                    button: "Simpan",
                    title:  "Tambah Mata Pelajaran",
                    submit: "Simpan"
                },
                remove: {
                    button: "Hapus",
                    title:  "Hapus Mata Pelajaran",
                    submit: "Hapus",
                    confirm: {
                        _: "Yakin Ingin Menghapus %d Mata Pelajaran?",
                        1: "Yakin Ingin Menghapus %d Mata Pelajaran ?"
                    }
                },
                error: {
                    system: "Gagal"
                }
            }
        } );
        $('#example').on( 'click', 'tbody td:not(:first-child)', function () {
            editor.inline( this );
        } );
        $('#example').DataTable( {
            dom: "Bftrip",
            ajax: "../control/controllers/joinSelf.php",
            columns: [
                {
                    data: null,
                    defaultContent: '',
                    className: 'select-checkbox',
                    orderable: false
                },
                { data: "s.nama_siswa"},
                { data: null,
                    render: function(data, type, row){
                        return row.k.nama_kelas+"-"+row.p.nama_pk+" "+row.j.nama_jurusan;
                    }
                },
                { data: "a.nama_ta" },
                { data: "m.nama_mapel"},
                { data: "n.ph1"},
                { data: "n.ph2"},
                { data: "n.ph3"},
                { data: "n.ph4"},
                { data: "n.ph5"},
                { data: "n.ph6"},
                { data: "n.ph7"},
                { data: "n.ph8"},
                { data: "n.rph"},
                { data: "n.hts"},
                { data: "n.has"},
                { data: "n.hpa" },
                { data: "n.predikat" },
                { data: "n.des_ph" },
                // {
                //     data: "tbl_mapel.nama_mapel",
                //     render: function ( val, type, row ) {
                //         return val.first_name ?
                //             val.first_name +' '+ val.last_name :
                //             '';
                //     },
                //     defaultContent: ""
                // }
            ],
            select: {//css select row ketika diklik
                style:    'os',
                selector: 'td:first-child',
                blurable: true
            },
            buttons: [
                { extend: "create", editor: editor },
                { extend: "remove", editor: editor }
            ]
        } );
    } );

and this my serverscript

<?php

// 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;


/*
 * Example PHP implementation used for the joinSelf.html example - the basic idea
 * here is that the join performed is simply to get extra information about the
 * 'manager' (in this case, the name of the manager). To alter the manager for
 * a user, you would change the 'manager' value in the 'users' table, so the
 * information from the join is read-only.
 */
Editor::inst( $db, 'tbl_nilai n','id_nilai')
    ->debug(true)
    ->field( 
        Field::inst( 'n.id_siswa' )
            ->options( Options::inst()
                ->table('tbl_siswa')
                ->value('id_siswa')
                ->label('nama_siswa')

            ),
        Field::inst('s.nama_siswa'),
        Field::inst( 's.id_pk' )//relasi antara tbl_siswa dan tbl_ploting
            ->options( Options::inst()
                ->table('tbl_ploting')
                ->value('id_pk')
                ->label('nama_pk')

            ),
        Field::inst( 'p.nama_pk' ),
        Field::inst( 'p.id_jurusan' )//relasi antara tbl_ploting dengan tbl_jurusan
            ->options( Options::inst()
                ->table('tbl_jurusan')
                ->value('id_jurusan')
                ->label('nama_jurusan')

            ),
        Field::inst( 'j.nama_jurusan' ),
        Field::inst( 'p.id_kelas' )//relasi antara tbl_ploting dan tbl kelas
            ->options( Options::inst()
                ->table('tbl_kelas')
                ->value('id_kelas')
                ->label('nama_kelas')

            ),
        Field::inst( 'k.nama_kelas' ),
        Field::inst( 'n.id_mapel' )
            ->options( Options::inst()
                ->table( 'tbl_mapel' )
                ->value( 'id_mapel' )
                ->label('nama_mapel')
            ),
        Field::inst( 'm.nama_mapel' ),
        Field::inst( 'n.id_ta' )
            ->options( Options::inst()
                ->table( 'thn_akademik' )
                ->value( 'id_ta' )
                ->label('nama_ta')
            ),
        Field::inst( 'a.nama_ta' ),
        Field::inst( 'n.ph1' ),
        Field::inst( 'n.ph2' ),
        Field::inst( 'n.ph3' ),
        Field::inst( 'n.ph4' ),
        Field::inst( 'n.ph5' ),
        Field::inst( 'n.ph6' ),
        Field::inst( 'n.ph7' ),
        Field::inst( 'n.ph8' ),
        Field::inst( 'n.rph' ),
        Field::inst( 'n.hts' ),
        Field::inst( 'n.has' ),
        Field::inst( 'n.hpa' ),
        Field::inst( 'n.predikat' ),
        Field::inst( 'n.des_ph' )
    )
    ->leftJoin( 'tbl_siswa s', 's.id_siswa', '=', 'n.id_siswa' )
    ->leftJoin( 'tbl_mapel m', 'm.id_mapel', '=', 'n.id_mapel' )
    ->leftJoin( 'thn_akademik a', 'a.id_ta', '=', 'n.id_ta' )
    ->leftJoin( 'tbl_ploting p', 'p.id_pk', '=', 's.id_pk' )
    ->leftJoin( 'tbl_kelas k', 'k.id_kelas', '=', 'p.id_kelas' )
    ->leftJoin( 'tbl_jurusan j', 'j.id_jurusan', '=', 'p.id_jurusan' )
    ->process($_POST)
    ->json();

after the button is clicked the results will be like in this picture

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,378Questions: 26Answers: 4,781

    Is the row removed from your database?

    Looks like you are returning an incorrect response for remove. This doc discusses the expected response:
    https://editor.datatables.net/manual/server#Remove

    Kevin

  • odydaharyodydahary Posts: 19Questions: 3Answers: 0

    the response is the same as the example
    but why isn't my database deleted?

  • colincolin Posts: 15,163Questions: 1Answers: 2,588

    Hi @odydahary ,

    Could you post here the response that you're seeing, please. Also, you didn't answer Kevin's question on whether the row is actually removed in the DB or nor? If you refresh the page, does the row return? And are you seeing console errors or server-side errors?

    Also, does the create button work OK?

    Cheers,

    Colin

  • odydaharyodydahary Posts: 19Questions: 3Answers: 0

    Sorry, I failed to focus because of this problem.
    yes, when the page refreshes the data that I delete appears again.
    the create and edit buttons function normally.
    when I delete data, nothing happens in the console.
    My database structure is as follows:
    according to the code above

  • odydaharyodydahary Posts: 19Questions: 3Answers: 0

    Am I wrong using leftjoin?

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin
    Answer ✓

    Could you change:

    ->process($_POST)
    

    to be:

    ->debug(true)
    ->process($_POST)
    

    please? Then show me the JSON that is returned from the server when you do a delete action in the browser.

    I suspect the issue might be the use of a table name alias for your main table: 'tbl_nilai n'. If you just use `'tbl_nilai' (and update the field names accordingly) does it then work?

    Thanks,
    Allan

  • odydaharyodydahary Posts: 19Questions: 3Answers: 0

    successful, yes yes yes I am happy....
    It turns out that the "alias" that makes all of that doesn't work.

    originally:

    Editor::inst( $db, 'tbl_nilai n','id_nilai')

    I changed it:

    Editor::inst( $db, 'tbl_nilai','id_nilai')
    thank you @allan , thank you all

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    Thanks - I'll look into resolving that in the PHP libraries (I recently did so in .NET).

    Allan

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    This should do it - although not all databases support DELETE FROM with an alias...

    Allan

  • odydaharyodydahary Posts: 19Questions: 3Answers: 0

    @allan

    is it for alias use? if yes then the result:

    there was an error in my code editor.

    at first from your github

    if (
        $field->set() !== Field::SET_NONE &&
        $this->_part( $fieldName, 'table' ) === $table
        $this->_part( $fieldName, 'table' ) === $tableMatch
    ) {
        $count++;
    }
    

    maybe due to lack of operator || or && ,

    if (
        $field->set() !== Field::SET_NONE &&
        $this->_part( $fieldName, 'table' ) === $table ||
        $this->_part( $fieldName, 'table' ) === $tableMatch
    ) {
        $count++;
    }
    

    if without this alias works.

    but I tried operator || with an generates the following error:

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin
    Answer ✓

    That's not the code that is in the repo. I replaced:

    $this->_part( $fieldName, 'table' ) === $table
    

    with:

    $this->_part( $fieldName, 'table' ) === $tableMatch
    

    This is the actual file. What I linked to before was the diff.

    Allan

  • odydaharyodydahary Posts: 19Questions: 3Answers: 0

    I think my MySQL doesn't support alias

    I tried running sql on phpmyadmin apparently alias made a mistake

    i try sql DELETE FROM tbl_nilai n WHERE (`n`.`id_nilai` = 5 )

This discussion has been closed.