My code is unable to read the "function(this)" and is passing in the search paramaters instead

Viewed 36

Context of code: So I am trying to paginate using the JSON values I receive from a function that I implemented, but I was unable to figure out how to paginate with only JSON values. And now I am trying to just make a prev and next button to get the appropriate results and render that page as a makeshift solution

Problem: for some reason when I go to the url /result?limit=2&offset=0, I recieve this error in the HTML, where when I click the button expand(which is supposed to lead to the JS function myFunction(z)) it sends in /result?limit=2&offset=0 instead of the tag where the button is located. Any help on the issue would be appreciated.

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Project Details</title>
    </head>
    <body>
        <link rel='stylesheet' href='//cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css'>
        <script src="//cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>       
        <table id='my-table' class="display" cellspacing="0" width="100%"> 
            <thead>
                <tr>            
                    <th>Project</th>
                    <th>Main Task</th>
                    <th>Sub Task</th>
                    <th>Start Date</th>
                    <th>Target Date</th>
                    <th>Actual Start Date</th>
                    <th>Actual Target Date</th>
                    <th>Estimate</th>
                    <th>Actual Hours Hours</th>
                    <th>Approved Hours</th>
                    <th>Overall Status</th>
                </tr>
            </thead>
            <tbody>
            {%- for val in details -%}
                <tr>
                    {%- set count = loop.index -1 -%}
                    <td><button id='{{count}}' onclick='myFunction(this)'>Expand</button>{{val['name']}}</td>
                    <td></td>
                    <td></td>
                    <td>{{val['start_date']}}</td>
                    <td>{{val['target_date']}}</td>
                    <td>{{val['timesheet_details']['actual_start_date']}}</td>
                    <td>{{val['timesheet_details']['actual_target_date']}}</td>
                    <td>{{val['estimate']}}</td>
                    <td>{{val['timesheet_details']['actual_hours']}}</td>
                    <td>{{val['timesheet_details']['approved_hours']}}</td>
                    <td>{{val['status']}}</td>
                </tr>
            {%- endfor -%}
            </tbody>
        </table>

        <script>
            function myFunction(z){
                console.log(z)
            }
            $(document).ready( function () {
                $('#my-Table').DataTable();
            } );
        </script>


    </body>
</html>

FLASK:

import flask
from flask import request,render_template,redirect,jsonify
from flask.helpers import url_for
from flask.wrappers import Request
import jinja2
import pymssql
import pandas as pd
from threading import Thread
from app2 import get_main_task, get_project_details, get_sub_task,get_proj_details

app = flask.Flask(__name__)
# print(proj) 
conn = pymssql.connect("10.20.4.44", "CSMSPMT_usr", "CSMSPMT_usr", "CSMSUAT")
query = f"SELECT Project_Id FROM VW_CSMS_PMT_ProjectDetails"
x = pd.read_sql(query,con=conn)['Project_Id'].tolist()

@app.route('/')
def lhome():
    proj = get_project_details(x[0:2],conn)
    return render_template('home copy 2.html',details=proj)

print(len(x))
@app.route('/result', methods=['GET'])
def result():

    offset = int(request.args['offset'])

    limit = int(request.args['limit'])
    if(offset+limit >=len(x)):
        print(x[offset:])
        details = get_project_details(x[offset:],conn)
    else:
        print('b')
        details = get_project_details(x[offset:offset+limit],conn)
    if(offset +limit <len(x)):
        next_url = '/result?limit='+ str(limit)+ '&offset='+str(offset+limit)
    else:
        next_url = ''
    prev_url = '/result?limit='+ str(limit)+ '&offset='+str(offset-limit)
    # return (jsonify({'result':details,'next_url':next_url,'prev_url':prev_url}))
    return render_template('newHome.html',details=details)
#pagination

if __name__ == '__main__':
    app.run(debug=True)
0 Answers
Related