jQWidgets Forums

jQuery UI Widgets Forums Grid Only display entries with dates in the future.

This topic contains 4 replies, has 2 voices, and was last updated by  AndySimpson 11 years, 2 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author

  • AndySimpson
    Participant

    Hi guys

    I have a json-file with entries from bookings, i.e.:

    [{"id":"293","room_name":"Eco Lab 1","room_number":"HH206","multi_id":null,"title":"Studie \"Forgetting\" Session 1","start":"2014-03-10 08:00","end":"2014-03-31 12:00","starttime":"08:00","stoptime":"12:00","allDay":false,"user":"rwernli","email":"blabla@blabla.com","single_seat":"true","seats":"4","comment":"","priority":"2","responsible":"me"},
    {"id":"295","room_name":"Eco Lab 1","room_number":"HH206","multi_id":null,"title":"Studie \"Forgetting\" Session 2","start":"2014-03-10 08:00","end":"2014-04-01 12:00","starttime":"08:00","stoptime":"12:00","allDay":false,"user":"rwernli","email":"blabla@blabla.com","single_seat":"true","seats":"4","comment":"","priority":"2","responsible":"me"}........

    I display them via jqxgrid. Unfortunately, it displays all entries, even those in the past. Is there a possibility to display only entries that take place in the present or future. Nothing in the past. I googled, but did not find a solution. Help’d be much appreciated 🙂

    cheers
    Andy


    Peter Stoev
    Keymaster

    Hi Andy,

    By using the DataAdapter plug-in’s beforeLoadComplete callback function, you can filter your data before loading it. For more information about the callback function and example of usage, look at: http://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxdataadapter/jquery-data-adapter.htm

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com


    AndySimpson
    Participant

    Hi Peter

    thanks for your answer. I read the article and did the following:

     var source =
                            {
                                datatype: "json",
                                datafields: [
                                    {name: 'title', type: 'string'},
                                    {name: 'room_name', type: 'string'},
                                    {name: 'room_number', type: 'string'},
                                    {name: 'start', type: 'date', format: "yyyy-MM-dd HH:mm"},
                                    {name: 'end', type: 'date', format: "yyyy-MM-dd HH:mm"},
                                    {name: 'user', type: 'string'}
                                ],
                                root: "record",
                                record: "",
                                // id: '',
                                url: "test.json",
                                sortcolumn: 'start',
                                sortdirection: 'asc'
                            };
                    var dataAdapter = new $.jqx.dataAdapter(source, {
                        beforeLoadComplete: function(records) {
                        
                            for (var i = 0; i < records.length; i++) {
                                var source = records[i];
                                var userDate = source.start;
                                
    
                                var date = new Date();
                                
                                var day = date.getDate();
                                var month = date.getMonth() + 1; //Months are zero based
                                var year = date.getFullYear();
                                var hour = date.getHours();
                                var min = date.getMinutes();
                                // tell the proper format
                                var today = '' + year + '-' + (month <= 9 ? '0' + month : month) + '-' + (day <= 9 ? '0' + day : day) + ' ' + (hour <= 9 ? '0' + hour : hour) + ' ' + (min <= 9 ? '0' + min : min);
                                if (userDate >= today) {
                                    return records;
                                }
                               
                               
                            }
    
                        }
                    });
    

    This does not the trick – the output is still everything. If I edit the if-condition to this:

    
    var empty = [];
    if (userDate >= today) 
    {
         return records;
    }
    else {
          return empty;
    }                           

    It displays “No data to display”.
    I think I misunderstood the beforeLoadComplete :-/

    cheers


    Peter Stoev
    Keymaster

    Hi Andy,

    I think that you missed the beforeLoadComplete’s idea. In beforeLoadComplete, you can modify the records or return a new Array. Loop through the records, push those which you are interested in a new array and return that array as a result after the for-loop.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com


    AndySimpson
    Participant

    Ok, I looked at it again, and now it works.
    For future vistors, that’s how I solved it:

    var dataAdapter = new $.jqx.dataAdapter(source, {
                        beforeLoadComplete: function(records) {
                         var data = new Array();
                            for (var i = 0; i < records.length; i++) {
                                var source = records[i];
                                var userDate = source.start;
                                
                                var date = new Date();                           
                                var today = date;
    
                                if (userDate >= today) {
                                    data.push(source)
                                  
                                }
                               
                                else {
                               
                                }
                            }
                            return data;
    
                        }
                    });

    Best,
    Andy

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.