使用金字塔从表中选择一行

发布于 2025-01-07 03:17:58 字数 431 浏览 0 评论 0原文

我想构建(构建表格很容易,我在模板内完成)一个表格并从中选择一行。我使用金字塔作为框架,我认为我需要以某种方式使模板与模型对话,但我不确定如何做。有人可以向我展示一个示例或指导我链接到可以看到解释和示例的链接(我找不到)。这是我的表格 HTML:

  <table border="1">
    <tr>
      <th>Course Number</th>
      <th>Course Name</th>
        </tr>
        <tr>
      <td>111</td>
      <td>What ever the name is</td>
    </tr>
  </table> 

I would like to construct (constructing a table is easy, I do it inside templates) a table and choose a row from it. I am using pyramid as a framework and I think somehow I need to make the templates talk to the model, but I am not sure how. Can someone show me an example or direct me to link where I can see an explanation and an example (I couldn't find one). This is my HTML for the table:

  <table border="1">
    <tr>
      <th>Course Number</th>
      <th>Course Name</th>
        </tr>
        <tr>
      <td>111</td>
      <td>What ever the name is</td>
    </tr>
  </table> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-01-14 03:17:58

做出一些假设,您所描述的内容将通过 ajax 处理,这是一种基于 javascript 的技术,并且超出了 Pyramid 框架的范围,但很容易添加。下面是一个非常简单的 HTML 示例。

<html>
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $('.row').bind('click',function(){
          var row = $(this);
          options = {
              url:"/some_pyramid_url_to_a_view/",
              type:"get",
              data:{'row_id':$(this).attr('id')},
              success:function(event){$(row).text(event.data.method_result)},
              error:function(){alert('error')},
              dataType:'json'
          }
          $.ajax(options);
      });
  });
</script>
  </head>
    <body>
      <table>
        <tr id="${row.id}" class="row">
          <td>Row</td>
        </tr>
    </body>
</html>

标签之间的代码是 javascript,这里我使用 jQuery 库创建对 url '/some_pyramid_url_to_a_view/' 的 ajax 调用,该调用与视图函数 'some_pyramid_view' 绑定。我使用“row”类“$('.row').bind('click',...”上的 jQuery 选择器将 click 事件绑定到表中的行元素,然后该事件由紧随其后的功能块“function(){...}”在选项对象中设置调用,并在数据“data:{'row_id':$(this).attr('”中传递行 ID。 id')}," 其中代码"$(this).attr('id')" 正在访问模板 '...' 中设置的 id 最后,我使用 '$.ajax(options)' 将请求发送到视图

import json

@view_config(xhr=True)
def some_pyramid_view(request):
    json_dict = dict()
    session = DBSession()
    if request.method == 'GET':
         row_id = request.GET['row_id']
         row = session.query(Row).get(row_id)
         json_dict['method_result'] = row.some_method_call()
    return Response(json.dumps(json_dict))

。 ,JSON 用于从金字塔视图与 javascript 进行通信。JSON 代表 JavaScript 对象表示法,是一种数据交换格式,我创建了一个 Python 字典,并使用“json”包将其转换为 JSON,然后将其发送回。到javascript 中的“成功”回调函数包含方法调用的结果。在成功回调函数中,我使用视图中的方法调用结果更新表行中的文本。 event.data.method_result)'

这可能超出您的预期,但非常值得学习如何使用 ajax 向您的网站添加功能。

Making some assumptions, what you describe would be handled via ajax, which is a technology based on javascript, and outside the scope of they Pyramid framework but easily added. Below is a very simple example of what the HTML might look like.

<html>
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $('.row').bind('click',function(){
          var row = $(this);
          options = {
              url:"/some_pyramid_url_to_a_view/",
              type:"get",
              data:{'row_id':$(this).attr('id')},
              success:function(event){$(row).text(event.data.method_result)},
              error:function(){alert('error')},
              dataType:'json'
          }
          $.ajax(options);
      });
  });
</script>
  </head>
    <body>
      <table>
        <tr id="${row.id}" class="row">
          <td>Row</td>
        </tr>
    </body>
</html>

The code between the tags is the javascript, here I am using the jQuery library to create the ajax call to the url '/some_pyramid_url_to_a_view/' which is tied to the view function 'some_pyramid_view'. I'm binding a click event to the row element in the table using the jQuery selector on the class 'row' "$('.row').bind('click',..." the event is then handled by the function block "function(){...}" immediately following. I'm setting up the call in the options object and passing the row id in the data "data:{'row_id':$(this).attr('id')}," where the code "$(this).attr('id')" is accessing the id set in the template '...' Finally I send of the request to the view using '$.ajax(options)'.

import json

@view_config(xhr=True)
def some_pyramid_view(request):
    json_dict = dict()
    session = DBSession()
    if request.method == 'GET':
         row_id = request.GET['row_id']
         row = session.query(Row).get(row_id)
         json_dict['method_result'] = row.some_method_call()
    return Response(json.dumps(json_dict))

There is another piece here, JSON is being used to communicate back to the javascript from the Pyramid view. JSON stands for JavaScript Object Notation and is a data-interchange format. I create a Python dictionary and using the 'json' package convert it to JSON which I send back to the 'success' call back function in the javascript with the results of the method call. In the success call back function I update the text in the table row with the result of the method call in the view. '$(row).text(event.data.method_result)'

This might be more than you were expecting but it is well worth learning how to use ajax to add functionality to your websites.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文