Bad Addy
Posts: 0
Joined: Fri Dec 13, 2013 9:34 pm

How to add listview counter via javascript

Using the REST system in Appery, you can set the counter value of a listview.

I need each value to be checked, which needs another action, which I have to do in JS for each listitem. How do I place a value in the listview counter value box in javascript ?

I have checked jquerymobile and Appery docs, but no mention of it.

Bad Addy
Posts: 0
Joined: Fri Dec 13, 2013 9:34 pm

How to add listview counter via javascript

The function I am calling, is on each array item, where it calls the JS function getCounterInfo(value, element);

This is the code I have used, as I need to query another table and respond with the answer before the next array element is loaded.

precode
$.ajax({
type: 'GET',
beforeSend: function(request) {
request.setRequestHeader('X-Appery-Database-Id', '588*****5c11c3d');
},
url: 'https://api.appery.io/rest/1/db/collections/statements',
dataType: 'json',
data: {
where: '{"group_name":'+getCounterInfo.group_name+', "group_type" : '+getCounterInfo.group_type+', "createdAt" : {"$gt" : '+setdateFormat+'}}',
count: "1"
},
success: function(res) {
console.log(res);
}
});
/code/pre

Image

group_name and group_type do not seem to be working? And is there not a better way to do this ?

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

How to add listview counter via javascript

There are two ways how to do this:

  1. Using server code http://docs.appery.io/tutorials/creat...

  2. Using security context service

  3. Create custom security context service: http://docs.appery.io/documentation/g...

  4. Use following code to wrap answer and make custom logic(including server requests)
    pre
    code
    Appery.MySecurityGeneric = Appery.createClass(SecurityContext, {

    invoke: function(service, settings) {
    var oldSuccess = settings.success;

    Code: Select all

     var onSuccess = function(value, other){ 
         var onNeedToComplete = function(){ 
             oldSuccess.call(this, value, other); 
         }; 
    
         //here is logic on value(response from server). 
         for(var i in value){ 
    
            //Set counterValue for each response item. 
            value[i].counterValue = i + 3; 
         } 
    
         //You should call this function when all your changes competed. So if you use async js - you should call it when all transformations are ready. 
         onNeedToComplete(); 
     }; 
    
     settings.success = onSuccess; 
     Appery.MySecurityGeneric.$super.invoke.call(this, service, settings); 

}

});
/code
/pre

where MySecurityGeneric - is your service name

  • Select "Security Context" on generic service properties tab.

    After you did all the steps there will be available "counterValue" field. Map "counterValue" field to the listItem.

Bad Addy
Posts: 0
Joined: Fri Dec 13, 2013 9:34 pm

How to add listview counter via javascript

Sorry, but this makes little sense to me. First you said there are two ways of doing this, but both are pointing to custom logic, just one has a script. So I don't understand that. Nor how it works with my existing script.

Second, you must already have a counterValue because you can use it while completing each array item:
Image

So I cannot see how doing all this, which I have no idea what it does, where to put it, or how that ties with my existing script!

I have not used any server scripts because honest, your docs do not make much sense in using them. Perhaps thats why I am in such a loss?

That said, as per above image, you already have a counterValue. How do I access this in javascript, so my count in the code I have provided can populate it ?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

How to add listview counter via javascript

Can you send screenshot of service "test" tab?

Bad Addy
Posts: 0
Joined: Fri Dec 13, 2013 9:34 pm

How to add listview counter via javascript

I read on another question a few months ago that you are unable to access the Counter Value.

I honestly do not think you understand what I am asking. In my image above, you have a Counter value, that you can populate with a response from a data query. But, as I cannot do a query within a query, I need to do another query while its populating each listItem.

So I wanted to fill the Current Value with the result to the second query. That is not a service, as I cannot do two services at once ;)

This is the work around, but I cannot get the listview as tidy as I can using a sevice.

precode

$.ajax({
type: 'GET',
beforeSend: function(request) {
request.setRequestHeader('X-Appery-Database-Id', '53088********1c8cd3d');
},
url: 'https://api.appery.io/rest/1/db/collections/groups/',
dataType: 'json',
data: {
where: '{"user_id" : "' + getVar('user_id') + '"}',
sort: 'group_name'
},
success: function(qcllist) {
$.each(qcllist, function(cn,cv) {
//count statements from last login date
var gn = cv.group_name;
var gt = cv.group_type;
//console.log(gt);
$.ajax({
type: 'GET',
beforeSend: function(request) {
request.setRequestHeader('X-Appery-Database-Id', '5308*************c8cd3d');
},
url: 'https://api.appery.io/rest/1/db/collections/statements',
dataType: 'json',
data: {
where: '{"$and" : [{"group_name": "'+cv.group_name+'"}, {"group_type" : "'+cv.group_type+'"}, {"_createdAt" : {"$gt" : {"$date" : "'+getVar('lastLoggedIn')+'"}}}]}',
count: "1"
},
success: function(res) {
var col,fcol;
if(res[0].count > 0) {
col = "red&quot
fcol = "white&quot
} else {
col = "white&quot
}

Code: Select all

                         //console.log(res); 
                         $('#groupsListView')&#46;append('<li class="section_screen_mobilelistitem_9 ui-btn-icon-right ui-icon-carat-r ui-li-static ui-body-inherit ui-li-has-count ui-btn-up-a ui-last-child"><a id="groupClick" ref="'+gn+'" name="'+gt+'" href="#"><div class="ui-li-static-container">'+gn+'<span class="ui-li-count" style="background-color:'+col+' !important; color:'+fcol+'">'+res[0]&#46;count+'</span><br><small>'+gt+'</small><></a></li>'); 
                     } 
                 }); 
          }); 
     } 

});
/code/pre

All I wanted to know, is how do you populate the Counter Value box within a Javascript Function, rather than a Service Response.

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

How to add listview counter via javascript

We proposed to you two variants how to do this. You could use one of this variant.
So if you don't want to use server code - you could implement your functionality on client side. Did you try to use security context service or server code?
Let us know if you need help with our implementation.

Return to “Issues”