Fork me on GitHub

JFinal和datatales-editor增删改

页面js代码如下:

editor = new $.fn.dataTable.Editor( {
            ajax: {
                url:"${ctx}/console/job/dtAction?job.orgID="+orgID,
                  data:function(data){
                    var result = {};
                    for(var i in data.data){
                      //封装json的格式
                         var result=data.data[i];
                         result.DT_Rowid=i;
                         result.action=data.action;
                         console.log(result); 
                         $.each(result,function (index, value) {
                             //console.log(index);
                             if(index!="action" && index!="DT_Rowid"){
                                 modifyJosnKey(result,index,"job."+index);
                             }
                         })
                    }
                    return result;
                },
            },
            table: "#example",
            idSrc:"id",
            fields: [ {
                    label: "主键",
                    name:  "id",
                    type:  "hidden",
                }, {
                    label: "岗位名称",
                    name:  "jobname"
                }, {
                    label: "岗位描述",
                    name:  "jobdesc",
                },{
                    label: "排序",
                    name:  "sortid",
                }
            ],
            i18n: {
                create: {
                    button: "新建",
                    title:  "新建",
                    submit: "保存"
                },
                edit: {
                    button: "编辑",
                    title:  "编辑",
                    submit: "保存"
                },
                remove: {
                    button: "删除",
                    title:  "删除",
                    submit: "删除",
                    confirm: {
                        _: "您是不是一定希望删除%d行吗?",
                        1: "您是不是一定希望删除1行吗?"
                    }
                },
                error: {
                    system: "发生错误,请联系管理员"
                },
                datetime: {
                    previous: '上一个',
                    next:     '下一个',
                    months:   [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ],
                    weekdays: [ '周一', '周二', '周三', '周四', '周五', '周六', '周日' ]
                }
            }
        } );
        table = $('#example').DataTable( {
              language: {
                  "sProcessing": "处理中...",
                  "sLengthMenu": "显示 _MENU_ 项结果",
                  "sZeroRecords": "没有匹配结果",
                  "sInfo": "显示第 _START_ 至 _END_ 项结果,共 _TOTAL_ 项",
                  "sInfoEmpty": "显示第 0 至 0 项结果,共 0 项",
                  "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                  "sInfoPostFix": "",
                  "sEmptyTable": "表中数据为空",
                  "sLoadingRecords": "载入中...",
                  "sInfoThousands": ",",
                  "oPaginate": {
                      "sFirst": "首页",
                      "sPrevious": "上页",
                      "sNext": "下页",
                      "sLast": "末页"
                  },
                  "oAria": {
                      "sSortAscending": ": 以升序排列此列",
                      "sSortDescending": ": 以降序排列此列"
                  }
              },
              searching:false,
              dom: '<"dt-panelmenu clearfix"Bfr>t<"dt-panelfooter clearfix"ip>',
              ajax: "${ctx}/console/job/dtAction?orgID="+orgID,
              columns: [
                    { data: "jobname", className: "center" },
                  { data: "jobdesc", className: "center" },
                  { data: "updatetime", className: "center" }
              ],
              select: true,
              buttons: [
                        { extend: "create", editor: editor },
                        { extend: "edit",   editor: editor },
                        { extend: "remove", editor: editor }
              ] 
          } );

        $("#tree").fancytree({
              source:${orgs},
            icons: false, // Display node icons.
            clickFolderMode: 2, // 1:activate, 2:expand, 3:activate and expand, 4:activate (dblclick expands)
            activate: function(event, data) {
                orgID=data.node.data.id;
                //console.log(orgID);
                //console.log(table);
                //console.log(table.context[0]._editor.s.ajax.url);
                table.context[0]._editor.s.ajax.url="${ctx}/console/job/dtAction?job.orgID="+orgID;
                table.ajax.url("${ctx}/console/job/dtAction?orgID="+orgID).load();//再次加载
            }
        });

后台java代码如下:

public void dtAction() {
    String action = getPara("action", "");
    switch (action) {
    case "create":
        Job job = getModel(Job.class);
        User user = getSessionAttr("User");
        job.set("insertuserid", user.getStr("username"));
        job.set("updateuserid", user.getStr("username"));
        service.save(job);
        JSONObject json = new JSONObject();
        json.put("data", job);
        //Map map1 = new HashMap();
        //map1.put("data", job);
        renderJson(json);
        break;
    case "edit":
        Job job1 = getModel(Job.class);
        service.update(job1);
        Map map2 = new HashMap();
        map2.put("data", job1);
        renderJson(map2);
        break;
    case "remove":
        Job job2 = getModel(Job.class);
        job2.delete();
        renderJson();
        break;
    default:
        String orgID = getPara("orgID");
        List<Job> jobs = Job.dao.find("select * from t_job where orgid=?", orgID);
        Map map = new HashMap();
        map.put("data", jobs);
        renderJson(map);
        break;
    }
    return;
}