如何修改PagingNavigator中Wicket链接生成的标记?

时间:2011-02-14 19:07:36

标签: wicket

我正在使用自定义的PagingNavigator组件,我正在寻找一种方法来更改为第一页,上一页和当前页面分页项生成的标记。

这是我的PaginingNavigator:

 <wicket:panel>
<ul class="pagination">
    <li class="first">
        <a wicket:id="first">&lt;&lt;</a>
    </li>
    <li class="prev">
        <a wicket:id="prev">&#060;</a>
    </li>
    <li wicket:id="navigation" class="page">
        <a wicket:id="pageLink" href="#">
            <span wicket:id="pageNumber">5</span>
        </a>
    </li>
    <li class="dots">...</li>
    <li wicket:id="lastPage" class="jump"></li>
    <li class="next">
        <a wicket:id="next">&#062;</a>
    </li>
    <li class="last">
        <a wicket:id="last">&gt;&gt;</a>    
    </li>
</ul>

在非活动项目的LI(最初,前一页,第一页和当前页面)内生成的标记是这样的:

<span title="Item Title" id="RandomID">
    <em>
      <span>ItemText</span>
    </em>
</span>

这使得非活动分页项的内容和当前页面的样式变得非常困难。我更喜欢它为非活动项输出类似的内容:

<a href="#" title=ItemTitle" id="RandomID" class="inactive">ItemText</a>

这是当前页面:

 <a href="#" title=ItemTitle" id="RandomID" class="currentPage">ItemText</a>

我还注意到,当你开始分页结果时,它会将SPAN标签插入到A标签中,并且因为它们上没有特定的类,所以正确设置样式会非常繁琐。

我一直在我们的代码库中挖掘,找不到我们要指定的任何地方,但由于我是一个前端人员,我很容易忽视。就我所知,它们似乎是标准的PagingNavigationLinks。

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

如果你看这里:

Apache Wicket - Search Engine Optimization

在“Make Paging Stateless”下,您可以了解如何覆盖分页导航的各个部分。

答案 1 :(得分:2)

您可以扩展核心PagingNavigator

import org.apache.wicket.markup.html.navigation.paging.IPageable;
import org.apache.wicket.markup.html.navigation.paging.IPagingLabelProvider;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;

class MyPagingNavigator extends PagingNavigator {

    public MyPagingNavigator(String id, IPageable pageable) {
        super(id, pageable);
    }

    public MyPagingNavigator(String id, IPageable pageable, IPagingLabelProvider labelProvider) {
        super(id, pageable, labelProvider);
    }



}

然后您创建一个MyPagingNavigator.html,您可以在其中进行更改。但请确保您不从MyPagingNavigator.html中删除任何组件

您可以使用wicket源中的原始内容(src / wicket / src / main / java / org / apache / wicket / markup / html / navigation / paging / PagingNavigator.html):

<?xml version="1.0" encoding="UTF-8" ?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<html xmlns:wicket>
<body>
  <wicket:panel>
    <a wicket:id="first">&lt;&lt;</a>&nbsp;<a wicket:id="prev">&lt;</a>
    <span wicket:id="navigation">
          <a wicket:id="pageLink" href="#"><span wicket:id="pageNumber">5</span></a>
    </span>
    <a wicket:id="next">&gt;</a>&nbsp;<a wicket:id="last">&gt;&gt;</a>
  </wicket:panel>
</body>
</html>

答案 2 :(得分:0)

要解决这个问题,我必须扩展一些课程。特别是,我需要覆盖行为 org.apache.wicket.markup.html.link.AbstractLink.disableLink(最终的ComponentTag标签)

在AbstractLink的disableLink函数中,“a”标记更改为“span”,并且href和onclick属性被删除。我首先回溯到PagingNavigator并将其扩展如下:

public class DreamPagingNavigator extends PagingNavigator {

    public DreamPagingNavigator(String id, IPageable pageable) {
        super(id, pageable);
    }

    public DreamPagingNavigator(String id, IPageable pageable, IPagingLabelProvider labelProvider) {
        super(id, pageable, labelProvider);
    }

    @Override
    protected PagingNavigation newNavigation(final String id, final IPageable pageable,
            final IPagingLabelProvider labelProvider) {
        return new DreamPagingNavigation(id, pageable, labelProvider);
    }

    @Override
    protected AbstractLink newPagingNavigationIncrementLink(String id, IPageable pageable,
            int increment) {
        return new DreamPagingNavigationIncrementLink<Void>(id, pageable, increment);
    }

    @Override
    protected AbstractLink newPagingNavigationLink(String id, IPageable pageable, int pageNumber) {
        return new DreamPagingNavigationLink<Void>(id, pageable, pageNumber);
    }

}

对于我的“Dream”主题,这允许我覆盖newPagingNavigationLink,newPagingNavigationIncrementLink等.PagingNavigationLink(et.al)的我的实现然后修改了禁用行为,如下所示:

public class DreamPagingNavigationLink<T> extends PagingNavigationLink<T> {

    public DreamPagingNavigationLink(String id, IPageable pageable, long pageNumber) {
        super(id, pageable, pageNumber);
    }

    @Override
    protected void disableLink(final ComponentTag tag)
    {
        // if the tag is an anchor proper
        if (tag.getName().equalsIgnoreCase("a") || tag.getName().equalsIgnoreCase("link") ||
            tag.getName().equalsIgnoreCase("area"))
        {
            // Remove any href from the old link
            tag.remove("href");
            tag.remove("onclick");

        }
        // if the tag is a button or input
        else if ("button".equalsIgnoreCase(tag.getName()) ||
            "input".equalsIgnoreCase(tag.getName()))
        {
            tag.put("disabled", "disabled");
        }
    }

}

我只是删除了href和onclick属性,而不是将“a”标记更改为“span”。

现在我得到一个没有href的锚标记。

相关问题