JQuery UI Dialog - only the first link works - php

I have a list of products from a particular supplier being displayed in a <table> populated with data from my mysql database.
Within this table, each product has a link that, when clicked, should show the product details in a ui-dialog, <div id = "detalhe_produto"></div>. The dialog does open, however, it only works only for the first item in the list. The other links of the following lines do not open the dialog.
Can anyone help me?
Here's the script I am using to open the ui-dialog:
<script type="text/javascript">
$(function(){
$('detalhe_produto').click(function(){
var selectedVal=$("#detalhe_produto").val();
var url="produto_detalhe.php?codigo="+selectedVal;
var dialog = $("#detalhe_produto");
if ($("#detalhe_produto").length == 0) {
dialog = $('<div id="detalhe_produto" style="display:hidden"></div>').appendTo('body');
}
dialog.load(
url,
{},
function (responseText, textStatus, XMLHttpRequest) {
dialog.dialog({
close: function (event, ui) {
dialog.remove();
},
modal: true,
width: 460
});
}
);
});
});
</script>
and then the code table:
<table border="0" cellpadding="0" cellspacing="3">
<?php do { ?>
<tr>
<td align="left" valign="top" class="lista_produto"><?php echo $row_lista_produtos['codigo']; ?></td>
<td width="15"></td>
</tr>
<?php } while ($row_lista_produtos = mysql_fetch_assoc($lista_produtos)); ?>
</table>
I have tried to change the href="#" to href="javascript: void (0)"
and the result was the same.
Suggestions?

If you have many distinct dialogs that can be clicked open, you need to use a class selector, . instead of an ID selector, #. You may have also forgotten the #.
So instead of this:
var dialog = $("#detalhe_produto");
do this:
var dialog = $(".detalhe_produto");
**Please see this jsfiddle: http://jsfiddle.net/trpeters1/uFDdb/2/
It has a complete working demonstration of the jqueryUI dialog for multiple dialogs specific to your use case. Meaning, the dialog shows a value specific to which link was clicked.

you missed a # in $('detalhe_produto').click(function(){

Related

Need to get table row's link attribute in JQuery and

I have a table which I has links on each row. I am trying to use AJAX to retrieve an array of data from the database via PHP and return the data which will print in another DIV.
I am trying to get the attribute value (link ID) that I assign to each row and pass this value to the query. I am not able to get the link ID, it does not display when I test issuing an alert. I read several post and tried a few things but no good. The alert popup displays "undefined".
<tr id="mashed_row">
<td class="linked-title dark unpadded">
<?php echo $link['keywords']; ?>
</td></tr>
<script type="text/javascript">
$(document).ready(function(){
$('#mashed_row a').click(function () {
alert($(this).closest('tr').find('td').attr('link_id'));
});
});
</script>
You are trying to get td attr which is not there, try this, hope it helps :-
$(document).ready(function(){
$('#mashed_row a').click(function () {
alert($(this).attr('link_id'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table>
<tr id="mashed_row">
<td class="linked-title dark unpadded">
key words
</td>
</tr>
<table>
Simply use $(this).attr('link_id')
$(document).ready(function(){
$('#mashed_row a').click(function () {
alert($(this).attr('link_id'));
});
});
Your TD does not have a link_id attribute, your <a> has it. I've created an example showing both versions:
$(document).ready(function(){
$('#mashed_row a').click(function () {
console.log("TD:", $(this).closest('tr').find('td').attr('link_id'));
console.log("A:", $(this).closest('tr').find('a').attr('link_id'));
// or if you really want to get the attribute from the clicked item, it's even easier:
console.log("direct:", $(this).attr('link_id'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table>
<tr id="mashed_row">
<td class="linked-title dark unpadded" link_id="link_id_TD">
Hello
</td>
</tr>
</table>

How can I achieve a jQuery onhover effect like WordPress post list's edit links?

On mouse hover, the post edit and other related links are visible on the WordPress Posts List. I did a similar thing with the following js:
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js?ver=3.8.1-alpha'></script>
<script type="text/javascript">
$(document).ready(function () {
$(".edit_links").css('visibility', 'hidden');
$(".each_row").mouseenter(function () {
$(".edit_links").css('visibility', 'visible');
}).mouseleave(function () {
$(".edit_links").css('visibility', 'hidden');
});
});
</script>
If my HTML & PHP are as follows:
<table width="100%" border="all">
<tr id="row-1" class="each_row">
<td>1</td>
<td class="name">"Name"
<div class="edit_links">
Edit
</div>
</td>
</tr>
<tr id="row-2" class="each_row">
<td>2</td>
<td class="name">"Name New"
<div class="edit_links">
Edit
</div>
</td>
</tr>
</table>
And the CSS is:
<style>.edit_links{visibility: hidden;}</style>
It does almost the same thing: load the link div on mouseenter. But, the problem is: it shows the edit links of all the rows on any row's mouseenter. And this is logical with the code.
But I want to load the edit links only on the hovered row, like WordPress. (Ref.: Image) See the edit links are visible only on a single row, not on all.
How can I modify my javascripts to achieve so?
You can do this with some simple CSS:
In addition to the rule you have to hide the links, add the following.
tr.each_row:hover .edit_links { visibility: visible; }
You can them remove the related javascript you've got there trying to do this.
Here's a jsfiddle: http://jsfiddle.net/CKaPv/1/
Here is a fiddle with the solution
jsfiddle
It selects each row in the table, and finds the next .edit_links class. The way you were trying to do it will select all elements with the .edit_links class.
$(".edit_links").css('visibility', 'hidden');
$("table tr.each_row").each(function() {
$(this).mouseenter(function () {
$(this).find('.edit_links').css('visibility', 'visible');
}).mouseleave(function () {
$(this).find('.edit_links').css('visibility', 'hidden');
});
});

Open dialog box with contents php traversed from a database using js/jquery

I am creating an image map with buttons. The buttons are created by php:
while($pc = mysql_fetch_array($result)){
if ($pc['Online_Status']==0){
$class = 'workstation-offline';
}
else{
$class = 'workstation-online';
}
echo "\r\n\t\t\t".'<button class="'.$class.'" id="'.$pc['PCID'].'" title= "'.$pc['PCID'].'>';
}
When the page loads, traverse.php dictates what class the button will be. In my css, the workstation-offline will be green, and red if offline. traverse.php also loads data from a database.
This is the html code:
<div class="image_map" style="background-image: url('image.jpg');">
<?php
include("traverse.php");
?>
</div>
What I'd like to happen is when a button is clicked, a dialog box will open and display data about the station with the ID "pc['PCID']" in a table like:
<h3>"The ID of the Button"</h3>
<table>
<tr>
<td>IP Address</td>
<td>pc['ipAddress']</td>
</tr>
<tr>
<td>MAC Address</td>
<td>pc['macAddress']</td>
</tr>
<tr>
<td>Other Info</td>
<td>pc['otherInfo']</td>
</tr>
</table>
How can I do that using js/jquery?
I would use jquery-ui modal dialog box. (http://jqueryui.com)
You could load data on the fly like this. Here's a click handler for the buttons that will create a dialog box and then load data from a server side page :
$(document).ready(function () {
$('.workstation-online,.workstation-offline').click(function () {
var url = "put the url to obtain info from database here";
var dialog = $('<div style="display:none" class="loading"></div>').appendTo('body');
dialog.dialog({
resizable: false,
width: 400,
height: 300,
modal: true,
closeText: '',
close: function (event, ui) {
// remove div with all data and events
dialog.remove();
}
});
dialog.load(
url,
function (responseText, textStatus, XMLHttpRequest) {
// remove the loading class
dialog.removeClass('loading');
}
);
return false;
});
});

How to add a content into the correct textarea by clicking on an "Add" button

When you open the application can you please click on the "Add Question" twice so it adds 2 table rows.
Now you will see in the application that above the horizontal line there is a textarea and a plus button, and below the horizontal line shows 2 table rows, both rows displaying its own textarea and plus button.
Now my question is that I want these 2 outcomes to happen but I do need help from somebody who is good at using Jquery/Javascript in order to solve this situation:
Situation 1. If the user clicks on the plus button ABOVE the horizontal line, then it displays a modal window which contains a search bar, Please type in search bar "AAA". You will now see a list of results. Now what I want is that if the user selects a row by clicking on the "Add" button, then I want the "QuestionContnet" within that row to be displayed in the textarea above the horizontal line. At the moment the Add button just closes the modal window but doesn't add anything into the textarea.
Situation 2: This deals with the user clicking on a plus button within one of the table rows BELOW the horizontal line. I want the same thing to happen except the "QuestionContent" added is displayed in the textarea within the same row the user has clicked the plus button, no where else.
How can both situations be solved so that it adds the QuestionContent into the correct textareas depending on which plus button is clicked? I am using an Iframe to display the content within the modal window.
UPDATE:
If you look at the application, it is now displaying "[Object] [object]" in textaea when I click "Add" button. Not the "Question".
Below is the code for the application:
<head>
<script type="text/javascript">
var plusbutton_clicked;
function insertQuestion(form) {
var $tbody = $('#qandatbl > tbody');
var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
var $plusrow = $("<td class='plusrow'></td>");
var $question = $("<td class='question'></td>");
$('.questionTextArea').each( function() {
var $this = $(this);
var $questionText = $("<textarea class='textAreaQuestion'></textarea>").attr('name',$this.attr('name')+"[]")
.attr('value',$this.val());
$question.append($questionText);
});
$('.plusimage').each( function() {
var $this = $(this);
var $plusimagerow = $("<a onclick='return plusbutton();'><img src='Images/plussign.jpg' width='30' height='30' alt='Look Up Previous Question' class='imageplus'/></a>").attr('name',$this.attr('name')+"[]")
.attr('value',$this.val());
$plusrow.append($plusimagerow);
});
$tr.append($plusrow);
$tr.append($question);
$tbody.append($tr);
form.questionText.value = "";
$('.questionTextArea').val('');
}
function plusbutton() {
// Display an external page using an iframe
var src = "previousquestions.php";
$.modal('<iframe src="' + src + '" style="border:0;width:100%;height:100%;">');
return false;
}
function closewindow() {
$.modal.close();
return false;
}
$('.plusimage').live('click', function() {
plusbutton($(this));
});
function plusbutton(plus_id) {
// Set global info
plusbutton_clicked = plus_id;
// Display an external page using an iframe
var src = "previousquestions.php";
$.modal('<iframe src="' + src + '" style="border:0;width:100%;height:100%;">');
return false;
}
function addwindow(questionText) {
if(window.console) console.log();
var txt = $(this).val(questionText);
if($(plusbutton_clicked).attr('id')=='mainPlusbutton') {
$('#mainTextarea').val(txt);
} else {
$(plusbutton_clicked).parent('td').next('td.question').find('textarea.textAreaQuestion').val(txt);
}
$.modal.close();
return false;
}
</script>
</head>
<body>
<form id="QandA" action="<?php echo htmlentities($action); ?>" method="post">
<div id="detailsBlock">
<table id="question">
<tr>
<td rowspan="3">Question:</td>
<td rowspan="3">
<textarea class="questionTextArea" id="mainTextarea" rows="5" cols="40" name="questionText"></textarea>
</td>
</tr>
</table>
<table id="plus" align="center">
<tr>
<th>
<a onclick="return plusbutton();">
<img src="Images/plussign.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage" id="mainPlusbutton" name="plusbuttonrow"/>
</a>
<span id="plussignmsg">(Click Plus Sign to look <br/> up Previous Questions)</span>
</th>
</tr>
</table>
<table id="questionBtn" align="center">
<tr>
<th>
<input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />
</th>
</tr>
</table>
</div>
<hr/>
<div id="details">
<table id="qandatbl" align="center">
<thead>
<tr>
<th class="plusrow"></th>
<th class="question">Question</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</form>
</body>
The details stored in the modal window comes from a seperate script known as "previousquestions.php", Below is the code where it shows the result of the "QuestionContent" field only displayed and it's "Add" button after the user has compiled a search:
<?php
$output = "";
while ($questionrow = mysql_fetch_assoc($questionresult)) {
$output .= "
<table>
<tr>
<td class='addtd'><button type='button' class='add' onclick='parent.addwindow();'>Add</button></td>
</tr>";
}
$output .= " </table>";
echo $output;
?>
Thank you
Application here
The first issue is this ->
<button id="add">
You cannot reuse an ID over and over, or, the page will iterate to the last element with that ID and NEVER run anything on any previous elements.
Quick fix:
<button class="add">
Simple enough.
We need to get the question text, from the first column, there are so many methods to do this with jQuery selectors it's mind blowing.
Situation 1
Let's take a peek at one option ->
$(document).on('click', '.add', function(){
//now that we've declared the click function, let's jump up to our parent element, and grab the text in the first cell.
var theQuestion = $("td:first-child", $(this).parent()).text();
//Now that we've figured out how to traverse the DOM to get the data we want, we need to move that data elsewhere.
$('.questionTextArea').val(firstCell);
});
Simple enough, right? That should solve the first problem you had.
Note: Textareas use value to set the data, whereas other elements will use .text()
Situation 2
Alright, now we need to figure out how to add a unique identifier to the "+" row when we click, and check for that "unique identifier" when appending the data, let's do it.
You have a <td> with a class of plusrow, sounds good, let's make a click function out of it, and give it a cool new class to reference.
$(document).on('click', '.plusrow', function(){
//adding a unique class for the purpose of the click.
$(this).addClass('activePlusRow');
});
So now the "+" we clicked has a new class -- activePlusRow, let's go back to our initial click handler for the add button, and give it some new conditional statements.
$(document).on('click', '.add', function(){
//lets get our Question Text...
var theQuestion = $("td:first-child", $(this).parent()).text();
//the row is present, let's then make sure that the proper cell gets your data.
if($('.activePlusRow').length > 0){
$('.activePlusRow').next('.textAreaQuestion').val(theQuestion);
$('.activePlusRow').removeClass('activePlusRow');
}
});
Alright, as you can see of the above, we test to see if the activePlusRow class exists, if it does, then we iterate to the next textarea with a class of textAreaQuestion and change the value of it, to theQuestion from the .add button we clicked.
Let me know if you've any questions.
I think best way to target chosen question to specified textarea is to parameterise your plusbutton function:
function plusbutton(plus_id) {
// Set global info
plusbutton_clicked = plus_id;
// Display an external page using an iframe
var src = "previousquestions.php";
$.modal('<iframe src="' + src + '" style="border:0;width:100%;height:100%;">');
return false;
}
Of course you have to declare global var plusbutton_clicked; before insertQuestion function so other functions could change/use this information. You have to call this function with unique parameter for each plusbutton (you generate it dynamically, so this is up to you how you want to do it)
Then you can simply use it in your $(document).on('click', '.add', function(){}); - when user clicks 'add' button you have your plusbutton id so you can navigate with jQuery to corresponding textarea and place text there.
I'm thinking next() isn't the right function, since it only searches for the immediately following sibling... Try this:
$(document).on('click', '.add', function(){
console.log("clicked");
//lets get our Question Text...
var theQuestion = $("td:first-child", $(this).parent()).text();
//the row is present, let's then make sure that the proper cell gets oru data.
if($('.activePlusRow').length > 0){
$('.activePlusRow').next('.question').find('textarea.textAreaQuestion').val(theQuestion);
$('.activePlusRow').removeClass('activePlusRow');
}
$.modal.close();
return true;
});

Jquery UI - multiple dialog boxes

I have initialized my jquery ui like this, I can get it working for one dialog box:
<script>
// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 1000;
$(function() {
$( "#treatment" ).dialog({
autoOpen: false,
show: "blind",
hide: "explode"
});
$( "#opener" ).click(function() {
$( "#treatment" ).dialog( "open" );
return false;
});
});
</script>
However, I have a table and for one column, 'treatments' the user has to click on "more info" to find out more, so far I have coded it like this:
foreach($foo['MedCond'] as $row)
{
?>
<tr>
<td><b>Name:</b></td>
<td><b>Symptoms:</b></td>
<td><b>Treatment:</b></td>
<td><b>Effective From:</b></td>
<td><b>Effective To:</b></td>
</tr>
<tr>
<td><?php echo $row['Name'];?></td>
<td><?php echo $row['Symptoms'];?> </td>
<td><div id="treatment"><p><?php echo $row['Treatment'];?></p></div><button id="opener">More info</button></td> [...]
So for every row for treatments, there is a corresponding dialog open button, once they click on that, details about that particular treatment shows up. I have got this working for one, but it fails when more then one shares the same "treatment div".
How can I get this to work.
Cheers.
Use class instead of Id. means use $( ".treatment" ).dialog and write div class="treatment". Same procedure for opener

Categories