te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Current state of bootstrap collapse stored in HTML5 Storage - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Current state of bootstrap collapse stored in HTML5 Storage - Stack Overflow

programmeradmin2浏览0评论

I'm not sure if this is possible, but I hope someone can direct me in the right direction to achieve what I wish to achieve.

I'm building a website around bootstrap from twitter; I use bootstrap collapse on a section box. This box isn't collapsed by default, but if a user pushes a button and collapses the box; I want this information to be stored in some kind of cache on the client side even after page refresh.

Let's say the user collapses them, and returns tomorrow; then they would still be collapsed until the user pushes the button again.

I won't include any of my code as I suspect there are more to this than only the added classes (I use the default one anyway), nor can I link to the project as it is not available outside of our intranet.

Here's a copy from bootstrap up on jsfiddle, though.

/

Any help would be appreciated. Thanks in advance.

I'm not sure if this is possible, but I hope someone can direct me in the right direction to achieve what I wish to achieve.

I'm building a website around bootstrap from twitter; I use bootstrap collapse on a section box. This box isn't collapsed by default, but if a user pushes a button and collapses the box; I want this information to be stored in some kind of cache on the client side even after page refresh.

Let's say the user collapses them, and returns tomorrow; then they would still be collapsed until the user pushes the button again.

I won't include any of my code as I suspect there are more to this than only the added classes (I use the default one anyway), nor can I link to the project as it is not available outside of our intranet.

Here's a copy from bootstrap up on jsfiddle, though.

http://jsfiddle/B43hy/

Any help would be appreciated. Thanks in advance.

Share Improve this question asked Aug 5, 2012 at 17:20 arvdsnarvdsn 751 gold badge2 silver badges7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

You can try using either cookies or HTML5 LocalStorage. Both of these mechanisms store client-side data. You should pick one of them (and in the case of cookies, write a get/set function).

Then you need to give each collapse element an id that is unique across the entire site, so that it doesn't conflict with any elements on other pages. Every time a user collapses an element, you save the id on the client. Every time a user un-collapses an element, you remove the id from storage. You can use the events that Bootstrap throws, like so:

$('.collapse').on('hidden', function() {
      // store this.id
}).on('shown', function() {
      // delete this.id
});

When you load a page, inside your $( document ).ready function you should add the following check:

$(document).ready(function(){
    $(".collapse").collapse().each(function(){
        if( isStored( this.id ) ) {
            $( this ).collapse( 'hide' );
        }
    });
});​

This will check to see if any of the collapse elements should be closed and hides them if so. The isStored method should be the one checking the cookies or LocalStorage to see if the id is there.

If you have a lot of these collapses on your site, then you will probably use a lot of ids for them. In this case, cookies might be a bad option because they will get transmitted to the server on every request, slowing down the connection with no real purpose. LocalStorage doesn't suffer from this flaw, and it's easier to user overall, but (like many HTML5 features) it only works in newer browsers.

I use jQuery storage api plugin, which allows you to put in cookie, local storage, namespace and session storage.

My code to remember the state of Bootstrap collapse fields is this easy:

 // Open/close the collapse panels based on history
    var storage  = $.localStorage;

    $('.collapse').on('hidden.bs.collapse', function () {
        storage.remove('open_' + this.id);
    });

    $('.collapse').on('shown.bs.collapse', function () {
        storage.set('open_' + this.id, true);
    });

    $('.collapse').each(function () {
        // Default close unless saved as open
        if (storage.get('open_' + this.id) == true) {
            $(this).collapse('show');
        }
    });
发布评论

评论列表(0)

  1. 暂无评论