How to handle column data type in serverSide processing flow?

How to handle column data type in serverSide processing flow?

YOMYOM Posts: 51Questions: 21Answers: 1
edited March 2022 in Priority support

I have a column in my table that seems to flip from string to number when running an edit. If I edit a single row the column value gets sent to the server as a string, if I edit multiple rows the column value is sent as a number for each row.

I want to be able to make this more consistent and only send numbers because that's how it's stored in our database. I didn't have much luck with getFormatter or setFormatter. Here is an example of what I'm trying to do:

 new Field("some_table.numeric_column")
.getFormatter((val, _) => parseInt(val, 10))
.validator((val, data, _) => isColumnNumeric(data))

isColumnNumeric is wanting the column (that is inside of the row/data object) to be a number- not a string.

P.S: Try not to focus too much on the utility of the isColumnNumeric function, it's just an example of a validator that wants the column to be a number, it isn't our actual implementation

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,449Questions: 1Answers: 10,055 Site admin

    Hi,

    fields.getFormatter is probably what you want here. What is happening is that when a value is read from an input element in the DOM, it must be read back as a string. That then gets sent to the server as such.

    When you multi-row edit, that would only happen if you change the value of that field (otherwise it just uses the existing data, including its type).

    The fields.getFormatter option on the client-side can be used to convert the string to a number when reading from the input.

    Allan

  • YOMYOM Posts: 51Questions: 21Answers: 1

    Hey Allan,

    The edit is being done programmatically by referencing the row nodes to be edited:

    editor
    .edit(selectedRows.nodes(), false)
    .val("field", value)
    .submit();
    

    When doing it like this, it looks like the getFormatter method never gets called on the field

  • allanallan Posts: 61,449Questions: 1Answers: 10,055 Site admin

    What version of Editor are you using please? Can you give me a link to your page so I can take a look into it?

    Thanks,
    Allan

  • YOMYOM Posts: 51Questions: 21Answers: 1

    We're using the following editor packages:

        "datatables.net-editor": "^1.6.5",
        "datatables.net-editor-dt": "^1.6.3",
        "datatables.net-editor-se": "^1.6.3"
    

    Unfortunately I don't think I'm able to give you access to the app as it's a private internal application for our company, haha.

    If there's an alternative solution do let me know- otherwise I'm happy to just cast it to a number in the validation function manually, not a big deal either way.

  • allanallan Posts: 61,449Questions: 1Answers: 10,055 Site admin
    Answer ✓

    That would do it. As noted in the fields.getFormatter documentation, it was introduced in v2.

    Are you using JSON to send the data to the server rather than the default HTTP parameters? You might need to use preXhr in 1.x to modify the data being sent to the server.

    Allan

  • YOMYOM Posts: 51Questions: 21Answers: 1

    Ahh makes sense, good catch on the versioning. We are indeed using JSON to send it to the server.

    I took a look at the preXhr event. It looks like it accomplishes what I want but not thinking the juice is worth the squeeze for this case. Probably be cleaner to just do it on the backend

    Either way thanks for the help. I'll mark this question as completed

Sign In or Register to comment.