• table实现 js数据访问 传递json数据用render_to_response


    1. $(document).ready(function(){
    2. $.ajax({
    3. url:'/query/',
    4. dataType:'json',
    5. type:'GET',
    6. success:function(data){
    7. for(var i=0;i<data.length;i++){
    8. var a='<tr>'+'<td>'+data[i].codename+'</td>'+'<td>'+data[i].name+'</td>'+'<td>'+data[i].content_type_id+'</td>'+'</tr>';
    9. $('tbody').append(a)
    10. }
    11. }
    12. });
    13. });
    14. js通过.访问属性
    1.ajax方式实现

    数据data为list,每一个元素为dict


    2.用bootstrap自带的table实现,还是麻烦了一点
    1. <script type="text/javascript" src="{% static 'js/bootstrap-table.js' %}"></script>
    2. <script type="text/javascript" src="{% static 'js/bootstrap-table-zh-CN.js' %}"></script>
    3. <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap-table.css' %}">
    1. <table data-toggle="table" data-url="/query/">
    2. <thead>
    3. <tr>
    4. <th data-field="codename">权限代码</th>
    5. <th data-field="name">权限名称</th>
    6. <th data-field="content_type_id">权限分组</th>
    7. </tr>
    8. </thead>

    3.最简单的实现,其实就是js访问后端传过来的数据,用.访问js属性,{{}}传递参数
    1. return render_to_response('query.html', {'user_permission': user_permission},context_instance=RequestContext(request))
    2. 然后在query.html里可以循环user_permission
    3. {% for user in user_permission%}
    4.   {{ user.user_name }}
    5. {% endfor %}

    4.输出复杂好看的表格

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8" />
    5. <title>数据汇总</title>
    6. <script type="text/javascript" src="/static/webprj/template.js"></script>
    7. <script type="text/javascript" src="/static/webprj/jquery-1.11.2.js"></script>
    8. <script type="text/javascript" src="/static/bootstrap/js/bootstrap.min.js"></script>
    9. <link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap.min.css">
    10. <script type="text/javascript" src="/static/bootstrap-table/bootstrap-table.js"></script>
    11. <link rel="stylesheet" type="text/css" href="/static/bootstrap-table/bootstrap-table.css">
    12. <style type="text/css">
    13. table,table td,table th{border:1px solid #000;border-collapse:collapse;font-size: 15px;}
    14. table tr td{
    15. padding:0px 0px 0px 5px;
    16. }
    17. table tr th{
    18. text-align:center;
    19. }
    20. table{
    21. max-width:100%;
    22. font-size:12px;
    23. }
    24. </style>
    25. </head>
    26. <body>
    27. <div>
    28. <div id="dv">
    29. <input id="print2" type="button" value="打印">
    30. </div>
    31. <table id="table" class="table table-bordered table-hover">
    32. </table>
    33. </div>
    34. <!--方法2-->
    35. <div style="display:none">
    36. <form id="postData_form" method="post" target="_blank">
    37. <!--<form id="postData_form" target="_blank">-->
    38. <input name='postData' id='postData' type='hidden' value=''/>
    39. </form>
    40. </div>
    41. <script type="text/javascript">
    42. //var results = document.getElementById("results");
    43. var spinfos={{spinfos|safe}};
    44. $(function(){
    45. $('#table').bootstrapTable({
    46. method: 'get',
    47. cache: false,
    48. height: 600,
    49. striped: true,
    50. trimOnSearch: true,
    51. checkAll:true,
    52. clickToSelect: true,
    53. singleSelect: false,
    54. pagination: false,
    55. pageSize: 10,
    56. pageNumber:1,
    57. pageList: [10, 20, 50, 100, 200, 500],
    58. search: true,
    59. showColumns: true,
    60. showRefresh: false,
    61. showExport: true,
    62. columns:[
    63. {
    64. field:'state',
    65. checkbox:'true'
    66. },
    67. {
    68. field:'name',
    69. title:'名称',
    70. align:"center",valign:"middle",sortable:"true"
    71. },{
    72. field:'spec',
    73. title:'规格',align:"center",valign:"middle",sortable:"true"
    74. },{
    75. field:'code',
    76. title:'商品代码',align:"center",valign:"middle",sortable:"true"
    77. },{
    78. field:'munit',
    79. title:'单位',align:"center",valign:"middle",sortable:"true"
    80. },{
    81. field:'code2',
    82. title:'条形码',align:"center",valign:"middle",sortable:"true"
    83. },
    84. {
    85. field:'price',
    86. title:'价格',align:"center",valign:"middle",sortable:"true"
    87. },{
    88. field:'vipprice',
    89. title:'会员价',align:"center",valign:"middle",sortable:"true"
    90. }],
    91. data:spinfos
    92. })
    93. $("#print2").click(function(){
    94. printData()
    95. })
    96. })
    97. function printData(){
    98. var newspinfos = []
    99. var rows = $.map( $('#table').bootstrapTable('getSelections'),function(row){
    100. return row
    101. });
    102. console.log(rows)
    103. //console.log(spinfos)
    104. var textspinfos = JSON.stringify(rows)
    105. console.log(textspinfos)
    106. $("#postData").val(textspinfos)
    107. var postUrl = "/printprice2"
    108. document.getElementById("postData_form").action=postUrl
    109. document.getElementById("postData_form").submit()
    110. }
    111. </script>
    112. </body>
    113. </html>































  • 相关阅读:
    amazeui学习笔记--css(HTML元素1)--按钮Button
    【DataStructure】The description of Java Collections Framework
    android面试题 不仅仅是面试是一个很好的学习
    SVNclient安装与使用
    Microsoft Visual C++ Runtime Library Runtime Error解决的方式
    fullcalendar日历控件集合知识
    android存储阵列数据SharedPreferences
    Codeforces 484E Sign on Fence(是持久的段树+二分法)
    漂亮的表格样式(使用CSS样式表控制表格样式)
    交叉编译libxml2
  • 原文地址:https://www.cnblogs.com/wuqingzangyue/p/5750034.html
Copyright © 2020-2023  润新知