缓存List.js结果 - 在浏览器后退按钮

时间:2018-04-16 11:15:12

标签: javascript wordpress list.js acfpro

我有一个网站,我使用List.js来过滤For Sale和To Let属性。

JS就是这样:

var options = {
    valueNames: [ 'status' ],
};

var hackerList = new List('property-list', options);

我正在使用一个选择框来填充隐藏的输入,并使用值来过滤列表:

<script type="text/javascript">
    function changeValue() {
        var option = document.getElementById('filter').value;

        if (option == "Sale") {
            document.getElementById('field').value = "Sale";
            document.getElementById("field").focus();
        } else if (option == "Letting") {
            document.getElementById('field').value = "Letting";
            document.getElementById("field").focus();
        } else if (option == "") {
            document.getElementById('field').value = "";
            document.getElementById("field").focus();
        }
    }
</script>

我需要知道如何使用WebStorageCookies来存储结果,但无论我尝试第二次添加更多代码,过滤器都会停止工作。

当用户按下属性列表中的后退按钮到列表页面时,它需要显示他们已经过滤的结果。

有什么想法吗?

每个结果都列在li中。我正在使用Wordpress和ACF Pro来执行此操作:

        <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <li class="col-md-4 <?php if( has_term( 'For Sale', 'propertycategories' ) ) {?>sale <?php } ?><?php if( has_term( 'To Let', 'propertycategories' ) ) {?>let<?php } ?>">
        <p class="status hidden"><?php if( has_term( 'For Sale', 'propertycategories' ) ) {?>Sale<?php } ?><?php if( has_term( 'To Let', 'propertycategories' ) ) {?>Letting<?php } ?></p>

        <a href="<?php the_permalink(); ?>" class="property wow fadeIn">
            <div class="property-image-container <?php the_field('offer_status2'); ?>">
                <span class="badge"><?php the_field('offer_status2'); ?> </span>
                <img class="property-image" src="<?php the_field('property_image_1'); ?>" alt=" "/>
            </div>
            <div class="property-details-container">
                <span class="float-left"><?php the_title(); ?></span>
                <span class="float-right">£<?php echo number_format( get_field('price') ); ?>             <?php if( has_term( 'To Let', 'propertycategories' ) ) {?>PCM <?php } ?></span>
                <div class="clearfix"></div>
            </div>
        </a>
    </li>
<?php endwhile; endif; ?>

我希望这会有所帮助。再次感谢您的帮助,我们非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以使用window.history.pushState“存储”有关在网址中应用的过滤器的信息。例如:

import mysql.connector as mariadb

conn = mariadb.connect(host='localhost', user='***', password='***', database='***', port=3306)

cursor=conn.cursor()

sqlcmd = "UPDATE aix_resourcegroups SET aix_rg_current_host=%(current_node)s, aix_rg_dns=%(dns)s, aix_rg_cluster=%(cluster_name)s, aix_rg_ip=%(ip)s, aix_rg_label=%(label)s WHERE aix_rg_name=%(resourcegroup)s"

data={'cluster_name': 'mycluster', 'resourcegroup': 'ZUPSOAP', 'label': 'myrg', 'current_node': 'nodename', 'ip': '1.2.3.4', 'dns': 'myhost.anywhere.net'}

cursor.execute(sqlcmd, data)

conn.commit()
conn.close()

该网址将被添加到用户历史记录中,因此当他点击属性列表中的后退按钮时,该网址将包含之前“已存储”的参数。

因此,在这种情况下,参数将类似于:http://example.com/?filter=Sale

现在您可以使用PHP / JS来应用过滤器。