Unable to populate data in option field of jtable

1.5k Views Asked by At

I have a jsp page in which i have implemented J-Table. I have a field in jtable called ClassID which i want to make as dropdown. So how am i trying to do it as :

$('#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'
    },

My Model i.e. the BEAN Class is :

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;
    }

When listAction : 'CRUDController?action=list' is called i get the populated data in jtable from servlet to the jsp

But when control comes to this line : option:'CRUDController?action=getClassID' , It goes to the dopost method of servlet class, searches the action=getClassID and then creates the jsonArray. But when the control comes back to the jsp page, it is unable to populate the dropdown in the JTable

My servlet code is :

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;
        }
      }
    }

What am i doing wrong? I have written the same code for action.equals("getClassID") as that for if (action.equals("list")). **For the later condition i am able to populate the Jtable but for this condition - action.equals("getClassID") i am not able to populate the dropdown in JTable.

The only difference is when action=list i am writing a list of BEAN Class to the response. ie.List<UserModel> lstUser = new ArrayList<UserModel>(); as you can see in the servlet code and when action=getClassID i am writing a list of String to the response i.e.List<String> lstClassID = new ArrayList<String>();

What should i write in option field of JTABLE in the jsp page so that i can populate the dropdown?** Looking forward to your solutions. Thanks in advance

1

There are 1 best solutions below

0
On

Hey you can try this.

declare a field whch u gonna use as dropdown like this

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

and write this in your front end

 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 how to write it in java but hopw it help u