我的更新方法工作不幸的是它总是更新错误的数据,在这种情况下,它总是更新我的数据库列表中的最后一个数据。我相信这是因为我的模态框指向$ user-> id,它总是指向最后一个id,因为我在顶部使用了for循环,有没有办法将它指向所选的id而不是?
view.blade.php
public void pickImageOnClick(View view){
shouldSwitch = false;
//startActivityForResult(i, PHOTO_FROM_MEMORY_REQUESTED);
Intent localIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
localIntent.setType("image/*");
localIntent.setAction(Intent.ACTION_GET_CONTENT);
localIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(localIntent, PHOTO_FROM_MEMORY_REQUESTED);
}
private File createGameFile(String part, String ext) throws Exception
{
//Getting external storage path, and .temp directory in it.
File mainDir= Environment.getExternalStorageDirectory();
mainDir=new File(mainDir.getAbsolutePath()+MAIN_FOLDER);
//If .temp does not exists, it is created.
if(!mainDir.exists()) mainDir.mkdir();
return new File(mainDir.getAbsolutePath().toString(), part+ext);
// return File.createTempFile(part, ext, mainDir);
}
public void playOnClick(View View){
Intent intent = new Intent(this, PuzzleActivity.class);
String[] gameSizes = getResources().getStringArray(R.array.gamesizes);
intent.putExtra(EXTRA_GAMESIZE, gameSizes[gameSizeSpinner.getSelectedItemPosition()]);
intent.putExtra(EXTRA_IMGURI, imageUri);
//Toast.makeText(getApplicationContext(), "Hi"+imageUri, 400).show();
int orientation; //Determining screen orientation.
orientation = (selectedImage.getWidth()>selectedImage.getHeight()) ?
GameBoard.ORIENTATION_HORIZONTAL : GameBoard.ORIENTATION_PORTRAIT;
intent.putExtra(EXTRA_BOARD_ORIENTATION, orientation);
// String str = orientation == 0 ? "PORTRAIT" : "HORIZONTAL";
// Log.d("KAMIL", "Orientation : " + str);
shouldSwitch = true;
startActivity(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PHOTO_FROM_MEMORY_REQUESTED && resultCode == RESULT_OK){
//updateSelectedPicture(data.getData());
try
{
imageUri = data.getData();
//scaleImage(uri, 2048);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 9;
//Opening the input stream and receiving Bitmap.
InputStream imageStream = getContentResolver().openInputStream(imageUri);
//selectedImage = BitmapFactory.decodeStream(imageStream);
selectedImage = BitmapFactory.decodeStream(imageStream,null,options);
//.selectedImage = BitmapFactory.decodeFile(i);
//Bitmap thumbnail = BitmapFactory.decodeFile(mFileTemp.getPath());
if (selectedImage.getHeight() > 1200)
selectedImage = Bitmap.createScaledBitmap(selectedImage, selectedImage.getWidth() * 1200 / selectedImage.getHeight(), 1200, false);
//Toast.makeText(getApplicationContext(), ""+selectedImage.getHeight(), 400).show();
System.gc();
playButton.setEnabled(true);
}
catch (FileNotFoundException localFileNotFoundException)
{
localFileNotFoundException.printStackTrace();
}
}
答案 0 :(得分:1)
你的模态是引用$ user对象,但它不在你的foreach循环中。
特别是这一行:
<form class="form-horizontal" role="form" action="/manage_accounts/{{ $user->id }}" novalidate>
您可以为模态弹出按钮注册onClick事件,该按钮会抓取用户ID的隐藏输入字段并动态更新操作URL。或者,您可以让操作URL相同并处理逻辑服务器端。这种方法将为您要更新的用户ID提供隐藏的输入字段,但处理URL结构要清晰得多。
编辑:
Javascript示例:
<script type="text-javascript">
$(function() {
$('.btn--edit').on('click', function() {
var formAction = $('.form-horizontal').attr('action').replace(/(?!.*/).*, '');
var userId = $(this).closest('[name=user_id]').val();
$('.form-horizontal').attr('action', formAction + '/' + userId);
});
});
</script>
这要求您更新类名为.btn - edit
的模态按钮<button class="btn btn-sm btn-warning btn--edit" type="button" data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
这也要求你在foreach的某个地方添加一个隐藏的输入。我选择了用户信息类。
<input type="hidden" name="user_id" value="{{ $user->id }}" />
答案 1 :(得分:0)
所以这就是我如何使用更新方法。
httpSession.getAttribute("loggedUser");//return type is Object here
使用Javascript:
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/" novalidate>
<input type="hidden" name="_method" value="PUT">
类名为.btn的模态按钮 - 编辑,
<script type="text/javascript">
$(function() {
$('.btn--edit').on('click', function(e) {
var userId = $(this).attr('data-for');
var formAction = "/manage_accounts/" + userId;
$('.form-horizontal').attr('action', formAction);
});
</script>