Datatables not updating after AJAX

Datatables not updating after AJAX

thomsolthomsol Posts: 3Questions: 1Answers: 0

I have the following Editor configuration:
editor = new $.fn.dataTable.Editor({
ajax: {
url: "updatepoints.php",
type: "post",
},
table: "#example2",
idSrc: "points_id",
fields: [
{ label: "Points ID:',
name: "points_id",
type: "hidden"
},
{ label: 'Name: ',
name: "full_name",
type: 'readonly',
},
{ label: 'Points:',
name: 'points'
}
]
});

Update works fine, the return object is:
{ points_id: 30296, full_name: 'Smith, John', points: '1.50'}

After the update, the grid is not updated... No errors are presented

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    Looks like the return object is not what Editor expects. See the Client Server data docs for examples. Basically the updates need to be in an array that is in the data object. I think it should look more like this:

    {
      data: [
        { points_id: 30296, full_name: 'Smith, John', points: '1.50'}
      ]
    }
    

    Kevin

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406
    Answer ✓

    It is even a lot more complex. I don't see DT_RowId in your object either.

    Here is an example of a very simple Editor that uses Editor's front and back end components with PHP. If you used the Editor PHP components you wouldn't have to worry about this by the way. This is all done automatically for you.

    var bondRateEditor = new $.fn.dataTable.Editor( {
        ajax: {
            url: 'actions.php?action=tblBondRate'
        },
        table: "#tblBondRate",
        fields: [ {
                label: lang === 'de' ? 'Wertpapier Typ:' : 'Bond Type:',
                name:  "bond_rate.bond_type",
                type:  "select",
                options: bondTypeOptions
            }, {
                label: lang === 'de' ? 'Restlaufzeit (Jahre):' : 'Duration Remaining (Years):',
                name:  "bond_rate.bond_duration",
                attr: {
                    class: amountMask
                }
            }, {
                label: lang === 'de' ? 'Kursdatum:' : 'Rate Date:',
                name:  "bond_rate.date",
                attr: {
                    class: dateMask
                },
                type:  "datetime",
                def:   function () { return today},
                format: 'L',
                opts:  {
                    showWeekNumber: true,
                    yearRange: 40,
                    momentLocale: momentLocale
                }
            }, {
                label: lang === 'de' ? 'Rendite %:' : 'Return %:',
                name:  "bond_rate.rate"
            }
        ]        
    } );
    
    Editor::inst( $db, 'bond_rate' )
        ->field(
            Field::inst( 'bond_rate.bond_type' ),
            Field::inst( 'bond_rate.bond_duration' )
                ->validator( function ( $val, $data, $opts ) {
                    return validatorAmount($data['bond_rate'], $val);
                } )
                ->getFormatter( function($val, $data, $opts) {
                    return getFormatterAmount($val);
                })
                ->setFormatter( function($val, $data, $opts) {
                    return setFormatterAmount($val);
                }),  
            Field::inst( 'bond_rate.date' )
                ->getFormatter( function ( $val, $data, $opts ) {
                        return getFormatterDate($val);                     
                    } )
                ->setFormatter( function ( $val, $data, $opts ) {
                        return setFormatterDate($val);
                    } ),
            Field::inst( 'bond_rate.rate' )
                ->validator( function ( $val, $data, $opts ) {
                    return validatorRate($data['rate'], $val, true);
                } )
                ->getFormatter( function($val, $data, $opts) {
                    return getFormatterRate($val);
                })
                ->setFormatter( function($val, $data, $opts) {
                    return setFormatterRate($val);
                }),
            Field::inst( 'bond_rate.update_time' )
        )
        ->process($_POST)
        ->json();
    

    And this is the object returned from the server with the PHP Editor instance:

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    DT_RowId isn't required in this case as idSrc is set and is pointing to points_id, which is in the data returned.

    The issue is, as Kevin says, that the response is not wrapped in a data array. Editor is a multi-row editor, hence the need for an array, and we put it in a data property to allow for other properties to also be returned such as error information (e.g. from the validators rf1234 shows).

    If your server-side API is already set and can't be changed, then you can use postSubmit to modify the data being returned from the server into something Editor expects. If that is the case, let me know and we'll take it from there.

    Allan

  • thomsolthomsol Posts: 3Questions: 1Answers: 0

    Thanks to all... Inclusion of the DT_RowId in the return set solved my problem. Many thanks!!

  • thomsolthomsol Posts: 3Questions: 1Answers: 0

    Fuller explanation: What works, which I'm now returning from PHP is:
    $dret = ['DT_RowId' => 30296, points_id = 30296, full_name: 'Smith, John', points: '1.50'];
    return json_encode(['data' => [$dret]]);

Sign In or Register to comment.