Ive got a jquery table that allows me to select a record. When that record is selected i want its details to appear in a table as another record. Ive managed to initailize a variable in the controller:
@var ||= []
That variable is then populated with the data from the project object:
<% @projects.each do |project| %>
<tr>
<td><%= project.project_number %></td>
<td><%= project.project_name %></td>
<% @var = project.project_name %>
<td><%= link_to_function image_tag("icons/add.png"), "CreateNewRow('var')" %></td>
<!-- link_to image_tag("icons/add.png"), tasklist_path(project.id), :as => "tasklist" -->
</tr>
<%- end -%>
As soon as the user clicks the button it will use the CreateNewRow () function:
function CreateNewRow(str)
{
var intLine = parseInt(document.frmMain.hdnMaxLine.value);
intLine++;
var theTable = document.getElementById("tbExp");
var newRow = theTable.insertRow(theTable.rows.length)
newRow.id = newRow.uniqueID
var newCell
var argument = str
//*** Column 1 ***//
newCell = newRow.insertCell(0);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"10\" NAM开发者_如何学PythonE=\"Column1_"+intLine+"\" ID=\"Column1_"+intLine+"\" VALUE=\"\"></center>";
//*** Column 2 ***//
newCell = newRow.insertCell(1);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"Column2_"+intLine+"\" ID=\"Column2_"+intLine+"\" VALUE=\"\"></center>";
newCell.innerHTML = "<center><SELECT NAME=\"Column5_"+intLine+"\" ID=\"Column5_"+intLine+"\"></SELECT></center>";
//*** Column 3 ***//
newCell = newRow.insertCell(2);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"Column3_"+intLine+"\" ID=\"Column3_"+intLine+"\" VALUE=\"\"></center>";
ID=\"Column5_"+intLine+"\"></SELECT></center>";
//*** Create Option ***//
CreateSelectOption("Column5_"+intLine)
document.frmMain.hdnMaxLine.value = intLine;
}
My question is how do i get the argument from the javascript function to display as text in the innerHTML value field.. or even better would be a label of some kind? Some thing that takes the argument and displays it in the table cell... im at my wits end....
1) Your rails view doens't set the "var" variable in javascript. It just sets a var variable in rails. An instance variable, even. None of which makes sense. Just do this instead
<%= link_to_function image_tag("icons/add.png"), "CreateNewRow('#{project.project_name}')" %>
2) You're missing a lot of semi-colons in your javascript. Consider running your javascript through jslint to catch those errors
3) Use text-align:center
in the cell's css class, rather than the <center>
tag to center the contents of the cells
4) If you're already using jQuery, there's a ton of helper functions you can use. So you should look into that.
But I don't want to rewrite all your code, so here's just the minimum you need:
newCell.innerHTML = '<INPUT TYPE="TEXT" SIZE="10" NAME="Column1_'+intLine+'" ID="Column1_'+intLine+'" VALUE="'+str+'">";
In other words: You set the value attribute (i.e. the content) of the text field, in exactly the same way as when you set the id and name attributes (and I've used single quotes around the string to avoid having to escape all the double-quotes inside it)
Lastly, unless you're writing old HTML 4, there's no need to have use all-uppercase tag names and attributes. <input type="text" ... >
works just as well, and is easier to read, I think
精彩评论