RadPHPでListBoxに項目を追加する方法。
ListBoxの項目であるItemsプロパティは配列(array)なので、配列(array)を代入することで一括して設定できる。
$items = array();
$items['key1'] = 'value1';
$items['key2'] = 'value2';
$this->ListBox1->Items = $items;
ListBoxに新しい項目を追加するときは、AddItem()メソッドを使用する。
integer AddItem( mixed $item, [mixed $object = null], [mixed $itemkey = null])
AddItem()の引数に1番目が項目名、2番目がオブジェクト、3番目がキーを受け取り、項目数を返す。
$this->ListBox1->AddItem($this->Edit1->Text, null, 'key3');
ListBoxの項目を削除するには、Itemsプロパティの配列を操作する。
Itemsプロパティが配列の時は、インデックスを指定して削除する。
//選択されている項目を削除する
$index = $this->ListBox1->ItemIndex; //選択されている項目のインデックス
$items = $this->ListBox1->Items;
array_splice($items, $index, 1);
$this->ListBox1->Items = $items;
Itemsプロパティが連想配列の時は、キーを指定して削除する。
$key = $this->ListBox1->ItemIndex; //選択されている項目のキー
$items = $this->ListBox1->Items;
unset($items[$key]);
$this->ListBox1->Items = $items;
すべての項目をクリアするときは、Claer()メソッドを使用する。
$this->ListBox1->Clear();
JavaScriptで項目を操作するときは、options属性を編集する。
項目を追加する
function Button1JSClick($sender, $params)
{
?>
//begin js
var opt = document.createElement("option");
opt.text = '表示文字';
opt.value = 'キー';
document.getElementById("ListBox1").options.add(opt);
return false;
//end
<?php
}
選択されている項目を削除する
function Button1JSClick($sender, $params)
{
?>
//begin js
var index = document.getElementById("ListBox1").selectedIndex;
document.getElementById("ListBox1").options[index] = null;
return false;
//end
<?php
}