Home Web Front-end Layui Tutorial How to implement data binding in layui

How to implement data binding in layui

Nov 19, 2020 am 11:15 AM
layui

How layui implements data binding: first introduce layui's css file and js file; then create a view to present the rendering results; then write the template and use a script tag to store the template; finally render the template .

How to implement data binding in layui

The operating environment of this tutorial: Windows 7 system, layui version 2.5.6. This article is applicable to all brands of computers.

Recommended: "layUI Tutorial"

Laytpl based on layui implements data binding

It took me a long time to remember the login password for my garden. You can imagine how long it has been since I logged in

Text
I used layui to make several management systems at the beginning, so it feels really comfortable to use. It’s easy to get started. The most commonly used ones in the management backend are form, table and pop-up window classes. The form table layer provided by layui is already very simple and easy to use. Regardless of the underlying encapsulation, I think the usage method shown to us is very simplified. In addition to the form table layer used in management, there are often some statistical data displays or content displays such as details pages. Then some used statistical data (except charts), such as text statistical data display. At this time, it is actually excellent to use layui based on laytpl to display this function.  

##Write a simple data binding first

The first step: Introduce layui’s css file and js file (introduced by yourself) The second step: Create a view to present the rendering results. The code is as follows:

 <p class="layui-row">        
 <p class="layui-col-md6" id="orderInfop"></p>       
</p>
Copy after login
Step 3: Write the template, use a script tag to store the template, the code is as follows:

<script type="text/html" id="orderInfo">
        <div class="layui-card">
            <div class="layui-card-header">订单概况</div>
            <div class="layui-card-body">
                <ul class="layui-row layui-col-space10 layadmin-backlog">
                    <li class="layui-col-xs6 layui-col-sm3">
                        <a class="layadmin-backlog-body">
                            <h3>营业额</h3>
                            <p><cite style="font-size:24px;">{{d.turnover}}</cite></p>
                        </a>
                    </li>
                    <li class="layui-col-xs6 layui-col-sm3">
                        <a class="layadmin-backlog-body">
                            <h3>订单数</h3>
                            <p><cite style="font-size:24px;">{{d.orderNum}}</cite></p>
                        </a>
                    </li>
                    <li class="layui-col-xs6 layui-col-sm3">
                        <a class="layadmin-backlog-body">
                            <h3>已发货</h3>
                            <p><cite style="font-size:24px;">{{d.delivered}}</cite></p>
                        </a>
                    </li>
                    <li class="layui-col-xs6 layui-col-sm3">
                        <a class="layadmin-backlog-body">
                            <h3>未发货</h3>
                            <p><cite style="font-size:24px;">{{d.unDelivered}}</cite></p>
                        </a>
                    </li>
                    <li class="layui-col-xs6 layui-col-sm3">
                        <a class="layadmin-backlog-body">
                            <h3>已取消</h3>
                            <p><cite style="font-size:24px;">{{d.cancelled}}</cite></p>
                        </a>
                    </li>
                    <li class="layui-col-xs6 layui-col-sm3">
                        <a class="layadmin-backlog-body">
                            <h3>已收货</h3>
                            <p><cite style="font-size:24px;">{{d.received }}</cite></p>
                        </a>
                    </li>
                    <li class="layui-col-xs6 layui-col-sm3">
                        <a class="layadmin-backlog-body">
                            <h3>已评价</h3>
                            <p><cite style="font-size:24px;">{{d.evaluated}}</cite></p>
                        </a>
                    </li>
                    <li class="layui-col-xs6 layui-col-sm3">
                        <a class="layadmin-backlog-body">
                            <h3>好评率</h3>
                            <p><cite style="font-size:24px;">{{d.favorableRate}}%</cite></p>
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </script>
Copy after login

Step 4: Render the template, the code is as follows:


<script>
  layui.use([&#39;laytpl&#39;], function () {
      var laytpl = layui.laytpl;

      //订单统计(正常情况下,此处应是ajax返回后的数据,这里是模拟数据。)
      //渲染模板所用的数据
      var data = { turnover: "23,251", orderNum: "256", delivered: "16", unDelivered: "130", cancelled: "10", received: "100", evaluated: "80", favorableRate: "80" }

      var orderInfoTpl = orderInfo.innerHTML  //获取模板,即上面所定义的 <script id="orderInfo">
      , orderInfoDiv = document.getElementById(&#39;orderInfoDiv&#39;);  //视图 即上面的 <div id="orderInfoDiv">
      laytpl(orderInfoTpl).render(data, function (html) { //渲染视图
          orderInfoDiv.innerHTML = html;
      });
    })
 </script>
Copy after login

Step 5: The effect is displayed as follows

Isn’t it very simple? Because a large number of backend systems are developed based on layui, if you encounter some data display types and do not want to use the traditional jquery binding method, it is actually very practical to use this.

Detailed data display

If it is a table, click to view the details, you can also display the data in this way

Step 1: Import The css files and js files of layui (introduced by yourself), and the css styles used in the page are written by yourself.

The second step: table data display, here is the assignment of known data, replace it with your own data during development, and define a click event for the table

 <table class="layui-hide" lay-filter="demoTableFilter" id="demoTable"></table>
Copy after login
  <script type="text/html" id="barDemo">      
   <a class="layui-btn layui-btn-xs" lay-event="detail">查看详情</a>
     </script>
Copy after login

Data used for rendering

<script type="text/html" id="demoDetail">
        <div>
            <div class="disF">
                <div class="flex1">
                    <div class="disF">
                        <label>姓名:</label>
                        <p class="flex1">{{d.username}}</p>
                    </div>
                </div>
                <div class="flex1">
                    <div class="disF">
                        <label>邮箱:</label>
                        <p class="flex1">{{d.email}}</p>
                    </div>
                </div>
                <div class="flex1">
                    <div class="disF">
                        <label>签名:</label>
                        <p class="flex1">{{d.sign}}</p>
                    </div>
                </div>
            </div>
            <div class="disF">
                <div class="flex1">
                    <div class="disF">
                        <label>性别:</label>
                        <p class="flex1">{{d.sex}}</p>
                    </div>
                </div>
                <div class="flex1">
                    <div class="disF">
                        <label>城市:</label>
                        <p class="flex1">{{d.city}}</p>
                    </div>
                </div>
                <div class="flex1">
                    <div class="disF">
                        <label>积分:</label>
                        <p class="flex1">{{d.experience}}</p>
                    </div>
                </div>
            </div>
        </div>
    </script>
Copy after login

The following is the assignment of table

table.render({
          elem: &#39;#demoTable&#39;
        , cols: [[ //标题栏
            { field: &#39;id&#39;, title: &#39;ID&#39;, width: 100 }
            , { field: &#39;username&#39;, title: &#39;用户名&#39;, width: 80 }
            , { field: &#39;email&#39;, title: &#39;邮箱&#39;, width: 180 }
            , { field: &#39;sign&#39;, title: &#39;签名&#39;, width: 180 }
            , { field: &#39;sex&#39;, title: &#39;性别&#39;, width: 80 }
            , { field: &#39;city&#39;, title: &#39;城市&#39;, width: 100 }
            , { field: &#39;experience&#39;, title: &#39;积分&#39;, minWidth: 80 }
            , {  width: 100, align: &#39;center&#39;, toolbar: &#39;#barDemo&#39; }
        ]]
        , data: [{
            "id": "10001"
            , "username": "杜甫"
            , "email": "xianxin@layui.com"
            , "sex": "男"
            , "city": "浙江杭州"
            , "sign": "人生恰似一场修行"
            , "experience": "116"
            , "ip": "192.168.0.8"
            , "logins": "108"
            , "joinTime": "2016-10-14"
        },{
            "id": "10002"
          , "username": "李白"
          , "email": "xianxin@layui.com"
          , "sex": "男"
          , "city": "浙江杭州"
          , "sign": "人生恰似一场修行"
          , "experience": "12"
          , "ip": "192.168.0.8"
          , "logins": "106"
          , "joinTime": "2016-10-14"
          , "LAY_CHECKED": true
         }]
      });
Copy after login

table page effect display

The third step is to create a pop-up for rendering detailed data Box

<p class="demoDetailp" style="display:none;padding:10px;">
        <p id="detailp"></p>
 </p>
Copy after login

The fourth step is to click "View Details" of the table to implement data binding through laytpl. The code is as follows:

table.on(&#39;tool(demoTableFilter)&#39;, function (obj) {
          var data = obj.data;
          if (obj.event === &#39;detail&#39;) {
              index = layer.open({
                  title: &#39;查看详情&#39;,
                  type: 1,
                  move: false,
                  content: $(&#39;.demoDetailDiv&#39;),
                  area: [&#39;750px&#39;, &#39;300px&#39;],
                  resize: false,
                  scrollbar: false
              });
              var demoDetailTpl = demoDetail.innerHTML  //获取模板,
                , detailDiv = document.getElementById(&#39;detailDiv&#39;);  //视图
                laytpl(demoDetailTpl).render(obj.data, function (html) { //渲染视图
                    detailDiv.innerHTML = html;
              });
          }
      });
Copy after login

The display effect is as follows:

Summary:

The above is the simplest way to record data using laytpl.

The above is the detailed content of How to implement data binding in layui. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get form data in layui How to get form data in layui Apr 04, 2024 am 03:39 AM

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

How to set up jump on layui login page How to set up jump on layui login page Apr 04, 2024 am 03:12 AM

Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

How layui implements self-adaptation How layui implements self-adaptation Apr 26, 2024 am 03:00 AM

Adaptive layout can be achieved by using the responsive layout function of the layui framework. The steps include: referencing the layui framework. Define an adaptive layout container and set the layui-container class. Use responsive breakpoints (xs/sm/md/lg) to hide elements under specific breakpoints. Specify element width using the grid system (layui-col-). Create spacing via offset (layui-offset-). Use responsive utilities (layui-invisible/show/block/inline) to control the visibility of elements and how they appear.

How to run layui How to run layui Apr 04, 2024 am 03:42 AM

To run layui, perform the following steps: 1. Import layui script; 2. Initialize layui; 3. Use layui components; 4. Import layui styles (optional); 5. Ensure script compatibility and pay attention to other considerations. With these steps, you can build web applications using the power of layui.

Which one is better, layui or elementui? Which one is better, layui or elementui? Apr 04, 2024 am 04:15 AM

Question: Which one is better, layui or ElementUI? Answer: It depends on the project requirements. Layui is more comprehensive, customizable and suitable for large projects, while ElementUI is more lightweight, beautiful and easy to use. The specific reasons for selection are as follows: Choose layui: Provides a wider range of functions and modules that allow a high degree of customization of component appearance and behavior. Suitable for large projects that require a wide range of functions and scalability. Choose ElementUI: Smaller size and faster loading speed. Components follow Material Design principles. , high aesthetics, providing a large number of ready-made components, reducing development complexity and time

What is the difference between layui and vue? What is the difference between layui and vue? Apr 04, 2024 am 03:54 AM

The difference between layui and Vue is mainly reflected in functions and concerns. Layui focuses on rapid development of UI elements and provides prefabricated components to simplify page construction; Vue is a full-stack framework that focuses on data binding, component development and state management, and is more suitable for building complex applications. Layui is easy to learn and suitable for quickly building pages; Vue has a steep learning curve but helps build scalable and easy-to-maintain applications. Depending on the project needs and developer skill level, the appropriate framework can be selected.

The difference between layui framework and vue framework The difference between layui framework and vue framework Apr 26, 2024 am 01:27 AM

layui and vue are front-end frameworks. layui is a lightweight library that provides UI components and tools; vue is a comprehensive framework that provides UI components, state management, data binding, routing and other functions. layui is based on a modular architecture, and vue is based on a componentized architecture. layui has a smaller ecosystem, vue has a large and active ecosystem. The learning curve of layui is low, and the learning curve of vue is steep. Layui is suitable for small projects and rapid development of UI components, while vue is suitable for large projects and scenarios that require rich functions.

What language is layui framework? What language is layui framework? Apr 04, 2024 am 04:39 AM

The layui framework is a JavaScript-based front-end framework that provides a set of easy-to-use UI components and tools to help developers quickly build responsive web applications. Its features include: modular, lightweight, responsive, and has complete documentation and community support. layui is widely used in the development of management backend systems, e-commerce websites, and mobile applications. The advantages are quick start-up, improved efficiency, and easy maintenance. The disadvantages are poor customization and slow technology updates.

See all articles