2022-06-09 pywebio table sort 表格排序

官方都沒找到如何給默認(rèn)的表格排序。。。結(jié)合jquery的例子改了下,默認(rèn)的put_table 沒有<thead>,所以需要單獨(dú)分離出來head 和 rows,然后在拼接回去。

# -*- encoding: utf-8 -*-"

from pywebio.input import *
from pywebio.output import *
from pywebio import config
from flask import Flask

app = Flask(__name__)
app.config.from_object('flask_config')

js_code = '''
    $(document).on('click','th',function(){
        var table = $(this).parents('table').eq(0);
        var head = table.find('tr:eq(0)').toArray()
        var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()));
        this.asc = !this.asc;
        if (!this.asc){rows = rows.reverse();}
        table.children('tbody').empty().html(head).append(rows);
    });
    function comparer(index) {
        return function(a, b) {
            var valA = getCellValue(a, index), valB = getCellValue(b, index);
            return $.isNumeric(valA) && $.isNumeric(valB) ?
                valA - valB : valA.localeCompare(valB);
        };
    }
    function getCellValue(row, index){
        return $(row).children('td').eq(index).text();
    }
'''

@config(js_code=js_code)
def test():
    put_table([
        ['Commodity', 'Price', 'Count'],
        ['Apple', '5.5', 4],
        ['Banana', '7', 2],
        ['orange', '3', 5]
    ])

if __name__ == '__main__':
    # `task_func` is PyWebIO task function
    app.add_url_rule('/', 'test', webio_view_local(test), methods=['GET', 'POST'])
    app.run(host='0.0.0.0', port=5000)

參考文檔:
https://www.hujiangtao.cn/2017/03/jquery-table-order/#%E7%9B%AE%E5%BD%95
https://pywebio.readthedocs.io/en/latest/guide.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 10點(diǎn)半睡,6點(diǎn)半起,2+6.5=8.5小時(shí)有核心技術(shù)的人,才能活到最后。 iOS 閱讀整理在sourcetree...
    士夢(mèng)閱讀 3,153評(píng)論 0 18
  • 有一起學(xué)Python的小伙伴別忘記加入我們的Python學(xué)習(xí)交流群群:367203382 一、算法設(shè)計(jì)[https...
    ztloo閱讀 1,090評(píng)論 0 7
  • Bookmarks 書簽欄 入職 華為新員工小百科(刷新時(shí)間202003023) - 人才供應(yīng)知多少 - 3MS知...
    Btrace閱讀 1,723評(píng)論 0 0
  • google搜索 https://www.osjapan.net https://ipv6.google-api....
    zwb_jianshu閱讀 1,091評(píng)論 0 0
  • 免費(fèi)計(jì)算機(jī)書籍匯總。 沒有注明語種的,都為英語資源。 目錄 一、Web 開發(fā)[#%E4%B8%80web-%E5%...
    天涯學(xué)館閱讀 1,265評(píng)論 1 2

友情鏈接更多精彩內(nèi)容