Confused about handling of different types

Confused about handling of different types

redisentredisent Posts: 4Questions: 2Answers: 0

I have a data table with 6 columns that contains strings and 1 column that contains numbers. As I edit and update the cells in column with numbers they are transformed into strings. so I end up with a column where some of the values are strings and some are numbers. This is not what I want. Is there a way to force the numeric column to only update the data as a number?

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Hi @redisent ,

    Is this using Editor? I'm not seeing that, see this example here. If you are using Editor, please can you update that example to demonstrate your problem.

    If you aren't using it, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • redisentredisent Posts: 4Questions: 2Answers: 0
    edited February 2019

    Trying to figure out how to build one of these examples. In the mean time, what I am asking is does DataTables handle numbers? in the example you gave all of the numbers are strings.

    render: function (data, type, row) { 
                                if (typeof(data)==='number') {
                                    return('number-' + data.toFixed(9));
                                } else {
                                    return typeof(data) + data;
                                }
    }
    

    I added this small render function(above) to your code, and i can see that everything is a string. My data object has actual numbers. after they are edited they are stored in the data object as strings. Then I have a column with some numbers and some strings.

    I can handle all this myself if need be, but i was wondering if DataTables innately handles numbers as well as strings. there seems to be a 'type' under columndefs, but it doesn't seem to do anything

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Yep, numbers are fully supported - in my example the age column is a number as can be seen if you change an age to be "1" or "100" - it'll get sorted correctly.

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,769

    I think this example replicates what @redisent is referring to:
    http://live.datatables.net/vagapezu/1/edit

    Click the "Show Extn values" button and you will see all of the values are numeric. Edit one of the extensions then click the button again you will see the edited value is now a string.

    If you place the same button in your example you will see the values in the Age column are strings.

    Kevin

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Ah, yep, thanks Kevin. They are still numbers, the ordering is numeric, not string. Why they change is one for Allan (I'd bet it's because there isn't a numeric field type so it's always stored as a string), but I suspect it'll be deep in the internals and not configurable.

    A workaround in the columns.render, though clumsy, is just to convert that cell to a number regardless.

    Cheers,

    Colin

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Right - I'm with you now. Thanks for the example Kevin. It is exactly as Colin says - the input type doesn't have an type information - its just an HTML input and Editor doesn't attempt to do any type casting. Also on submit to the server the HTTP parameters are typeless, so even if the typing information were added to the input, it wouldn't make the trip through to the server. JSON or some other typed format would need to be used.

    Allan

  • redisentredisent Posts: 4Questions: 2Answers: 0

    Thanks for the feedback. I was able to build an example
    http://live.datatables.net/gaqikewe/1/edit
    change the age, and you will see number turn into string

    I understand that it is stored as a string. And the sorting function and render functions are there to handle the nuances of the data type.

    I guess what i need to do is convert all numbers to string, before table is made available to edit. then once editing is done, I have to remember which columns were originally numeric and convert them back from strings to numbers

This discussion has been closed.