Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CascadeDropDown #2261

Open
extremeshannon opened this issue Aug 6, 2021 · 1 comment
Open

CascadeDropDown #2261

extremeshannon opened this issue Aug 6, 2021 · 1 comment

Comments

@extremeshannon
Copy link

Hello I am new to jtable and like it so far. I am looking for an example of the options code for CascadeDropDown. I am using php and have not found server side code I need to populate the dropdown list from PHP/mySQL. Thanks

@misterparsons
Copy link

Firstly, here is a snippet of a jtable definition which here deals with product group and product types within the groups.

groupId: {
	title: 'Group',
	options: 'ajax/jt.productGroup.php?act=opt',
},
typeId: {
	title: 'Prod Type',
	dependsOn: 'groupId', 
	options: function (data) {
		if (data.source == 'list') {
			//Return url of all cities for optimization.
			//This method is called for each row on the table and jTable caches options based on this url.
			return 'ajax/jt.prodType.php?act=opt';
		}

		//This code runs when user opens edit/create form or changes country combobox on an edit/create form.
		//data.source == 'edit' || data.source == 'create'
		return 'ajax/jt.prodType.php?act=opt&group=' + data.dependedValues.groupId;
		},
},

You will see there are 3 different URLs which need to be supported in php

The first is the simplest for just a full list of product groups, so here is a snippet of php

$sql = "SELECT id AS Value, name AS DisplayText";
$sql .= " FROM product_group";

$result = $mysqli->query( $sql);
if ($result) {
	$buffer['Result'] = "OK";
	$buffer['Options'] = ''; // placeholder definition
	while ($row = $result->fetch_assoc()) {
		$buffer['Options'][] = $row;
	}
}
else {
	throw new Exception ( "MySQL query error: " . $mysqli->error); 
}

which demonstrates how to build the buffer for jtable

The 2nd and 3rd URLs use the same script with options. The first of the two gets all the product types, which is used in the list operation of jtable. The second gets just the product types for a specific group, and is used in the Add and Edit dialogues.


$sql = "SELECT id AS Value, name AS DisplayText";
$sql .= " FROM product_type";
if (isset($_REQUEST['groupId']) && strlen($_REQUEST['groupId'])>0) $sql .= " WHERE group_id=" . $_REQUEST['groupId'];

use the same query processing as the previous snippet.

This is just a reply to your question with demonstration code. Appropriate protection should be used to validate the groupId before using it in any sql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants