动态获取 jQuery 字段以进行 jtable 分页



我正在使用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>

我已经找到了做到这一点的方法,我正在发布这个以供其他正在搜索这个的人参考

<%@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>

最新更新