zoukankan      html  css  js  c++  java
  • layui 分页 + vue渲染表格(html) (layui已于2021-10-13下架)

    layui 分页vue渲染表格:

    步骤:

    1、初始化获取表格列表数据,默认显示第一页数据列表;

    2、列表渲染完接着初始化layui分页

    3、分页点击跳转时再获取指定页码的数据列表

    问题点:

    点击分页后【jump】跳转分页将页面传递给获取list的方法,数据可以实时渲染最新的,但是方法不能再调用同一个,否则页码不能更新

     代码如下:

    
    
    <script>
    new Vue({
    el: '#app',
    data: {
    page: 1,
    total: 0,
    pageSize: 5,
    state: 4,
    loadImg: "",
    timer: null,
    free_status: 1,
    list: [],
    userInfo: {},
    radioData: [
    { value: '使用会员币',balance: "" },
    { value: '使用工单条数',balance: ""}
    ],
    tmpListTotal: 0,
    radioVal: 1,
    dialog: false,
    cIndex: -1,
    isFirstPage:true
    },
    filters: {
    /*空字符串转换*/
    capitalize: function (value) {
    if (!value) return '暂无用户名';
    value = value.toString();
    return value.charAt(0).toUpperCase() + value.slice(1);
    }
    },
    created() {
    this.getUserInfo();
    this.getList(this.page);
    },
    mounted() {
    this.$nextTick(() => {
    window.addEventListener('scroll', this.handleScroll, true);
    })
    },
    methods: {
    getList(_page) {
    let _url = "/apis/form_message/get_message";
    let _this = this;
    $.ajax({
    url: _url,
    data: {
    page: _page
    },
    method: "POST",
    success: function (res) {
    if (res.code == 1) {
    _this.total = res.data.list_count;
    _this.pageSize = res.data.pagesize;
    if(res.data.list_count === 0){
    _this.state = 6;
    }
    if(res.data.list_count <= _this.pageSize){
    _this.state = 5;
    }
    if (_this.page == 1) {
    _this.list = res.data.list;
    } else {
    _this.list = _this.list.concat(res.data.list);
    }
    if(_this.isFirstPage){
    _this.pageInit(_this.total); // 只有第一页,初始化的时候需要调用,页面跳转的时候不需要,只需要渲染列表即可
    }
    }
    }
    });
    },
    pageInit(listTotal) {
    let _this = this;
    if(listTotal === 0){
    $("#join_page").hide();

    }else{
    $("#join_page").show();
    }
    layui.use(['laypage','layer'], function() {
    let laypage = layui.laypage,layer = layui.layer;
    if(listTotal) {
    _this.tmpListTotal = listTotal;
    } else {
    listTotal = _this.tmpListTotal;
    }
    //页码初始化
    laypage.render({
    elem: 'list_page'
    ,theme: '#007AFF'
    , count: listTotal //数据总数
    ,limit: _this.pageSize // 每页条数
    ,prev:"<"
    ,next:">"
    ,jump: function(obj,first){
    if(!first){
    _this.isFirstPage = false;
    _this.getList(obj.curr);
    }else{
    _this.isFirstPage = true
    }

    }
    });
    });
    },

    /*获取用户信息*/
    getUserInfo() {
    let _url = "/form/message.php";
    $.ajax({
    url: _url,
    method: "POST",
    async: false, // 同步
    success: function (res) {
    let _res = JSON.parse(res);
    if (_res.code === 1) {
    userInfo = Object.assign({}, _res.data);
    console.log('userInfo', userInfo);
    }
    },
    error: function (res) {
    console.log('error_res', res);
    }
    });
    },
    /*选择购买方式*/
    getRadioVal() {
    console.log('radioVal',this.radioVal);
    },
    goBuy(index) {
    let _this = this;
    _this.cIndex = index;
    _this.dialog = !_this.dialog;
    _this.radioData[0].balance = userInfo.balance;// 剩余会员币余额
    _this.radioData[1].balance = userInfo.number; // 剩余工单数量

    },
    cancel(index){
    let _this = this;
    _this.cIndex = index;
    _this.dialog = !_this.dialog;
    },
    confirm(index){
    let _this = this;
    _this.cIndex = index;
    this.dialog = !this.dialog;
    let paramData = {
    type: _this.radioVal,
    userid: userInfo.userid,
    price:_this.list[index].price,
    rid:_this.list[index].rid,
    free:_this.list[index].is_resource
    }
    console.log("confirm",paramData);
    let _url = "/apis/form_message/buy_message";
    $.ajax({
    url: _url,
    method: "POST",
    data: paramData,
    success: function (res) {
    layer.msg(res.msg);
    if(res.code === 1){
    window.location.href= "../member/message.html";
    console.log('购买成功', res);
    }
    },
    error: function (res) {
    console.log('error_res', res);
    }
    });
    }
    }
    })
    </script>
     

     

    html:

    <div id="app">
        <table cellspacing="0" class="tb ls">
            <tbody>
            <tr>
                <th data-hide="1200">用户名</th>
                <th data-hide="1200">价格</th>
                <th>IP</th>
                <th>添加时间</th>
                <th>可领取次数</th>
                <th>已领取次数</th>
                <th>信息</th>
                <th>操作</th>
            </tr>
            <tr v-for="(item,p_index) in list" :key="p_index">
                <td>{{item.username | capitalize}}</td>
                <td>{{item.price}}</td>
                <td>{{item.ip}}</td>
                <td>{{item.create_time}}</td>
                <td>{{item.number_count}}</td>
                <td>{{item.c_number}}</td>
                <td>
                    <ul v-for="(_item,_index) in list[p_index].answer" :key="_index">
                        <li>{{_item.content}}</li>
                    </ul>
                    <div class="mask"  v-show="cIndex == p_index && dialog"></div>
                    <div class="buyBox" v-show="cIndex == p_index && dialog">
                        <p class="title">购买商品</p>
                        <p>是否确定购买此商品,提交后不可取消。</p>
                        <div class="radio"  v-for="(item, index) in radioData" :key="index">
                            <input
                                    type="radio"
                                    v-model="radioVal"
                                    :value="index+1"
                                    @change="getRadioVal"
                                    :id="index+1"
                            />
                            <label :for="index+1">{{ item.value }}<span>剩余:{{item.balance}}</span></label>
                        </div>
                        <div class="operate">
                            <button class="confirm" @click.stop="confirm(p_index)">确定</button>
                            <button  @click.stop="cancel(p_index)">取消</button>
                        </div>
                    </div>
                </td>
                <td><button @click.stop="goBuy(p_index)">购买</button></td>
            </tr>
            </tbody></table>
            <div id="list_page" class="list_page" style="text-align: center;"></div>
    </div>

     

    没有人能一路单纯到底,但是要记住,别忘了最初的自己!
  • 相关阅读:
    艾伟_转载:学习 ASP.NET MVC (第三回)实战篇 狼人:
    艾伟_转载:40条ASP.NET开发Tip 狼人:
    艾伟_转载:20条.NET编码习惯 狼人:
    艾伟_转载:数组排序方法的性能比较(上):注意事项及试验 狼人:
    艾伟_转载:使用LINQ to SQL更新数据库(上):问题重重 狼人:
    艾伟_转载:学习 ASP.NET MVC (第四回)实战篇 狼人:
    艾伟_转载:学习 ASP.NET MVC (第五回)理论篇 狼人:
    艾伟_转载:ASP.NET MVC 2博客系列 狼人:
    艾伟_转载:Cookie是什么?用法是怎样?与SESSION有什么区别?(二) 狼人:
    艾伟_转载:ASP.NET MVC 2博客系列之一:强类型HTML辅助方法 狼人:
  • 原文地址:https://www.cnblogs.com/LindaBlog/p/15419557.html
Copyright © 2011-2022 走看看