how can i get the form(input) data in datatable?

how can i get the form(input) data in datatable?

noodle_linnoodle_lin Posts: 6Questions: 2Answers: 0

Link to test case:
** code**:

    <script>
        $(document).ready(function () {
            var table = $('#example').DataTable({
                    "ajax": {
                        url: "/admin/data/base.json",
                        dataSrc: ""
                    },
                    "columns": [
                        {title:'first', data: 'first'},
                        {title:'second', data: 'second'},
                        {title:'third', data: 'third'},
                        {title:'fourth', data: 'fourth'}],
                    "columnDefs": [
                        {
                            targets: [0, 2],
                            render: function (data, type, row, meta) {
                                if (type === 'display') {
                                    return '<input type="text" value="' + data + '">';
                                }
                                return data;
                            },
                        }
                    ],
                    destroy: true
                })
            ;

            $('button').click(function () {
                var data = table.$('input, select').serialize();
                alert("The following data would have been submitted to the server: \n\n"+
        data +'...'
                );
            });
        });
    </script>


  <button type="submit">Submit form</button>
  <table id="example" class="display" style="width:100%">
  </table>

Description of problem:

when i submit, it returns

seems that the data is empty...
how can i fix it?

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    You are creating a text input with this:

    return '<input type="text" value="' + data + '">';
    

    But you are trying to retrieve select inputs with this:

    var data = table.$('input, select').serialize();
    

    Try changing to this:

    var data = table.$('input, text').serialize();
    

    Kevin

  • noodle_linnoodle_lin Posts: 6Questions: 2Answers: 0

    @kthorngren remain the same. data is empty.
    is there something to do with how i create the text input or how i post data into datatable?
    because when i change

    var data = table.$('input, text').serialize();

    into

    var data = table.data()

    it returns sth below after submit

  • noodle_linnoodle_lin Posts: 6Questions: 2Answers: 0


    this is the DT

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    It might be worth looking at Editor - as that's designed to easily submit the data to the server.

    Colin

Sign In or Register to comment.