jfinal layui 多选传值问题整理

使用layui在显示数据表格进行多选的时候遇到的几个问题:

1、增加监听,让你的数据表格可以进行复选。

    layui.use('table', function(){
        var $ = layui.jquery;
        var table = layui.table;

        //监听表格复选框选择
        table.on('checkbox(table)', function(obj){
            console.log(obj)
        });

2、获取选择的项,这里注意几点,

1、table.checkStatus('tablelist') 这里的tablelist是定义的table id

<table id="tablelist">

2、使用 checkStatus.data 来获取数据 ,数据格式为数组

3、定义 数组 ids 来存放id的时候,首先要初始化  var ids=[]; 设置为数组

4、ids增加id的时候推荐使用push,删除id 使用pop

        $("#sync").on('click',function () {
            var checkStatus = table.checkStatus('tablelist'),data = checkStatus.data;
           var ids=[];
            for(var i=0;i<data.length;i++){
                ids.push(data[i].id);
            }
            console.log(ids);
            $.ajax({
                type : 'post',
                url : '/goController/methon',
                data:{ids:ids},
                success : function(response){
                    parent.layer.alert(response.msg);
                },
                error : function (response) {
                    parent.layer.alert(response.msg);
                }
            });
        });

3、jfinal 在接收传过来的数组时有指定的方法,getparaValues ,这里要注意 写为 “ids”的时候获取值为Null,只有设置为 ids[] 时候才可以获取相关值

String[] ids = getParaValues("ids[]");

4、处理ids直接生成 select * from table where id in ("id1","id2");

使用 Arrays.toString 方法直接转为 id1,id2, 这种形式,方便组装 sql

            String idstr = Arrays.toString(ids);
            idstr = idstr.substring(1,idstr.length()-1);
            sqlstr.append(" and id in (" +idstr +")");

  方法其实挺简单,作者都提供了,但调试的时候不注意就要浪费很多时间,写下来作为备忘。

原文地址:https://www.cnblogs.com/shej123/p/11678825.html