我已加载 select2 这样的数据:
$data = ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(),'group_id', 'group_name');
echo $form->field($model, 'group_id')->widget(Select2::classname(), [
'data' => $data,
'model' => $model,
'language' => 'en',
'options' => ['placeholder' => Yii::t('modules','Pilih Kelompok')],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
],
])->label('Kelompok');
$data
变量返回结果:
Array
(
[1] => Tanpa Kategori
[3] => Bisnis
[4] => Kawan
[5] => Bisnis Kerang
[6] => Bisnis Selang
[99] => Keluarga
)
和 select2 正常工作,但我无法显示所选值或initial value
。我错过了什么吗?
答案 0 :(得分:2)
您在private static final int REQUEST_CAMERA = 0;
private static final int CAMERA_REQUEST = 1;
private static final int RESULT_LOAD_IMAGE = 2;
private static final int GALLERY_KITKAT_INTENT_CALLED = 3;
private static final int CROP_IMAGE_GALLERY = 4;
private static final int CROP_IMAGE_CAMERA = 5;
private String FilePath;
private Uri ImageUri, croppedImageUri;
private File pic_file;
private void SelectImage()
{
final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Remove", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
builder.setTitle("Upload Photo");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pic_file = new File(Environment.getExternalStorageDirectory(),
"tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
ImageUri = Uri.fromFile(pic_file);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
ImageUri);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} else if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE);
} else if (options[item].equals("Remove")) {
ivProfilePic.setImageResource(R.mipmap.ic_profile_pic);
FilePath = "";
dialog.dismiss();
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
/*
* After capture Image or Select Image from Gallary On activityResult Call
*
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
{
try {
CropImage();
} catch (Exception e) {
rootLogger.error(Utility.createLogableFormat(Utility.getStringStackTrace(e)));
}
}
else if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
try {
ImageUri = data.getData();
CropImage();
} catch (Exception e) {
rootLogger.error(Utility.createLogableFormat(Utility.getStringStackTrace(e)));
}
}
else if (requestCode == CROP_IMAGE_CAMERA) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
if (extras != null) {
FilePath = ImageUri.getPath();
File f = new File(FilePath);
Bitmap bmp_post_news = decodeSampledBitmapFromResource(f.getAbsolutePath(),120,120);
ivProfilePic.setImageBitmap(bmp_post_news);
}
}
}
else if (requestCode == CROP_IMAGE_GALLERY) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
if (extras != null) {
FilePath = croppedImageUri.getPath();
Bitmap bmp_post_news = decodeSampledBitmapFromResource(FilePath,120,120);
ivProfilePic.setImageBitmap(bmp_post_news);
}
}
}
}
public Bitmap decodeSampledBitmapFromResource(String Filepath,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(Filepath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(Filepath, options);
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
int inSampleSize = 1;
final int height = options.outHeight;
final int width = options.outWidth;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
protected void CropImage() {
CropImageIntentBuilder cropImage = new CropImageIntentBuilder(3, 4,120,120, ImageUri);
//cropImage.setOutlineColor(0xFF03A9F4);
cropImage.setSourceImage(ImageUri);
cropImage.setCircleCrop(true);
cropImage.setOutlineCircleColor(Color.WHITE);
cropImage.setOutputQuality(100);
Intent intent = cropImage.getIntent(this);
//intent.putExtra("return-data", false);
startActivityForResult(intent, CROP_IMAGE_CAMERA);
}
中添加tags
属性,以便进行多项选择,例如....
pluginOptions
您展示Demo
答案 1 :(得分:0)
看一下kartik \ base \ InputWidget第190行的代码:
if ($this->hasModel()) {
$this->name = !isset($this->options['name']) ? Html::getInputName($this->model, $this->attribute) : $this->options['name'];
$this->value = !isset($this->options['value'])? Html::getAttributeValue($this->model, $this->attribute) : $this->options['value'];
}
我发现,在使用AJAX加载数据时,应在options [value]中设置初始多个值,如下所示:
<?= $form->field($model, $attribute)->widget(Select2::className(), [
'initValueText' => $initText, // array of text to show in the tag for the selected items
'options' => [
'placeholder' => 'Any text you want ...',
'multiple' => true,
'class' => 'form-control',
'value' => $initIds, // array of Id of the selected items
],
而initValueText旁边的设置值导致array_combine错误
答案 2 :(得分:0)
尝试使用这样的..在更新时,我们需要在1变量和1变量中的所有值中选择已经选择的值..并将其发送到select2。
$query = NewsTags::find()->where(['news_id' => $model->id])->all();
$services = array();
$services_id_list = array();
foreach ($query as $ds) {
$tag_id = $ds->tag_id;
$tag_name = Tags::findOne($tag_id)->tag_name;
$services[$ds->tag_id] = $tag_name;
array_push($services_id_list, $ds->tag_id);
}
$data= ArrayHelper::map(Tags::find()->where([])->all(),'id','tag_name');
echo Select2::widget([
'name' => 'NewsTags[tag_id][]',
'id' => 'newstags-tag_id',
'value' => $services_id_list,
'data' => $data,
'maintainOrder' => true,
'options' => [
'placeholder' => 'Select a Service ...',
'multiple' => true
],
'pluginOptions' => [
'tags' => true,
'maximumInputLength' => 10
],
]);
这里NewsTags [tag_id] []是模型及其专栏。我们不会直接在此处调用 $ model-&gt;属性