[Flask] Flask Digest, Vol 26, Issue 1

sidwoodstock . sidwoodstock at gmail.com
Mon Aug 7 18:38:37 EDT 2017


Couldn't you just concatenate the ID into the URL of your ajax call?

$(document).ready(function() { $('#test_table').DataTable( { "ajax" : {
"url" : $SCRIPT_ROOT + "/_get-x-test-rows" + *"/{{ x.id <http://x.id> }}/"*,
"data" : { "id" : {{ x.id }} }}
); } );

-Scott

On Tue, Aug 1, 2017 at 9:00 AM, <flask-request at python.org> wrote:

> Send Flask mailing list submissions to
>         flask at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/flask
> or, via email, send a message with subject or body 'help' to
>         flask-request at python.org
>
> You can reach the person managing the list at
>         flask-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Flask digest..."
>
>
> Today's Topics:
>
>    1. Datatables question (Craig Amundsen)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 1 Aug 2017 08:13:45 -0700
> From: Craig Amundsen <amundsen.craig at gene.com>
> To: flask <flask at python.org>
> Subject: [Flask] Datatables question
> Message-ID:
>         <CAJKLyXQFxfJ_5=dMU7wVWZ-KGxeAiVM3082WDtxBymUowG4QGA@
> mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Hi -
>
> I've googled around a bit and am having trouble figuring out how to pass a
> value from a flask page back to the view method that produces the json for
> loading a datatables table.
>
> Currently I'm using datatables to display all the entries in a given table
> with great success:
>
> in an html template where I have table with id="test_table":
> <script>
>         $(document).ready(function() { $('#test_table').DataTable( { "ajax"
> : $SCRIPT_ROOT + "/_get-test-rows" } ); } );
>     </script>
>
> in views.py:
>
> @main.route('/_get-test-rows')
> def get_test_rows():
>     trows = Test.query.order_by(Test.name)
>     rows = [ [tr.name, tr.x.name, tr.y, tr.z] for tr in trows]
>     return josnify(data = rows)
>
>
> In my above example, I have another table X that tr.x points to. I'd like
> to have a page that displays information about individual rows in X that
> has a table showing all rows from Test that have that point to that
> particular row in X.
>
> @main.route('/show-x/<int:id>')
> def show_x(id):
>     x = X.query.get_or_404(id)
>     return render_template('x.html', x = x)
>
> I know I can just get all the Test rows I need with tr =
> Test.query.filter_by(x_id = id).order_by(Test.name) pass them to
> render_template and render the table myself, but I'd prefer to have a
> jQuery call in the page that gets the rows of Test where x_id = x.id.
> Sometimes there are many rows of Test that will be shown and I'd like the
> page to start loading so the User sees that the click did get through and
> now we're waiting for the values to get loaded.
>
> My problem is that I can't figure out how to write the <script> call to
> include x.id and how to read that value in my view function. I tried this:
>
> $(document).ready(function() { $('#test_table').DataTable( { "ajax" : {
> "url" : $SCRIPT_ROOT + "/_get-x-test-rows", "data" : { "id" : {{ x.id }}
> }}
> ); } );
>
> and
>
> @main.route('/_get-x-test-rows/<int:id>')
> def get_x_test_rows(id):
>     tr = Test.query.filter_by(x_id = id).order_by(Test.name)
>     ...
>
> But the view method never gets called. I think I probably need to do
> something along the lines of
>
> @main.route('/_get-x-test-rows')
> def get_x_test_rows()
>     id = extract the id value from something somehow
>     tr = Test.query.filter_by(x_id = id).order_by(Test.name)
>     ...
>
> But I haven't been able to figure out how to do that.
>
> Could someone point me in the right direction to figure out how to do this?
>
> Thanks,
> - Craig
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/flask/attachments/
> 20170801/830d4ed6/attachment-0001.html>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
>
> ------------------------------
>
> End of Flask Digest, Vol 26, Issue 1
> ************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170807/4759021b/attachment.html>


More information about the Flask mailing list