Using DataTables render function with standalone editing.

Using DataTables render function with standalone editing.

OpticmountainOpticmountain Posts: 3Questions: 2Answers: 0

Is they a way to implement the above?

For example - below is there a built-in way of rendering 'Expenses' as a currency?

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: "../php/standalone.php",
        fields: [ {
                label: "Expenses:",
                name:  "expenses"
            }
        ]
    } );
 
    $('#edit').on( 'click', function () {
        editor
            .buttons( {
                label: "Save",
                fn: function () { this.submit(); }
            } )
            .edit();
    } );
} );

One workaround is to initialise the data in such a format then manipulate any send data (with preSubmit), and render any return data (with setData):

editor
     .on( 'preSubmit', function ( e, d, action ) {
          if (action == "create" || action == "edit") {
            $.each( d.data, function (key, value) {
              if(typeof d.data[key]['expenses'] != "undefined") {
                 d.data[key]['expenses'] = d.data[row]['expenses'].replace(/\D/g,'');
               }
         });
        }
      })
     .on( 'setData', function ( e, json, data, action ) {
          if (action == "create" || action == "edit") {
             $.each( data, function (key, value) {
               if(key == 'expenses') {
                 data[key] = $.fn.dataTable.render.number( ',', '.', 0, '$' ).display(data[key]);
                   }
         });
        }
      })

This is not ideal however as the editor field is then always poplulated with the actual rendered data rather than the value which is displayed as rendered. This can be quite frustrating for large formatted numbers such as $2,424.00 which in non-standalone mode and with

$.fn.dataTable.render.number( ',', '.', 0, '$' )

would display as 2424 which is easier the edit.

Not sure where to go next (other than to cut down on expenses!)

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,356Questions: 1Answers: 10,444 Site admin
    Answer ✓

    I'm afraid there isn't really a good way to use orthogonal data with a standalone Editor at the moment. I might need to look at adding support for a data-editor-value attribute which would be used to get / set the attribute, and then any rendering is left up to you.

    However, at the moment, the only option is to use the val() method to get the current value, strip out want you don't want and then set the value again. This could be done on initEdit.

    Sorry I don't have a better answer just now - but thanks for posting this. I've added it to the list of future Editor updates.

    Allan

  • OpticmountainOpticmountain Posts: 3Questions: 2Answers: 0

    Hi Allan, thanks for getting back to me so quickly - and for DataTables in general - a fantastic and powerful tool.

    Ahhh, the initEdit event is an excellent work-around for now - and thanks for looking into adding data-editor-value support in the future.

    Many thanks, Tim

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

    Just a quick message in case anyone finds this thread in future. Support for the data-editor-value attribute will be included in Editor 1.6.

    Allan

This discussion has been closed.