0

I have a table with 2 columns: Keys and Values.

The keys are dates, formatted with the sap time format. Something like this:

"/Date(1510700400000)/"

I have a function which shows the date in a readable form.

        convertDate: function() {
        if (value === 'null') {
            return value.replace('null', 'no date specified')
        } else {
            var d = new Date(parseInt(value.replace('/Date(', '').replace(')/', ''), 10))
        }
        var month = d.getUTCMonth() + 1 // months from 1-12
        var day = d.getUTCDate()
        var year = d.getUTCFullYear()
        return year + '/' + month + '/' + day

    },

I access my value via:

            oTable2.addColumn(new sap.ui.table.Column({
            label: new sap.ui.commons.Label({
                text: "RDATE"
            }),
            sortProperty: "RDATE",
            template: new sap.ui.commons.TextView().bindProperty("text", "key")
        }));

Now I want to call my function on "key".

How can I accomplish that? :)

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
sleepysimon
  • 393
  • 3
  • 6
  • 24

1 Answers1

1

You can achieve this using a formatter function.

 template: new sap.ui.commons.TextView().bindProperty("text", {
    path: "key",
    formatter: function(value){
        if (value === 'null') {
            return value.replace('null', 'no date specified')
        } else {
            var d = new Date(parseInt(value.replace('/Date(', '').replace(')/', ''), 10))
        }
        var month = d.getUTCMonth() + 1 // months from 1-12
        var day = d.getUTCDate()
        var year = d.getUTCFullYear()
        return year + '/' + month + '/' + day
    }
});

Reference: https://sapui5.netweaver.ondemand.com/#/topic/07e4b920f5734fd78fdaa236f26236d8

Stephen S
  • 3,936
  • 2
  • 23
  • 33
  • thanks, that works! only when I try to return "no date specified" when key == null, that wont work. it gives out: NaN/NaN/NaN formatter: function(key) { if (key) { var d = new Date(parseInt(key.replace('/Date(', '').replace(')/', ''), 10)) var month = d.getUTCMonth() + 1 // months from 1-12 var day = d.getUTCDate() var year = d.getUTCFullYear() return year + '/' + month + '/' + day } else { return "asd" } } – sleepysimon Jan 09 '18 at 08:39
  • I am not sure what you're trying to do. Does the older formatter function not work or did you change the code ? – Stephen S Jan 09 '18 at 09:52
  • the old formatter didnt work because of my if statement if value === null. i changed the sequence, and when there is a date, its displayed normally. only for when theres no date specified the formatter is not working – sleepysimon Jan 09 '18 at 11:45