Format JSON string before displaying it?

Format JSON string before displaying it?

cojlcojl Posts: 14Questions: 3Answers: 1

Hey guys!

I'm running MVC 5 with Entity framework, and I populate the datatable with a model containing both Date and TimeSpan variables. I don't know how to go about formatting date, timespan, decimals etc before sending it to the view in MVC.

When I was processing the data and filtering client-side it was pretty straightforward, but now I'm processing the data on the server-side.

Date, for instance, will look like the following: "/Date(1230764400000)", when I really want "2000-01-01" etc.

As you can see, the date is not very nicely formatted, and is the reason that I want to format the data before displaying it. Same goes for a couple of TimeSpan objects etc that I eventually want to show in the table.

Table Initialization:

        var table = $('#myTable').DataTable({
        responsive: true,
        "serverSide": true,
        "ajax": {
            "type": "POST",
            "url": '/Table/DataHandler',
            "contentType": 'application/json; charset=utf-8',
            'data': function (data) { return data = JSON.stringify(data); }
            },
            "drawCallback": function(settings){
            $('.card').hide();
            },
           "paging": true,
           "deferRender": true,
           "columns": [
           { "data": "RegId" },
           { "data": "PresenceDate" }, etc...

Controller that returns a JSON object to my table:

            public JsonResult DataHandler(DTParameters param)
            {
            try
            {
            var dtsource = new List<spRegistrations_Result>();
            using (entities dc = new entities())
            {
                dtsource = dc.spRegistrations().ToList();
            }

            List<String> columnSearch = new List<string>();

            foreach (var col in param.Columns)
            {
                columnSearch.Add(col.Search.Value);
            }

            List<spRegistrations_Result> data = new ResultSet().GetResult(param.Search.Value, param.SortOrder, param.Start,  param.Length, dtsource, columnSearch);
            int count = new ResultSet().Count(param.Search.Value, dtsource, columnSearch);
            DTResult<spRegistrations_Result> result = new DTResult<spRegistrations_Result>
            {
                draw = param.Draw,
                data = data,
                recordsFiltered = count,
                recordsTotal = count
            };
            return Json(result);
        }
        catch (Exception ex)
        {
            return Json(new { error = ex.Message });
        }
    }

Model:

          public int RegId { get; set; }
          public System.TimeSpan StartTime { get; set; }
          public System.TimeSpan EndTime { get; set; }
          public System.DateTime PresenceDate { get; set; }

I'm still pretty new to ajax, and don't know how to go about this the easiest way. Thanks for any input !

This question has accepted answers - jump to:

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    https://datatables.net/reference/option/columns.render

    Render it one way for display and another way for sorting. Look at moment.js (separate library) for dealing with that date. It looks like a Unix timestamp.

    http://momentjs.com/docs/#/parsing/unix-timestamp/

  • cojlcojl Posts: 14Questions: 3Answers: 1

    Hmm alright. I'll look into it. Thing is, I wan't to format alot, for instance I have a float which I want cut down to only 3 decimals, a couple TimeSpans that I want to display properly and then the date, which I guess I could use momentjs for!

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    The render option is column specific, so you have a lot of flexibility.

  • cojlcojl Posts: 14Questions: 3Answers: 1

    Alright I'll look into it, will update later!

  • cojlcojl Posts: 14Questions: 3Answers: 1
    Answer ✓

    I solved it, looks perfect now thanks to columns.render. I really appreciate the advice!

  • sqldaddysqldaddy Posts: 1Questions: 0Answers: 0

    Hello,
    As I have seen that @cojl ,has solved his query. I have a package of
    SSIS JSON File Source and other related topic package with sample work. Where task is explained step by steps with snapshorts and to whom they are getting again problem then We have shared video of work. Feel free to see
    http://zappysys.com/products/ssis-powerpack/ssis-json-file-source/

    Hope it will helpful for future. :-)

This discussion has been closed.