Pre-sort a table from JSON

Pre-sort a table from JSON

stevelibbeystevelibbey Posts: 1Questions: 1Answers: 0

I am building a secure messaging component for a web app (which I can't share right now). I have a JSON file with the typical email info: from, to, subject, and status. I need to filter the datatable by these statuses: "new" shows up in the inbox tab, "sent" in the sent tab, "trash" in the trash tab, etc.

Example JSON:

{
            "status":"new",
            "attachment":"yes",
            "subject":"Visit Summary",
            "from":"Daniels, Roland MD",
            "date":"12 Mar 2014 11:16 AM",
            "patient":"Harris, Anna"
        },
        {
            "status":"trash",
            "attachment":"",
            "subject":"Request New Time (Reschedule)",
            "from":"Osterberg, James",
            "date":"11 Mar 2014 04:55 PM",
            "patient":""
        }

The datatable code:

$(document).ready(function() {
    $('#smInbox').dataTable( {
        "ajax": "sm/data/inbox.json",
        "columns": [
            { "data": null, "defaultContent": '<input type="checkbox" />', "orderDataType": "dom-checkbox" },
            { "data": function(data, type, row){
                    if(data.status == 'new'){
                        return '<span class="ic10 blueDot10" title="New Mail"></span>';
                    } else if(data.status == 'replied'){
                        return '<span class="ic16 replyGray16" title="Replied"></span>';
                    } else if(data.status == 'forwarded'){
                        return '<span class="ic16 forwardGray16" title="Forwarded"></span>';    
                    } else {
                        return data.status; 
                    }
                } },
            { "data": function(data, type,row){
                    if(data.attachment == 'yes'){
                        return "<span class='ic16 mailAttach16' title='Files attached'></span>";    
                    } else {
                        return data.attachment; 
                    }
                } },
            { "data": "subject" },
            { "data": "from" },
            { "data": "date" },
            { "data": "patient" }
        ],
        paging: false,
        scrollY: 450,
        responsive:true,
        "dom": 'lrtip',
        "order": [[ 5, "desc"]]
    } );

I have built search filters before which redraw based on keyed input, but I am not sure how to filter before drawing the table. If it was a SQL query I know how I'd write it.

Any ideas would be appreciated!

Answers

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Do you want to effectively remove the items that would be filtered out so they can't be displayed in the table at all? If so, the ajax.dataSrc option will allow you to modify the JSON returned from the server.

    Alternatively, if you do need the results to be displaying in future, use search or searchCols to specify an initial search term.

    Allan

This discussion has been closed.