jimie VIP
Total posts: 531
19 Окт 2015 07:18

Hello Sergey,

Can you help me with some code which can show a percent of how many fields you've completed in an article ?

Rgds

Последние изменения: 21 Окт 2015


Sergey
Total posts: 13,748
20 Окт 2015 14:11

You mean on the article form?

That is quite possible to do. I think you can add bootstrap progress bar element to the form to show result.

<div class="progress">
  <div class="bar" id="form_progress"></div>
</div>

The idea here is to get all fields on the form and check how many was filled.

(function($){
    var fields = jQuery('input[type="text"], select, textarea', '#adminForm');
    var total_fields = fields.length;
    var form_progress = $('#form_progress');

    fields.change(function(){
        calcAvg();
    });

    calcAvg();

    function calcAvg() {
        var not_empty = 0;
        $.each(fields, function(key, val){
            if($(val).val()) {
                not_empty++;
            }
        });

        // Return completion in %
        form_progress.css('width', ((100 / total_fields) * not_empty) + '%');
    }
}(jQuery))

Of course this is a raw idea code is not tested. But I think it will be good starting point for you.


jimie VIP
Total posts: 531
20 Окт 2015 15:39

This is good for submission form, but actually what I want is to count the number of fields or percent, that a user has completed in his article ( section allowed max 1 article per user )

So then I show a message like:

" You have completed XX% of your article fields, please update it"

Rgds


Sergey
Total posts: 13,748
21 Окт 2015 18:32

jimie This is good for submission form, but actually what I want is to count the number of fields or percent, that a user has completed in his article ( section allowed max 1 article per user )

I cannot get it. You want to count articles or fields in article?

Where you want to show that alert in new article form or in full article view?

Or you mean you want something that will show in article full view how complete is this article and how much there is still room for improvement?

May be you need in template using PHP count all fields that has values. Something like count($item->fields_by_id). Then you will know how many fields are there. And you know how many fields are there at all. For example 30.

echo sprintf("You have completed %d%% of your article fields, please update it", ((100 / 30) * count($item->fields_by_id)));
Работает на Cobalt