Is there any way to Implement Chained Select in Forms

Feels free to drop any suggestions of what should be put in to the next release of SLiMS.

Moderators: wynerst, buitenzorg812, purwoko, sutriadi, ido, Dicarve, ridorido

Post Reply
Annu
Posts: 36
Joined: 12 Oct 2012, 14:38
Location: Islamabad
Contact:

Is there any way to Implement Chained Select in Forms

Post by Annu » 21 Mar 2013, 16:30

Please guide me how to Implement Chained Select using available form element

$form->addSelectList('topic', __('Title'), $abc_options, $ud_d['biblioId']);

Annu
Posts: 36
Joined: 12 Oct 2012, 14:38
Location: Islamabad
Contact:

Re: Is there any way to Implement Chained Select in Forms

Post by Annu » 29 Mar 2013, 11:26

Please reply

User avatar
buitenzorg812
Posts: 830
Joined: 20 Sep 2011, 14:18
Location: Jakarta, Indonesia
Contact:

Re: Is there any way to Implement Chained Select in Forms

Post by buitenzorg812 » 29 Mar 2013, 22:33

technically i don't know to answer this. let me try to ask in bahasa. ada yang temen2 yang bisa bantu? mas jushadi atau ghq?

User avatar
hqm
Posts: 21
Joined: 26 Mar 2013, 19:17

Re: Is there any way to Implement Chained Select in Forms

Post by hqm » 30 Mar 2013, 08:29

bikin select yang saling ngikut sama lain, misalnya milih propinsi, setelah propinsi kepilih nanti walikota/kabupaten aja yang ada di select selanjutnya, begitu juga pas select walikota/kabupaten kepilih, kecamatan di kabupaten itu aja yang tampil, dan seterusnya... dan seterusnya sampe nomer rumah ... #lebay ah ..

berhubung saya masih explore2 source code, jadi belum bisa ikutan nimbrung ngasih solusi, bantu translate ajah :D

User avatar
buitenzorg812
Posts: 830
Joined: 20 Sep 2011, 14:18
Location: Jakarta, Indonesia
Contact:

Re: Is there any way to Implement Chained Select in Forms

Post by buitenzorg812 » 30 Mar 2013, 10:54

sekalian jelasin dalam bahasa inggris donq mas brow :).

User avatar
hqm
Posts: 21
Joined: 26 Mar 2013, 19:17

Re: Is there any way to Implement Chained Select in Forms

Post by hqm » 31 Mar 2013, 20:51

Annu wrote:Please guide me how to Implement Chained Select using available form element

$form->addSelectList('topic', __('Title'), $abc_options, $ud_d['biblioId']);

The logic is load a Select with an array, and give an $str_elmnt_attr with an onchange method to load another list based on selected option value. To make it more efficient (not too much request, i'll load using Ajax request)

example: Add additional fields Country and City in a Membership Form. Country was loaded within the form, and city is loaded as the Country selector was changed. (I use JSON service from http://www.geonames.org/ to load Country and City, please refer to its documentation for details).

1. Add some field in a Membership Form (after Address field)

Code: Select all

 $form->addTextField('textarea', 'memberAddress', __('Address'), $rec_d['member_address'], 'rows="2" style="width: 100%;"');

	//request country list from geonames.org using CURL function, and store it in an array and make it as $array_option parameter in Country list
	$curl_init  = curl_init('http://api.geonames.org/countryInfoJSON?username=hqms');
	curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
	$curl_res = curl_exec($curl_init) ;

	$country_list = json_decode($curl_res);

	foreach ($country_list->geonames as $country ) {
		$_country[]= array($country->geonameId,$country->countryName);
	}

    // add country field, load the city using javascript function
    $form->addSelectList('country_id', __('Country'), $_country,'','onchange="javascript:loadCity($(this).val(), \'#city_id\');"');

    //add city field
	$form->addSelectList('city_id', __('City'), array());
2. Create a javascript function loadCity, to load the cities in selected country and populate in the city selector (I add some function in updater.js)

Code: Select all

/**
 *
 * Load city from http://api.geonames.org/childrenJSON to city_selector_id Element.
 *
 * @param integer geocodeCountryID
 * @param string city_selector_id
 * @return void
 **/
function loadCity(geocodeCountryId, city_selector_id){
	//load using ajax get method the cities in a country by its geonameId
	$.get("http://api.geonames.org/childrenJSON?geonameId="+geocodeCountryId+"&username=hqms",
			function(data){

			//empty the selector
			$(city_selector_id).empty();

				//fill it up !!
				$.each(data.geonames, function(index, value){
					$(city_selector_id).append($('<option></option>').val(value.geonameId).html(value.name));
				});
			},
			"json");
 
}
Those two steps above is only for viewing Country and City fields, as you asked us. I haven't implemented for save or edit those fields to database functionality (I don't have intended to implement those for my SLiMS so I won't push the example to my Fork )

User avatar
jushadi
Posts: 35
Joined: 27 Sep 2011, 16:29
Location: Makassar
Contact:

Re: Is there any way to Implement Chained Select in Forms

Post by jushadi » 01 Apr 2013, 13:18

menarik mas, gimana kalau daftarnya bukan dari luar, jadi opsinya karena sedikit saja langsung ada pilihan opsi di kode misalnya

ada field merk favorit berupa select option nokia, samsung, apple

jika dipilih nokia maka pilihan select option nokia 3310, nokia N70 dll, dan seterusnya jika samsung tipe hp samsung, apple dll. (cuma contoh):

// field pilihan merk
$options[] = array('nokia', __('Nokia'));
$options[] = array('samsung', __('Samsung'));
$options[] = array('apple', __('Apple'));
$form->addSelectList('gadget_merk', __('Gadget Favorit'), $options, $sysconf['gadget_merk']);

// field pilihan tipe hp berdasarkan merk yang dipilih

$form->addSelectList('gadget_type', __('Type HP'), $options, $sysconf['gadget_type']);
mohon pencerahannya :)
Read!

User avatar
hqm
Posts: 21
Joined: 26 Mar 2013, 19:17

Re: Is there any way to Implement Chained Select in Forms

Post by hqm » 01 Apr 2013, 13:36

jushadi wrote:menarik mas, gimana kalau daftarnya bukan dari luar, jadi opsinya karena sedikit saja langsung ada pilihan opsi di kode misalnya

ada field merk favorit berupa select option nokia, samsung, apple

jika dipilih nokia maka pilihan select option nokia 3310, nokia N70 dll, dan seterusnya jika samsung tipe hp samsung, apple dll. (cuma contoh):

// field pilihan merk
$options[] = array('nokia', __('Nokia'));
$options[] = array('samsung', __('Samsung'));
$options[] = array('apple', __('Apple'));
$form->addSelectList('gadget_merk', __('Gadget Favorit'), $options, $sysconf['gadget_merk']);

// field pilihan tipe hp berdasarkan merk yang dipilih

$form->addSelectList('gadget_type', __('Type HP'), $options, $sysconf['gadget_type']);
mohon pencerahannya :)
masalah ini ada scripting disini yang harus dijalanin ya mas, 1. server-side scripting (pas lagi load pertama kali merk HP-nya), 2. client-side scripting, pas lagi populate tipe HP sesuai dengan merk yang dipilih.

kalo yang pertama itu bisa langsung di file php yang terlibat, kalo yang kedua, itu mau gak mau harus ada request ke server lagi (mau formatnya html, xml ataupun json) nanti di masukin ke selector kedua (dalam hal ini tipe HP).

atau .. kita masukin semua value merk+tipe HP ke array 2 dimensi di javascript.. nanti tinggal pilih aja sesuai dengan index yang dipilih .. (nah, kebayang kan kalo dijadiin satu script tuh nama negara+kota-kotanya ... :D )

User avatar
jushadi
Posts: 35
Joined: 27 Sep 2011, 16:29
Location: Makassar
Contact:

Re: Is there any way to Implement Chained Select in Forms

Post by jushadi » 01 Apr 2013, 19:18

trims infonya, dah solved
Read!

Annu
Posts: 36
Joined: 12 Oct 2012, 14:38
Location: Islamabad
Contact:

Re: Is there any way to Implement Chained Select in Forms

Post by Annu » 18 Apr 2013, 11:45

Regards :arrow: jushadi

Post Reply