Table of Contents
回复内容:
这样的情况就像购物车一样对吧?每一页选中的商品都扔进购物车,再批量处理。
Home Backend Development PHP Tutorial jquery-ajax - php+ajax分页时,checkbox复选框选中的问题

jquery-ajax - php+ajax分页时,checkbox复选框选中的问题

Jun 06, 2016 pm 08:51 PM
php

目的:所有的数据实现分页显示,不是查询所有的数据,而是每次取固定的条数。而且在每页选中的数据ID都可以保存,一起提交选中的数据,做相应的操作。比如第一页选中2条,第二页选中3条,提交时是5条,如果返回第一页,这也显示选中的数据,回到第二页,也会显示选中的数据,以此类推,取消选中,翻页几次,无任何问题;总之,无论选中还是取消,翻页都没有bug
这个该如何实现呢?..分页这个我没问题,但是这个复选框一点下一页,就会给刷新掉

回复内容:

目的:所有的数据实现分页显示,不是查询所有的数据,而是每次取固定的条数。而且在每页选中的数据ID都可以保存,一起提交选中的数据,做相应的操作。比如第一页选中2条,第二页选中3条,提交时是5条,如果返回第一页,这也显示选中的数据,回到第二页,也会显示选中的数据,以此类推,取消选中,翻页几次,无任何问题;总之,无论选中还是取消,翻页都没有bug
这个该如何实现呢?..分页这个我没问题,但是这个复选框一点下一页,就会给刷新掉

两种方案

方案1
- - -
1. 每一个页面一个单独的容器(div)。

<div class="content">
    <div class="page page-1">
        ...
    </div>
</div>
Copy after login

2. 加载新页面时,首先检查该页面是否已经加载过了,例如加载页面4

// 代码仅为示意
if($('.page-4', '.content').length > 0) {
    $('.page', '.content').hide();
    $('.page-4', '.content').show();
} else {
    page4 = render_page(load_page(4)); //得到page4的结构
    $('.page', '.content').hide();
    $('.content').append(page4);
}
Copy after login

这样在切换页面的时候就可以保存页面信息了。

方案2
- - -
创建一个对象来存放页面数据,并且让它支持自定义事件。当PAGE内容发生改变时,重新按照PAGE进行渲染。这实际上是一个MVC的方案。

下面的代码随手写的,只是为了示意。请勿直接使用。

// 可以用EventEmitter等等事件库来支持,我这里为了简单就用jQuery啦
var PAGE = $({
    current: -1,
    data: {}
});
PAGE.extend({
    fetch: function() {
        $.get('url', function(data) {
            this.data[current_page] = data
            this.trigger('value_change');
        })
    },
    show: function(page) {
        PAGE.current = page;
        if(this.data[page]) {
            this.trigger('value_change');
        } else {

            this.fetch(page);
        }
    },
});
PAGE.bind('value_change', function() {
    render_page();
});

var render_page = function() {
        if(PAGE[PAGE.current]) {
            //按照PAGE[PAGE.current]的内容渲染 div#page的内容
        } else {
            throw('error');
        }
    };


$('input[type="checkbox"]', '#page').change(function() {
    // 更新PAGE内容
    PAGE.data[current_page][item] = value

    PAGE.trigger(value_change);
});
Copy after login

选中后用一个数组变量a[]来保存选中的id;

分页时上一页数据的那部分html是删掉的还是隐藏的?删掉了肯定就肯定也没有了呀。

这样的情况就像购物车一样对吧?每一页选中的商品都扔进购物车,再批量处理。

我用CodeIgniter的时候用里面的购物车类处理过这种情况。原理就是用session存储选中的ID,每个ID会生成一个唯一的rowid,渲染页面的时候根据生成的rowid判断当前项是否被选中。

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
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles