climu / openstudyroom

Website for the Open Study Room online go/baduk/weiqi community.
https://openstudyroom.org
GNU General Public License v3.0
67 stars 33 forks source link

Better ranking page #474

Closed BCoulange closed 2 years ago

BCoulange commented 2 years ago

This PR is mainly to create a new view of rankings for a given community.

But I added some cool stuff that can serve others :

Datatable helper

You can now add a datatable for your data very easily :)

Data provider

First you need to deliver data on an endpoint and the data need to have the form :

{
  "data":[
    {
       "col1":"val11",
       "col2":"val21",
       "col3":"val31"
    },
    {
       "col1":"val12",
       "col2":"val22",
       "col3":"val32"
    },
    {
       "col1":"val13",
       "col2":"val23",
       "col3":"val33"
    }
  ]
} 

controller

Next you need to prepare the datatable configuration in your view.py, with the following keys :

    context = {
        'datatable_config':{
            'columns':[     # name (what is displayed) and key (the key name) of your columns
                { "title":"Column 1", "key":"col1"},
                { "title":"Column 2", "key":"col2"},
                { "title":"Column 3", "key":"col3"}
            ],
            'id':'my_beautiful_table_id', # id you want for your datatable
            'url':f'/my/data/provider'  # where to fetch the data json 
        }
    }

Template

Finally in the template you can use the datatable tag that will put all what is necessary (html and js) :

{% load datatable_tags %}

...

{{ datatable_config | datatable_tag }}

JS lib

And this datatable is accessible via a small lib osr_datatable_loader as an object of window. For example you can change the source of the data by giving a new url :

osr_datatable_loader.update_data_source(new_url)
climu commented 2 years ago

Nice work! Thanks.