无法在jtable的选项字段中填充数据

时间:2013-12-10 07:59:06

标签: json jsp servlets jquery-jtable

我有一个jsp页面,其中我实现了J-Table。我在jtable中有一个名为ClassID的字段,我想将其作为下拉列表。那么我如何尝试这样做:

$('#UserTableContainer').jtable({
            title : 'Table of Users',
            actions : {
                listAction : 'CRUDController?action=list',
            fields : {

    ClassID : {
    title : 'ClassID',
    list : true,
    width : '50%',
    edit : true,
    option:'CRUDController?action=getClassID'
    },

我的模型,即BEAN类是:

private int id;
    private String name;
    private String classID;

public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

public String getClassID() {
        return classID;
    }
    public void setClassID(String classID) {
        this.classID = classID;
    }

当listAction:'CRUDController?action = list'被调用时,我得到jtable中从servlet到jsp的填充数据

但是当控件来到这一行时:选项:'CRUDController?action = getClassID'它转到servlet类的dopost方法,搜索action = getClassID然后创建jsonArray。但是当控件返回到jsp页面时,它无法填充JTable中的下拉列表

我的servlet代码是:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (request.getParameter("action") != null) {
    List<UserModel> lstUser = new ArrayList<UserModel>();
    String action = (String) request.getParameter("action");
    Gson gson = new Gson();
    response.setContentType("application/json");
        if (action.equals("list")) {
        try {
        // Fetch Data from User Table
        lstUser = daoForMat.getAllUserList();
        // Convert Java Object to Json
        JsonElement element = gson.toJsonTree(lstUser, new TypeToken<List<UserModel>>() {
        }.getType());
        JsonArray jsonArray = element.getAsJsonArray();
        String listData = jsonArray.toString();
        // Return Json in the format required by jTable plugin
        listData = "{\"Result\":\"OK\",\"Records\":" + listData + "}";
        response.getWriter().print(listData);
        } catch (Exception ex) {
        String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getMessage() + "}";
        response.getWriter().print(error);
        ex.printStackTrace();
        }


        else if(action.equals("getClassID") ){
        System.out.println("I came to action getClassID");
        List<String> lstClassID = new ArrayList<String>();
    //Here i am able to get the List containing classID
        lstClassID = GetClassList();
        JsonElement element = gson.toJsonTree(lstClassID , new TypeToken<List<String>>() {
        }.getType());
        JsonArray jsonArray = element.getAsJsonArray();
        String listData = jsonArray.toString();
        // Return Json in the format required by jTable plugin
        listData = "{\"Result\":\"OK\",\"Records\":" + listData + "}";
        response.getWriter().print(listData );
        //return jsonArray;
        }
      }
    }

我做错了什么?我为 action.equals(“getClassID”)编写了与 if(action.equals(“list”))相同的代码。 **对于后来的条件,我能够填充Jtable但是对于这种情况 - action.equals(“getClassID”)我无法填充JTable中的下拉列表。

唯一的区别是action=list我正在为响应编写BEAN类列表。即。List<UserModel> lstUser = new ArrayList<UserModel>();正如您在servlet代码中看到的那样  当action=getClassID我在回复中写一个字符串列表,即List<String> lstClassID = new ArrayList<String>();

我应该在jsp页面中的JTABLE选项字段中写什么才能填充下拉列表?**期待您的解决方案。提前致谢

1 个答案:

答案 0 :(得分:0)

嘿,你可以试试这个。

声明一个你将用作下拉列表的字段

                     Location:
                          {
                            title: 'Location',
                            width: '12%',
                            list: true,
                            options: '/JTablePractice.aspx/GetContinentalOptions',
                        },

并在前端写下

 public static object GetContinentalOptions()
    {
        using (var db = new ASPPracticesEntities1())
        try
        {

            var numbers = db.Members.Select(c => new { DisplayText = c.Location, Value = c.Location }).ToList();
            return new { Result = "OK", Options = numbers };
        }
        catch (Exception ex)
        {
            return new { Result = "ERROR", Message = ex.Message };
        }
    }

Donno如何在java中编写它,但是它可以帮助你