【歸納】Layui table.render里的json后台傳入


在使用Layui的table元素時,傳入的json的數據格式是有其自身定義的,需要另外添加一些字符,以正確傳入。

 

 

 

為了傳入符合前端格式的數據:

        table.render({
            elem: '#test'
            ,url:'http://localhost:8080/pictures'
            ,toolbar: '#toolbarDemo' //開啟頭部工具欄,並為其綁定左側模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定義頭部工具欄右側圖標。如無需自定義,去除該參數即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用戶數據表'
            ,cols: [
                [
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left', totalRowText: '合計:'}
                ,{field: 'picname', title: '名字', width:200}
                ,{field: 'character', title: '屬性', width:150}
                ,{field: 'home', title: '棲息地', width: 200}
                ,{field: 'url', title: 'url', width: 300}
                ,{fixed: 'right', title: '操作',width: 165, align:'center', toolbar: '#barDemo'}
            ]
            ]
            ,page: true
        });

在entity層,使用了transient 修飾符對字段進行限定:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity(name="picture")
public class Picture {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @Column(name = "picname")
    private String picname;
    @Column(name = "type")
    private transient String type;
    @Column(name = "url")
    private String url;

    @Column(name = "userid")
    private transient Long userid;
    @Column(name = "englishname")
    private transient String englishname;
    @Column(name = "character")
    private String character;
    @Column(name = "home")
    private String home;
}

在傳入中,為了滿足Layui的格式要求,加上了一些頭部:code、msg、count、data

    @RequestMapping("pictures")
    public String getPictures(HttpSession session){
        User user=(User)session.getAttribute("user");
        Long userid=user.getId();
        List<Picture> pictures;
        if(userid==1)
            pictures=picService.getAllPicture();
        else
            pictures=picMapper.getPicbyUserid(userid);
        String picstring=gson.toJson(pictures);
        log.info("  序列化結果:" + "{ \"code\": 0, \"msg\": \"\",\"count\": 15,\"data\": "+picstring+"}");
        picstring="{ \"code\": 0, \"msg\": \"\",\"count\": 10,\"data\": "+picstring+"}";
        return picstring;
    }

 

最終成功傳入,顯示:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM