jQuery Autocomplete is not working PHP/HTML/JS
I have a textbox which I wanted to be auto-complete using jQuery Autocomplete plugin. I use
jquery-1.7.1.min.js and jquery-ui-1.8.17.custom.min.js. My other fields are okay except my role field (textbox). Even though I tried to hard-code my values
$html .= 'var employeenames = ["Emp0", "Emp1", "Emp2", "Emp3"];' . PHP_EOL;
It’s not working, but when I viewed the source code, The values were there.
Please have a look at my codings:
protected function jsGenerateResourcesAutocomplete(){
$employeeNames = $this->employeeNames;
$html = "";
$html .= '<script>' . PHP_EOL;
$html .= 'var employeenames = ["Emp0", "Emp1", "Emp2", "Emp3"];' . PHP_EOL;
$html .= '$(function() {' . PHP_EOL;
$html .= '$("#resource-input input:text").each(function() {' . PHP_EOL;
$html .= '$(this).autocomplete({source: employeenames});' . PHP_EOL;
$html .= '})' . PHP_EOL;
$html .= '})' . PHP_EOL;
$html .= '</script>' . PHP_EOL;
return $html;
}
protected function jsGenerateSkillAutocomplete() {
$skills = $this->skills;
$html = "";
$html .= '<script>' . PHP_EOL;
$html .= 'var skills = [' . PHP_EOL;
foreach($skills as $skill) {
$html .= '"' . $skill->getSkillName() . '",' . PHP_EOL;
}
$html .= '];' . PHP_EOL;
$html .= '$(function() {' . PHP_EOL;
$html .= '$("#skill-input input:text").each(function() {' . PHP_EOL;
$html .= '$(this).autocomplete({source: skills});' . PHP_EOL;
$html .= '})' . PHP_EOL;
$html .= '})' . PHP_EOL;
$html .= '</script>' . PHP_EOL;
return $html;
}
protected function roleAssignment() {
$roles = $this->roles;
$numResources = $this->numResources;
$numWeeks = $this->numWeeks;
#$employeeNames = $this->projectEmployeeRole;
$employeeRoleIds = $this->employeeRoleIds;
$employeeNames = $this->employeeNames;
$employeeWorkload = $this->employeeWorkload;
is_array($employeeNames);
$html .= '<fieldset id="role-assignment">' . PHP_EOL;
if( !strcmp($_GET["page"], "createballpark") || !strcmp($_GET["page"], "editballpark") ) {
$html .= '<legend>Ballpark Role Assignment</legend>' . PHP_EOL;
}
else {
$html .= '<legend>Project Role Assignment</legend>' . PHP_EOL;
}
$html .= '<div>Assigns roles to resources. Put "null" or blank on the field to save
the role in the table without a resource.</div>';
$html .= HTML::javascriptButton("addrow", "Add Resource", "appendRow('roleTable')") . PHP_EOL;
$html .= "";
$html .= '<table id="roleTable" class="ui-widget">';
# Flag to check in normal roles looping if manager has been found in array
$isManagerFound = false;
# The row of the manager in the arrays, set alongside $isManagerFound = true;
$managerRow;
# Generate header and first row (Project Manager row)
// TESTING/////////////////////////////////////////////////////////////////////////////////////////////
//for($row = -1; $row < $numResources; $row++ ) {
for($row = -1; $row < $numResources; $row++) { // I made this to 3 only
$html .= "<tr>" . PHP_EOL;
if($row == -1) {
# Generate Header
$html .= "<td></td>" . PHP_EOL; # Remove Row Button
$html .= "<td>Role</td>" . PHP_EOL;
$html .= "<td></td>" . PHP_EOL; # Search Button
$html .= "<td>Resource</td>" . PHP_EOL;
$html .= "<td></td>" . PHP_EOL; # View Workload Button
}
else {
# Generate role dropdown and resources
$rowCounter = $row + 1;
# Delete Row Button
$html .= '<td>';
if($row != 0)
$html .= '<input type="button" class="delete-button" onclick="deleteRow(\'roleTable\', this)"/>';
$html .= '</td>';
# Role Dropdown and Resource Textbox
# First iteration: Find the Project Manager on the array
if($row == 0) {
$html .= "<td>" . PHP_EOL;
# Role dropdown (select only the Project Manager but load everything for perfect width)
$html .= HTML::selectStart("role-".$rowCounter, true) . PHP_EOL;
foreach($roles as $role) {
$isSelected = false;
$roleId = $role->getRoleId();
$roleName = $role->getRoleName();
if($roleId == RoleKeys::MANAGER) //TODO For displaying project manager name on textbox
$isSelected = true;
$html .= HTML::option($roleId, $roleName, $isSelected);
}
$html .= HTML::selectEnd();
# Output "?" button
$html .= '<td><input type="button" class="search-user-button" onclick=""></td>' . PHP_EOL;
# Find the project manager if applicable
if($this->formStatus != self::CREATE &&
isset($employeeNames)) {
for($i = 0; $i < count($employeeRoleIds); $i++) {
if($employeeRoleIds[$i] == RoleKeys::MANAGER) {
$resourceName = $employeeNames[$i];
break;
}
}
}
# Create new project/ballpark
else {
# Load project manager (visitor name)
$resourceName = $this->managerName;
}
$html .= '<td class="resource-input">';
$postId = "resource-".$rowCounter;
$html .= HTML::inputText($postId, 20, $resourceName);
$html .= "</td>" . PHP_EOL;
}
# Other rows; it will skip the project manager
else {
$currentResource = $row - 1;
# Role Dropdown
$html .= "<td>" . PHP_EOL;
# If create form, will generate blank roles instead.
if($this->formStatus == self::CREATE) {
# Get the manager index in the roles array to skip
if(!isset($managerIndex)) {
$managerIndex = $this->getManagerIndexFromRoles($roles);
}
# Skip the project manager role
if($currentResource >= $managerIndex) {
$currentResource += 1;
}
# Generate role dropdown
$html .= $this->generateRoleRow($currentResource);
# Output "?" button
$html .= '<td><input type="button" class="search-user-button" onclick=""></td>' . PHP_EOL;
# Resource textbox (employee names)
$html .= '<td class="resource-input">';
$postId = "resource-".$rowCounter;
$html .= HTML::inputText($postId, 20, null);
$html .= "</td>" . PHP_EOL;
# Output ViewWorkload button
$html .= '<td><input type="button" class="workload-button" onclick="loadViewWorkloadPage(\''.$postId.'\')"></td>';
$html .= "</tr>" . PHP_EOL;
continue; # Skip work weeks loop
}
# If the form is not create, get the role ID of current resource in array
if($this->formStatus != self::CREATE &&
isset($employeeRoleIds)) {
$roleIdEmployee = $employeeRoleIds[$currentResource];
}
# Adjust if current resource = manager and set flag to true
if($roleIdEmployee == RoleKeys::MANAGER ) {
$managerRow = $currentResource;
$isManagerFound = true;
}
# Adjust if manager has been found in array
if($isManagerFound) {
$currentResource += 1;
$roleIdEmployee = $employeeRoleIds[$currentResource];
}
# Generate role dropdown
$html .= HTML::selectStart("role-".$rowCounter) . PHP_EOL;
foreach($roles as $role) {
$isSelected = false;
# Do not display the Project Manager option
$roleId = $role->getRoleId();
if($roleId == RoleKeys::MANAGER)
continue;
# Set the option as selected in edit/error form
else if($roleId == $roleIdEmployee)
$isSelected = true;
$roleName = $role->getRoleName();
$html .= HTML::option($roleId, $roleName, $isSelected) . PHP_EOL;
}
$html .= HTML::selectEnd() . PHP_EOL;
$html .= "</td>" . PHP_EOL;
# Output "?" button
$html .= '<td><input type="button" class="search-user-button" onclick=""></td>' . PHP_EOL;
# Resource textbox (employee names)
$html .= '<td class="resource-input">';
if($this->formStatus != self::CREATE &&
isset($employeeNames)) {
$resourceName = $employeeNames[$currentResource];
# Force blank if "null" employee
if(!strcmp($resourceName,"null")) {
$resourceName = null;
}
}
else {
$resourceName = null;
}
$postId = "resource-".$rowCounter;
$html .= HTML::inputText($postId, 20, $resourceName);
$html .= "</td>" . PHP_EOL;
}
# Output View Workload button
$html .= '<td><input type="button" class="workload-button" onclick="loadViewWorkloadPage(\''.$postId.'\')"></td>' . PHP_EOL;
}
# This loop-section generates the dynamic weeks columns
for($week = 0; $week < $numWeeks; $week++) {
# Generate Header
if($row == -1) {
$html .= '<td>WK '.($week+1).'</td>';
}
# Generate Workload
else {
if ($row == 0) {
# In case the Project Manager was the last entry in the array
if(!$isManagerFound)
$managerRow = $numResources - 1;
# Generate project manager workload
$value = $employeeWorkload[$managerRow][$week];
}
else {
# Generate developer workload
$value = $employeeWorkload[$currentResource][$week];
}
$value = round($value, 2);
if($value == 0)
$value = null;
$workloadId = 'workload-'.($row+1).'-'.($week+1);
$html .= '<td>'.HTML::inputText($workloadId, 2, $value).'</td>';
}
}
$html .= "</tr>" . PHP_EOL;
}
$html .= "</table>" . PHP_EOL;
$html .= "</fieldset>" . PHP_EOL;
return $html;
}
And my javascript code:
function createRoleResource(cell) {
var id = "resource-" + numRows;
var input = document.createElement('input');
input.type = 'text';
input.id = id;
input.name = id;
input.value = '';
input.size = 20;
cell.appendChild(input);
$("#"+id).autocomplete({source: employeenames});
// setEventHandler(id);
}
function createSkillTextbox(cell) {
var skillId = "skill-" + numSkills;
var input = document.createElement('input');
input.type = 'text';
input.id = skillId;
input.name = skillId;
input.value = '';
input.size = '55';
cell.appendChild(input);
$("#"+skillId).autocomplete({source: skills});
}
If you know what’s wrong with these codings, please let me know. Thanks.
Total Views: 4 Today Views: 0















