无法使用PHP

时间:2016-03-04 12:24:31

标签: php jquery twitter-bootstrap

概述

我有一个带有li元素的导航栏。我将类active添加到与变量$page对应的元素中。

对于每个列表元素,我放置与此类似的代码:

<li<?= $page=="new" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=new">New event</a></li>

这样,使用此示例,如果$page"new",我会将active类添加到li元素。

这适用于顶级列表项以及下拉列表。

问题

我希望下拉列表子元素的父元素在其中一个子元素处于活动状态时也显示active类。

要使用包含页面值"new""existing""gateway"的下拉列表执行此操作,我会使用:

<li class="dropdown<?= $page=="new"||$page=="existing"||$page="gateway" ? " active" : null ?>">

然而,这不起作用;在Chrome开发人员工具中检查元素时,唯一的类是dropdown1,并且任何其他添加的类都已消失。

在Chrome开发者工具中手动添加active类会产生正确的结果。

完整代码

<div id="navbar" class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li<?= $page=="dashboard" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=dashboard">Dashboard</a></li>
        <li class="dropdown<?= $page=="new"||$page=="existing"||$page="gateway" ? " active" : null ?>">
            <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Events <span class="caret"></span></a>
            <ul class="dropdown-menu">
                <li<?= $page=="new" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=new">New event</a></li>
                <li<?= $page=="existing" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=existing">Existing events</a></li>
                <li<?= $page=="gateway" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=gateway">Gateway</a></li>
            </ul>
        </li>
        <li><a href="<?= $rootUrl ?>?logout=true">Sign out</a></li>
    </ul>
</div>

问题

当其子元素之一具有active类时,如何使下拉列表的父元素具有active类?

1 个答案:

答案 0 :(得分:1)

"=" $page="gateway" $page=="gateway" <li class="dropdown<?= $page=="new"||$page=="existing"||$page="gateway" ? " active" : null ?>"> 错过了double u; srand(unsigned(time(NULL))); for(int i = 0; i < 10; i++){ u = (double)rand()/(RAND_MAX+1) + (rand()%101); //Acquire random number, turn it into decimal and then add it to another number between 0-100. u *= 10; //Shift numbers left to remove first decimal from round. round(u); //Round to nearest whole number. u /= 10; //Shift right to return the first decimal. cout<<u<<" "<<flush; }

public class CustomerAdapter extends ArrayAdapter<ShareWithArticle> {
    private final Context mContext;
    private final List<ShareWithArticle> mDepartments;
    private final List<ShareWithArticle> mDepartments_All;
    private final List<ShareWithArticle> mDepartments_Suggestion;
    private final int mLayoutResourceId;
SharedFragment fragment;
    public CustomerAdapter(Context context, int resource, List<ShareWithArticle> departments,SharedFragment fragment) {
        super(context, resource, departments);
        this.mContext = context;
        this.mLayoutResourceId = resource;
        this.mDepartments = new ArrayList<>(departments);
        this.mDepartments_All = new ArrayList<>(departments);
        this.mDepartments_Suggestion = new ArrayList<>();

        this.fragment = fragment;
    }

    public int getCount() {
        return mDepartments.size();
    }

    public ShareWithArticle getItem(int position) {
        return mDepartments.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            if (convertView == null) {
                LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                convertView = inflater.inflate(mLayoutResourceId, parent, false);
            }
            ShareWithArticle department = getItem(position);
            final TextView name = (TextView) convertView.findViewById(R.id.textView2);
            final TextView userId = (TextView) convertView.findViewById(R.id.textView3);
            name.setText(department.getUsrCompNm());
            userId.setText(department.getUsrID() + "");
            convertView.setOnClickListener(new Listener(name,userId));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }
class Listener implements View.OnClickListener
    {
    TextView name;
     TextView userId;

    Listener(TextView name, TextView userId)
    {
    this.name = name;
    this.userId = userId;

    }

     @Override
        public void onClick(View v) {

                           String userNm  =   name.getText().toString();
                    String userIdtxt = userId.getText().toString();

                    //create a method in fragment class and pass the data using tthe object.

                fragment.setData(userNm,userIdtxt);

        }
    }
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            public String convertResultToString(Object resultValue) {
                return ((ShareWithArticle) resultValue).getUsrCompNm();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                if (constraint != null) {
                    mDepartments_Suggestion.clear();
                    for (ShareWithArticle department : mDepartments_All) {
                        if (department.getUsrCompNm().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                            mDepartments_Suggestion.add(department);
                        }
                    }
                    FilterResults filterResults = new FilterResults();
                    filterResults.values = mDepartments_Suggestion;
                    filterResults.count = mDepartments_Suggestion.size();
                    return filterResults;
                } else {
                    return new FilterResults();
                }
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                mDepartments.clear();
                if (results != null && results.count > 0) {
                    // avoids unchecked cast warning when using mDepartments.addAll((ArrayList<Department>) results.values);
                    List<?> result = (List<?>) results.values;
                    for (Object object : result) {
                        if (object instanceof ShareWithArticle) {
                            mDepartments.add((ShareWithArticle) object);
                        }
                    }
                } else if (constraint == null) {
                    // no filter, add entire original list back in
                    mDepartments.addAll(mDepartments_All);
                }
                notifyDataSetChanged();
            }
        };
    }

}