How to user preSubmit function?

How to user preSubmit function?

YudsTableYudsTable Posts: 6Questions: 3Answers: 0

I have matter with the preSubmit what can I do with it, I wanna to set the data after deal for submit. such as
data:{ 'id':id, 'name',name } but it doesn't work, what should I do?(It's my graduate design,please help me)
Thank you very much!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    I'm afraid I don't understand your question. Can you link to a page showing the issue and show your full code.

    Allan

  • YudsTableYudsTable Posts: 6Questions: 3Answers: 0

    ok, I means do you have an example of the preSubmit function or which way can I use to modify the data submit to serverside where the default data like action=create&data[0][id]=11&data[0][name]=aa but what I want is id=1&name=aa
    Thank you, Allan

  • loloskiloloski Posts: 46Questions: 5Answers: 1

    I believed the reason you want to do this because you don't use PHP or .NET libraries accompany with datatables?

    I suggest to use the library accompany with datatables or transform the ajax request to POST and use JSON.stringify to send the request as JSON string then on the server side decode json string to form an associative array and create a parser from there.

    I hope you get what I mean

  • loloskiloloski Posts: 46Questions: 5Answers: 1
    edited March 2016 Answer āœ“
    editor = new $.fn.dataTable.Editor( {
            ajax: {
                create: {
                    type: 'POST',
                    url:  '/your_create_script_path.php',
                    contentType: 'application/json',
                            data: function ( data ) {
                            return JSON.stringify( data );
                    }
                },
                edit: {
                    type: 'POST',
                    url:  '/your_edit_script_path.php',
                    contentType: 'application/json',
                            data: function ( data ) {
                            return JSON.stringify( data );
                    }
                },
                remove: {
                    type: 'DELETE',
                    url:  '.
                }
    

    On the server side

    $inputJSON = file_get_contents('php://input');
    $input= json_decode( $inputJSON, TRUE ); //convert JSON into array

    print_r($input); //walk through the decoded json string and create parser from there. I walk down this path as well to easily parse the data sent by datatables to your preferred scripting language.

  • YudsTableYudsTable Posts: 6Questions: 3Answers: 0

    OK Thanks your help I got it for use the JSON data ,and I use Java didn't familiar with php and .net so Iā€˜m in trouble.Thanks your example again,
    By the way,Can I change the submit success function, the default action looks like just modify in the table, but doesn't through the ajax.reload() to reload the table, does this ajax: { type: 'POST', url: '${pageContext.request.contextPath}/index/add', data: function( data ) { return JSON.stringify(data); }, contentType: 'application/json', success:function(){ dtable.ajax.reload(); } work?
    and must I return the default data format?

  • loloskiloloski Posts: 46Questions: 5Answers: 1
    edited March 2016

    I don't really understand your question but from the looks of it this is what you need

    editor.on('submitSuccess',function() { 
         dtable.ajax.reload();
    });
    

    please siff through the documentation it was clear an concise promise, one of the strength of datatables is very good documentation.

  • YudsTableYudsTable Posts: 6Questions: 3Answers: 0

    eh,Now I don't have enough time to read the documentation,and my English is poor(I'm a senior of china,I'm doing my final design).
    Above all thank you very much, you are friendly!
    Have a good day!

  • loloskiloloski Posts: 46Questions: 5Answers: 1

    sure thing no worries :), i'm not a native english speaker either hehe

  • e8416o8e8416o8 Posts: 25Questions: 9Answers: 1

    loloski ,
    I used your code snippet and create & edit servlets are executed when create & edit button is clicked.
    Before the servlet is called, I can see JSON data but inside of servlet, I am getting nullpointer exception.

    Any suggestions?

    ```
    ajax: {

            create: {
                type: 'POST',
                url:  '/myPayers/AddData',
                contentType: 'application/json',
                data: function ( data ) {
                        console.log("Create-data="+JSON.stringify( data ));
                        return 'json='+JSON.stringify( data );
                },
                success: function(data) {
    
                },
                error: function(data) {
                    alert('fail');
                }
            },
    

    ...

        // this parses the json
        JSONObject jObj = null;
        try {
            String jsonStr = request.getParameter("json");
            System.out.println("jsonStr = "+jsonStr);
            jObj = new JSONObject(request.getParameter("json"));  <--HERE -NullPointerException 
            System.out.println(jObj.toString());
    
        } catch (JSONException e1) {
    

    ...

This discussion has been closed.