在我的页面上,用户可以选择多个项目。 我使用复选框并遍历用户的所有项目。 现在,我想向用户显示他在该页面上选择的所有项目的总价。 可以这么说,购物车的总价。
如何在用户单击它们的同一页面上访问我的商品的ID?我知道在控制器中我可以通过
访问它们Item.find(params[:items])
非常感谢:)
答案 0 :(得分:0)
您需要通过Javascript完成此操作。
一种方法是将ID存储在html元素的this标签中(在这种情况下为Activity
)
然后您可以在Javascript回调中以以下方式获取该值
public class Core_Activity extends AppCompatActivity implements
View.OnClickListener, Detailed_Group_Fragment.TransitionScheduler {
private static final String TAG = "Core_Activity";
private ViewPager viewPager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
postponeEnterTransition();
...
}
...
public void scheduleStartTransition(final View sharedView){
sharedView.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
Log.i(TAG,"The sharedView called onPreDraw.");
sharedView.getViewTreeObserver()
.removeOnPreDrawListener(this);
startPostponedEnterTransition();
return true;
}
}
);
}
或者如果您使用的是jQuery
Fragment
现在,您将需要使用Javascript访问商品的值(具有选定ID的商品的单独价格)。您可以通过将id到price的映射存储在js变量中来实现
public class Detailed_Group_Fragment extends Fragment {
private static final String TAG = "Detailed_Group_Fragment";
private static final String TRANSITION_NAME = "transition_name";
private static final String GROUP_BASE = "group_base";
TransitionScheduler transitionScheduler;
public interface TransitionScheduler{
void scheduleStartTransition(View sharedView);
}
public static Detailed_Group_Fragment newInstance(GroupBase groupBase, String transitionName){
Detailed_Group_Fragment detailed_group_fragment = new Detailed_Group_Fragment();
Bundle bundle = new Bundle();
bundle.putParcelable(GROUP_BASE, groupBase);
bundle.putString(TRANSITION_NAME,transitionName);
detailed_group_fragment.setArguments(bundle);
return detailed_group_fragment;
}
...
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final GroupBase groupBase = getArguments().getParcelable(GROUP_BASE);
String transitionName = getArguments().getString(TRANSITION_NAME);
final TextView name = view.findViewById(R.id.detail_group_name_txt);
name.setText(transitionName);
name.setTransitionName("hungry");
transitionScheduler.scheduleStartTransition(name);
TextView desc = view.findViewById(R.id.group_desc_txt);
desc.setText(groupBase.getDescription());
final ImageView image = view.findViewById(R.id.group_image);
if(groupBase.getImageURI()!=null && groupBase.getBytes()==null){
Log.i(TAG,"Fetching the photo for this group.");
mGroupViewModel.getImage(groupBase.getImageURI())
.addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
groupBase.setBytes(bytes);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
image.setImageBitmap(bitmap);
}
});
}
if(groupBase.getBytes()!=null){
Bitmap bitmap = BitmapFactory.decodeByteArray(groupBase.getBytes(),0,groupBase.getBytes().length);
image.setImageBitmap(bitmap);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
//We need to get an instance of the Class that will execute our interface method.
transitionScheduler = (TransitionScheduler) getActivity();
} catch (ClassCastException e) {
Log.e(TAG,"Class cast exception." + e.getMessage());
}
}
或者,您也可以将价格存储在每个复选框的数据属性中,并使用该值更新所选总价。