Search code examples
angularjsx-editable

angularjs xeditable number but source is string formatted


I am using xeditable to edit data inline. Now I want to edit a numeric field, but the source data contains the number as string formatted. So I get the error ngModel:numFmt. How can I convert a string formatted number (example: 0.3) into a numeric for xeditable?

I tried with the string-to-number directive, but it seems, that xeditable did not recognize it:

        <td>
            <span data-editable-number="row.factor" 
                data-ng-model="row.factor"
                data-e-name="factor" 
                onbeforesave="validateRow(data, row.id)"
                onaftersave="save(row)" 
                data-e-required
                string-to-number="string-to-number"
            >
                {{row.factor}}

            </span>
        </td>

Solution

  • You can create a filter to convert String to Integer

    app.filter('num', function(){
        return function(input) {
            return parseInt(input, 10);
        };
    });
    

    and use it like this

    editable-number="row.factor | num "

    Working Plunker

    The previous didn't work updating the model, better way to achieve this is to transform the data beforehand.

    e.g

    var rows = [{factor:"1"}, {factor: "4"}, {factor: "9"}];
    var tranformed_rows  = rows.map(function(row) {
    
       row.factor = parseInt(row.factor);
       return row;
    });
    

    and then use tranformed_rows in your view.