Field number type not correct in postEdit event

Field number type not correct in postEdit event

altrackaaltracka Posts: 9Questions: 6Answers: 0

I have several number fields. I've checked that they are indeed numeric when I add the data to the datatable. The Editor form recognizes the numeric types based on fields.attr.type, because it displays a spinner in the edit box, and the spinner does properly increment/decrement the number.

But in the postEdit event, the types are all strings. I could convert them to numbers myself, but shouldn't have to. What am I missing?

    var editor = new $.fn.dataTable.Editor({
      table: containerId,
      idSrc: 'transaction_id',
      fields: [
        { label: 'Transaction ID', name: 'transaction_id', attr: {type: "number"}},
        { label: 'Contribution', name: 'contribution', attr: { type: "number" }},
        { label: 'Non-Equity Return', name: 'nonequity_return', attr: { type: "number" }},
        { label: 'Equity Distribution', name: 'equity_distribution', attr: { type: "number" }},
        { label: 'Reinvestment', name: 'reinvestment', attr: { type: "number" }},
      ],
    });

    editor.on('postEdit', function (e, json, data) {
      data['transaction_id'] // is a string
      data['contribution'] // is a string
      // etc
      submitTransaction(data)
    });

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    The key thing here is that Editor reads the value from the input element. That means it is always a string, regarding of the input type attribute.

    With Editor 2 I introduced formatters to handle situations such as this - specifically in this case fields.getFormatter would be used:

    {
      attr: { type: "number" },
      getFormatter: function(val) {
        return parseInt(val, 10);
      },
      label: 'Reinvestment',
      name: 'reinvestment',
    },
    

    It is, without question a real annoyance that the field types don't support typed data - but that is an unfortunate side effect of using the DOM to store and edit the value in the input field.

    Allan

Sign In or Register to comment.