How to get values in json file based on a certain value

How to get values in json file based on a certain value

patreeeeekpatreeeeek Posts: 14Questions: 7Answers: 0

Hey guys!

I have a datatable with messages.

Below is a sample data of my JSON fie.

{"aaData":[{"messageId":"14857658871809681877","sender":"+639168760480","smsc":"+639170000299","portId":"1","content":"Test message","receiveTime":"2017-01-30 16:44:44","hasRead":"No"}

I want to display these messages like a normal inbox of a phone. Which means the datatable will only display the latest message of the sender and the number and it wont repeat.

this is what I have so far. I don't know how to make the viewing conditional. Can someone help me please. Thanks!

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Where are you retrieving the data from?

    If using a SQL like database you can something like the SQL SELECT DISTINCT to return data with different (distinct) values. It would be best to do this in your sever script if you can.

    Otherwise you will need to process your JSON in Javascript and remove unwanted data before presenting to Datatables.

    Kevin

  • patreeeeekpatreeeeek Posts: 14Questions: 7Answers: 0

    How can I process my JSON file in javascript?

    $(document).ready(function () {

            $('#datatables').DataTable({
                "dom": '<"toolbar">frtip',
                "responsive": true,
                "scrollY": "550px",
                "scrollCollapse": true,
                "ajax": "smsMessages.json",
            "aoColumns": [
                {"mData": "sender"},
                {"mData": "content"},
                {"mData": "receiveTime"},
                {"mData": "messageId",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        $(nTd).html("<i class='ti-pencil-alt btn btn-simple btn-edit btn-icon' href='#roleModal' data-toggle='modal' data-mode='edit'></i>\n\
                         <a href='#' type='button' class='btn btn-simple btn-danger btn-icon dt-delete'><i class='ti-close'></i></a>");
                    }
                }
            ],
                language: {
                    "search": "_INPUT_",
                    searchPlaceholder: "Search records"
                }
            });
            demo.initCirclePercentage();
        });
    

    this is how I retrieve my data right now

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    You can use ajax.dataSrc as a function and in the function loop through the data and keep what you want. The docs have a function example. The loop is simply your preferred way to loop through an array of objects.

    Kevin

This discussion has been closed.