【问题标题】:getting jquery fields dynamically for jtable pagination为jtable分页动态获取jquery字段
【发布时间】:2014-12-24 12:20:30
【问题描述】:

我正在使用 jquery 使用 jtable 插件在 jsp 中进行分页

以下代码是用 jsp 编写的,这里所有要在分页中显示的字段都是硬编码的来自另一个表的数据,其中列的数字或类型可能不同,它不会显示内容,所以我试图根据所选查询中的列给出这些字段名称。我将把所有列名称作为列表获取到这个 jsp

like list=[Id,name,salary,Doj,Dob,Ctc];

此列表可能因表而异,因此我想动态地将列表中的这些值作为以下代码中的字段,例如使用迭代器之类的东西,我对 jquery 完全陌生。 除了一些从列表中动态获取字段值的建议

这是 jsp 的代码,它使用 ajax 调用从数据库中检索数据

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <!-- Include one of jTable styles. -->
    <link href="css/metro/crimson/jtable.css" rel="stylesheet"
        type="text/css" />
    <link href="css/jquery-ui-1.10.3.custom.css" rel="stylesheet"
        type="text/css" />
    <!-- Include jTable script file. -->
    <script src="js/jquery-1.8.2.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
    <script src="js/jquery.jtable.js" type="text/javascript"></script>
    <script type="text/javascript">
    function TxtValue()
    {
        var Selectedval = document.getElementById("data");
        document.getElementById("name").value = Selectedval.value;


        }
/************** jquery fields are hardcoded here*******************************/


    $(document).ready(function() {
                $('#PersonTableContainer').jtable({
                    title : 'Table of people',
                    paging : true, //Enable paging
                    pageSize : 10, //Set page size (default: 10)           
                    actions : {
                        listAction : 'CRUD?action=list',
                        createAction : 'CRUD?action=create',
                        updateAction : 'CRUD?action=update',
                        deleteAction : 'CRUD?action=delete'

                    },
                    fields : {
                        EMPLOYEE_CODE  : {
                            title : 'EMPLOYEE_CODE ',
                            key : true,
                            list : true,
                            create : true,
                            edit : false

                        },

                        NAME  : {
                            title : 'NAME',
                            width : '30%',
                            edit : true
                        },
                        CC : {
                            title : 'CC',
                            width : '30%',
                            edit : true
                        },
                        LOADED_COST_PA  : {
                            title : 'LOADED_COST_PA ',
                            width : '20%',
                            edit : true
                        },
                        LOADED_COST_PM  : {
                            title : 'LOADED_COST_PM ',
                            width : '30%',
                            edit : true
                        },
                        DOJ  : {
                            title : 'DOJ ',
                            width : '30%',
                            edit : true
                        },LOB   : {
                            title : 'LOB  ',
                            width : '30%',
                            edit : true
                        },LOADED_COST_PA  : {
                            title : 'LOADED_COST_PA ',
                            width : '30%',
                            edit : true
                        },ONSITE_MANAGER  : {
                            title : 'ONSITE_MANAGER ',
                            width : '30%',
                            edit : true
                        }


            }
        });
             //Re-load records when user click 'load records' button.
            $('#LoadRecordsButton').click(function (e) {
                e.preventDefault();
                $('#PersonTableContainer').jtable('load', {
                    name: $('#name').val(),
                    id: $('#id').val()
                });
            });

            //Load all records when page is first shown
            $('#LoadRecordsButton').click();
        //  $('#PersonTableContainer').jtable('load');

        /***********************************************/


        $('#id').change(function(event) {
                var columnName = $("select#id").val();
                $.get('CRUD?action=columnFilter', {
                        columnName : columnName
                }, function(response) {

                var select = $('#data');
                select.find('option').remove();
                  $.each(response, function(index, value) {
                  $('<option>').val(value).text(value).appendTo(select);
              });
                });
                });



            /*******************************************/

        });

        </script>
        </head>
        <body>
        <%!  
        public String tablename = "";  
        %>

        <%
           HttpSession sec = request.getSession();
          List columnsList=(List)sec.getAttribute("columnsList");

        %>
        <c:import url="manage_data.jsp" />
        <br/>
        <div class="filtering"
            style="width: 60%; margin-right: 20%; margin-left: 20%; text-align: center;">
            <form>
                Search By: <select id="id" name="table">
                    <option selected="selected" value="default">Complete Data</option>
                                
                    <c:forEach var="str" items="${columnsList}">
                        <option>${str}</option>
                        <br>
                    </c:forEach>

                </select>
                 Select Data:
                <select id="data" onclick ="TxtValue()"   >
                    <option  selected="selected">--NONE--</option>


                        <option></option>                
                </select>


                 Enter Value: <input type="text" name="name" id="name" />

                <button type="submit" id="LoadRecordsButton">Load records</button>
            </form>

            <div id="PersonTableContainer"></div>
        </div>
        </body>
        </html>

【问题讨论】:

    标签: jquery ajax jsp jakarta-ee jquery-jtable


    【解决方案1】:

    我已经找到了执行此操作的方法,我发布此内容以供其他正在搜索此内容的人参考

    <%@page import="java.util.ArrayList"%>
    <%@page import="java.util.List"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Setup and Load Data to jTable using Servlets and JSP</title>
    <!-- Include one of jTable styles. -->
    <link href="css/metro/crimson/jtable.css" rel="stylesheet" type="text/css" />
    <link href="css/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
    <!-- Include jTable script file. -->
    <script src="js/jquery-1.8.2.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
    <script src="js/jquery.jtable.js" type="text/javascript"></script>
    <script type="text/javascript">
    <% List<String> strList = new ArrayList<String>();
    strList.add("one");
    strList.add("two");
    strList.add("three"); %>
    
    var jsArray = [<% for (int i = 0; i < strList.size(); i++) { %>"<%= strList.get(i) %>"<%= i + 1 < strList.size() ? ",":"" %><% } %>];
    var fields = {
    
        };
    var arrayLength = jsArray.length;
    
        for(var i=0;i<arrayLength;i++)
            {
        fields[jsArray[i]] = {
            title: jsArray[i],
            width: '40%'
        };
    }
    
        $(document).ready(function () {
            $('#PersonTableContainer').jtable({
                title: 'Table of people',
                actions: {
                    listAction: 'CRUDController?action=list',
                    createAction:'CRUDController?action=create',
                    updateAction: 'CRUDController?action=update',
                    deleteAction: 'CRUDController?action=delete'
                },
                fields:fields
    
            });
            $('#PersonTableContainer').jtable('load');
        });
    
    </script>
    </head>
    <body>
    <div style="width:60%;margin-right:20%;margin-left:20%;text-align:center;">
    <div id="PersonTableContainer"></div>
    </div>
    
    
    </body>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多