Get the value of the dynamic anchor tag and pass i

2019-08-10 23:41发布

问题:

I am developing a web application using Tornado framework. Currently I am working on a front end view. This page actually displays a list of ids and a small note about that id. (Its a table with two columns)


id -------- Note

100 ----- Note on 100

101 ----- Note on 101


![Table with link on id and note on id][1]

The ids are displayed dynamically. When I click on the id (its a link- anchor tag), it should take me to another page which has all the details about that id.

Approach - I thought of creating a cookie which will store the value of the id clicked, and get the cookie in tornado handler and retrieve all information about that id and send it to the next view.

The code so far is -

HTML

                <table class="table table-striped" id="tableid">
                    <tbody>
                        <tr>
                          <th>Config ID</th>
                           <th>Note</th>
                        </tr>

                       <tr>
                            {% for d in data %} 

                            <td>
                            <a onclick = "configidCreateCookieFunction()" href="/SelectedConfig"       name = "configid" id ="configid">{{ d[0] }}</a></td>
                                <td> {{ d[1]}}</td>
                              {% end %}
                            </tr>
                        <tbody>

                </table>
            </div>

So on clicking the link it would store the value of the id, and then the link points to a handler where I can get the cookie and retrieve more information about that id.

JavaScript

<script type="text/javascript">
 function configidCreateCookieFunction(){
  var cid = document.getElementById("configid").innerHTML;
  sessionStorage ['configid'] = cid
  }

Tornado code -

class SelectedConfigHandler(BaseHandler):
     def get(self):
        configid= None
        configid = self.get_cookie("configid")
        print configid

But this is not working. When I print the id -its getting displayed as None.

So basically I am not able to pull the value of the id which is a link, and the id is basically coming from the database Please help me out. Is my approach correct?

Thanks in Advance

回答1:

Where is the database here? since you use id why not accessing a database, say, MongoDB.

You have to know, that the cookie will be sent and received with your http messages, so you have to be aware of that, unless you want to make a local storage, but here, you have to garantee that, your users use an HTML5 browser, and send the data for the first time to be stored at the client end.

So the approache i suggest, is using the database, and use a hidden <input> and the value is the id.