简体   繁体   English

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

[英]Choosing a row from a table using pyramid

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: 这是我的表格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> 

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. 做出一些假设,您描述的内容将通过基于Javascript的ajax处理,并且不在Pyramid框架的范围内,但可以轻松添加。 Below is a very simple example of what the HTML might look like. 下面是一个非常简单的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>

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

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用于从Pyramid视图传递回javascript。 JSON stands for JavaScript Object Notation and is a data-interchange format. JSON表示JavaScript对象表示法,并且是一种数据交换格式。 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. 我创建了一个Python字典,并使用“ json”包将其转换为JSON,然后将其与方法调用的结果一起发送回javascript中的“成功”回调函数。 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)' '$(行)。文本(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. 这可能比您预期的要多,但是值得学习如何使用Ajax向您的网站添加功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM