flask_table icon indicating copy to clipboard operation
flask_table copied to clipboard

Default sort & unsorted ID columns

Open michaelmarzec opened this issue 3 years ago • 0 comments

Hello - another couple of questions that I am currently struggling to resolve.

  1. Is there an option so that the default direction setting = 'desc'?
  2. Is there a way to set it so that the first column (id or # column) does not get sorted (i.e., is always sorted in 'asc' order)?

My code for reference is below. Thanks!

class SortableTable(Table):
    id = Col('#')
    Team_Name = Col('Team Name')
    Average_Age = Col('Average Age')
    allow_sort = True

    def sort_url(self, col_key, reverse=False):
        if reverse:
            direction = 'desc'
        else:
            direction = 'asc'
        return url_for('index', sort=col_key, direction=direction)
@app.route('/', methods=['GET','POST'])
def index():
	df = main()
	sort = request.args.get('sort', 'Team_Name')
	reverse = (request.args.get('direction', 'asc') == 'desc')
	df = df.sort_values(by=[sort], ascending=reverse)
	output_dict = df.to_dict(orient='records')
	table = SortableTable(output_dict,
                          sort_by=sort,
                          sort_reverse=reverse)

	return render_template('view.html',  table=table.__html__())


if __name__ == "__main__":
	app.run()

michaelmarzec avatar Jan 26 '22 04:01 michaelmarzec