>  기사  >  웹 프론트엔드  >  Layui에서 table.render 사용

Layui에서 table.render 사용

尚
앞으로
2020-01-09 17:41:098371검색

Layui에서 table.render 사용

프런트 엔드 구현 코드는 그림(전체 코드)과 같습니다.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>数据表格</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

  <div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
      <a lay-href="">主页</a>
      <a><cite>组件</cite></a>
      <a><cite>数据表格</cite></a>
      <a><cite>开启头部工具栏</cite></a>
    </div>
  </div>
  
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">开启头部工具栏</div>
          <div class="layui-card-body">
            <table class="layui-hide" id="test-table-toolbar" lay-filter="test-table-toolbar"></table>
            
            <script type="text/html" id="test-table-toolbar-toolbarDemo">
              <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
              </div>
            </script>
             
            <script type="text/html" id="test-table-toolbar-barDemo">
              <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
              <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="../../layuiadmin/layui/layui.js"></script>  
  <script>
  layui.config({
    base: &#39;../../layuiadmin/&#39; //静态资源所在路径
  }).extend({
    index: &#39;lib/index&#39; //主入口模块
  }).use([&#39;index&#39;, &#39;table&#39;], function(){
    var admin = layui.admin
    ,table = layui.table;
  
    table.render({
      elem: &#39;#test-table-toolbar&#39;
       ,url:"http://localhost:8090/program-web/api/magic_change/oj/problem/page_list?userId=youcongtech"
      ,toolbar: &#39;#test-table-toolbar-toolbarDemo&#39;
      ,title: &#39;程序设计题绑定&#39;
      ,cols: [[
        {type: &#39;checkbox&#39;, fixed: &#39;left&#39;},
        {field:&#39;problemId&#39;, width:300, title: &#39;ID&#39;, sort: true}
        ,{field:&#39;title&#39;, width:400, title: &#39;题目&#39;}
        ,{width:215, align:&#39;center&#39;, fixed: &#39;right&#39;, toolbar: &#39;#test-table-toolbar-barDemo&#39;}
      ]]
      ,page: true
    });
    
    //头工具栏事件
    table.on(&#39;toolbar(test-table-toolbar)&#39;, function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case &#39;getCheckData&#39;:
          var data = checkStatus.data;
          layer.alert(JSON.stringify(data));
        break;
        case &#39;getCheckLength&#39;:
          var data = checkStatus.data;
          layer.msg(&#39;选中了:&#39;+ data.length + &#39; 个&#39;);
        break;
        case &#39;isAll&#39;:
          layer.msg(checkStatus.isAll ? &#39;全选&#39;: &#39;未全选&#39;);
        break;
      };
    });
    
    //监听行工具事件
    table.on(&#39;tool(test-table-toolbar)&#39;, function(obj){
      var data = obj.data;
      if(obj.event === &#39;del&#39;){
        layer.confirm(&#39;真的删除行么&#39;, function(index){
          obj.del();
          layer.close(index);
        });
      } else if(obj.event === &#39;edit&#39;){
        layer.prompt({
          formType: 2
          ,value: data.email
        }, function(value, index){
          obj.update({
            email: value
          });
          layer.close(index);
        });
      }
    });
  
  });
  </script>
</body>
</html>

핵심 js 코드는 다음과 같습니다.

table.render({
   elem: &#39;#test-table-toolbar&#39;
 ,url:"http://localhost:8090/program-web/api/magic_change/oj/problem/page_list?userId=youcongtech"
   ,toolbar: &#39;#test-table-toolbar-toolbarDemo&#39;
   ,title: &#39;程序设计题绑定&#39;
,cols: [[
  {type: &#39;checkbox&#39;, fixed: &#39;left&#39;},
     {field:&#39;problemId&#39;, width:300, title: &#39;ID&#39;, sort: true}
     ,{field:&#39;title&#39;, width:400, title: &#39;题目&#39;}
     ,{width:215, align:&#39;center&#39;, fixed: &#39;right&#39;, toolbar: &#39;#test-table-toolbar-barDemo&#39;}
   ]]
   ,page: true
 });

백그라운드 반환 데이터 형식이 다음과 같아야 합니다.

{
  "msg": "success",
  "code": "0",
  "data": [
    {
      "title": "for循环输出",
      "problemId": 1139
    },
    {
      "title": "测试2",
      "problemId": 1138
    },
    {
      "title": "测试1",
      "problemId": 1137
    },
    {
      "title": "for循环-Plus",
      "problemId": 1140
    },
    {
      "title": "第一个C++程序",
      "problemId": 1141
    }
  ]
}

그렇지 않으면, 관련 프롬프트가 나타납니다(예: 값에 대한 코드는 000000이 아니라 0이어야 하며 데이터의 해당 데이터는 위와 같아야 합니다. 그렇지 않으면 열에서 자동으로 일치하지 않습니다.

백그라운드 구현 코드는 다음과 같습니다.

제어 레이어 코드(라우팅)

@GetMapping("/page_list")
@ApiOperation(value="根据用户ID获取题目分页列表",httpMethod="GET",notes="根据用户ID获取题目分页列表")
public JSONObject page_list(@RequestParam String userId, @RequestParam (value="page") String pageno, @RequestParam (value="limit") String pagesize) {
    
    System.out.println("userId:"+userId+"|| pageno:"+pageno+"||pagesize:"+pagesize);
    
    JSONObject json = new JSONObject();
    
    //当前页
    Integer page = Integer.parseInt(pageno.trim());
    //每页的数量
    Integer size = Integer.parseInt(pagesize.trim());

    Map<String, Object> paramMap = new HashMap<>();
    paramMap.put("userId", userId);
    paramMap.put("start", (page - 1) * size);  //当前页的数量
    paramMap.put("size", size);  //当前页
    
    List<Problem> problemList = problemService.getProblemPageListInfo(paramMap);
     
    int count =problemService.getProblemPageTotalCount(paramMap);

    if(!problemList.isEmpty()) {

        json.put("msg", "success");
        json.put("code", "0");
        json.put("data", problemList);
        json.put("count", count);
        
    }else {
        
        json.put(CommonEnum.RETURN_MSG, "error");
        json.put(CommonEnum.RETURN_CODE, "222222");
    }
    
    return json;

}

서비스 및 해당 구현 클래스 ) .

위 내용은 Layui에서 table.render 사용의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 cnblogs.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제