How do i use jquery to edit tables in the django manager? - javascript

I have a table output in Django from my DB into HTML.
I want to make it so the width for my fields ajusts to the text size, i found some js here:
Adjust width of input field to its input
...but when i paste the code to my .js files it doesnt change the fields at all on the page. this is my css code currently
table#infos td input {
width:4em;
}
table#infos td textarea {
width:6em;
height:2em;
}
and this is an eg of some of the html that isnt being changed properly
<tbody>
<tr>
<td>
<div class="resizing-input">
<span class="sort_value" style="display:none;">1</span>
<input id="id_form-0-id" min="0" name="form-0-id" type="number" value="1" />
</div>
</td>
<--etc--!>
the inputs id's are auto generated from the db column names etc
the jquery i tried to use was from this
http://jsfiddle.net/7pccrdbw/2/

Related

make table rows and following cells as mandatory fields

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<table>
<tr>id</tr>
<input type="text" id= "101" style = "color:black" required></td>
<input type="button" value="Submit" style = "color:Red;">
<script>
document.getElementById("101").reset()
</script>
</body>
</html>
1) i want to make mandatory my id field but i am unable to do it .I used required but it is not working
2) i want to empty my text fields after submitting the fields when user clicks submit it is not working
3)In my code i used tr as text fields plese suggest for following tr tag only
You need to keep your code inside <form> and just submit type is enough to reset it. No need to write javascript to reset. Here is your working code snippet:
<body>
<form>
<table>
<tr>
<td>id</td>
<td><input type="text" id= "101" style = "color:black" required></td>
<td><input type="submit" value="Submit" style = "color:Red;"></td>
</tr>
</table>
</form>
</body>
If you want separate reset button to reset the field, add this in your table:
<td><input type="reset" value="Reset" style = "color:Red;"></td>
First, your html markup is wrong (inside TR you need to put TD), plus you are missing the form tag.
Adding the form tag, the page will submit and refresh. Te text field will be blank by default, no need to use javascript.
"required" is a HTML5 tag, it will work only with html5 browser that support it.
This code will work with every compatible html5 browser
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form>
<table>
<tr>
<td>id</td>
<td><input type="text" id= "101" style = "color:black" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" style = "color:Red;"></td>
</tr>
</table>
</form>
</body>
</html>
If you want an "editable grid" i.e. a table like structure that allows you to make any of the rows a form, use CSS that mimics the TABLE tag's layout: display:table, display:table-row, and display:table-cell.
There is no need to wrap your whole table in a form and no need to create a separate form and table for each apparent row of your table.
Try this instead:
<style>
DIV.table
{
display:table;
}
FORM.tr, DIV.tr
{
display:table-row;
}
SPAN.td
{
display:table-cell;
}
</style>
<div class="table">
<form class="tr" method="post" action="blah.html">
<span class="td"><input type="text"/></span>
<span class="td"><input type="text"/></span>
</form>
<div class="tr">
<span class="td">(cell data)</span>
<span class="td">(cell data)</span>
</div>
</div>
The problem with wrapping the whole TABLE in a FORM is that any and all form elements will be sent on submit (maybe that is desired but probably not). This method allows you to define a form for each "row" and send only that row of data on submit.
The problem with wrapping a FORM tag around a TR tag (or TR around a FORM) is that its invalid HTML. The FORM will still allow submitting as usual but at this point, the DOM is broken. Note: Try getting the child elements of your FORM or TR with JavaScript, it can lead to unexpected results.
Note that IE7 doesn't support these CSS table styles and IE8 will need a doctype declaration to get it into "standards" mode: (try this one or something equivalent)
Any other browser that supports display:table, display:table-row and display:table-cell should display your CSS data table the same as it would if you were using the TABLE, TR and TD tags. Most of them do.
Note that you can also mimic THEAD, TBODY, TFOOT by wrapping your row groups in another DIV with display: table-header-group, table-row-group, and table-footer-group respectively
you are missign a semi-colon in your input field. Should be
<input type="text" id="101" style="color:black;" required>
In general it's preferable to create a separate .css file rather than utilizing the style attirbute
In general you are invokingthe javascript incorrectly.
<input type="button" value="Submit" style = "color:Red;" onclick="clearInput()">
And you should change the javascript accordingly:
<script>
function clearInput() {
document.GetElementById('101').value = "";
}
</script>
Your table form in general is wrong. Your general structure should look something like this
<table>
<td>
<tr>1</tr>
<tr>2</tr>
</td>
<td>
<tr>Column 2 Row</tr>
</td>
</table>
A working js fiddle example:
function clearInput() {
document.getElementById('101').value = "";
}
input[type="button"] {
color: red;
}
<input type="text" id="101">
<input type="button" value="Submit" onclick="clearInput()">

Submit select form item value - not working [duplicate]

I'm trying to create a table where each row is a form. I want that each input is in a different table division, but I still need that for example, all first inputs belong to the same table head and so on.
What I'm trying to do is an editable grid, more or less this:
<table>
<tr>
<form method="POST" action="whatever">
<td><input type="text"/></td>
<td><input type="text"/></td>
</form>
</tr>
<tr>
<form method="POST" action="whatever">
<td><input type="text"/></td>
<td><input type="text"/></td>
</form>
</tr>
</table>
But apparently I cannot arrange the tags in that way (or so is what the w3c validator said).
Any good way to do this?
If you want a "editable grid" i.e. a table like structure that allows you to make any of the rows a form, use CSS that mimics the TABLE tag's layout: display:table, display:table-row, and display:table-cell.
There is no need to wrap your whole table in a form and no need to create a separate form and table for each apparent row of your table.
Try this instead:
<style>
DIV.table
{
display:table;
}
FORM.tr, DIV.tr
{
display:table-row;
}
SPAN.td
{
display:table-cell;
}
</style>
...
<div class="table">
<form class="tr" method="post" action="blah.html">
<span class="td"><input type="text"/></span>
<span class="td"><input type="text"/></span>
</form>
<div class="tr">
<span class="td">(cell data)</span>
<span class="td">(cell data)</span>
</div>
...
</div>
The problem with wrapping the whole TABLE in a FORM is that any and all form elements will be sent on submit (maybe that is desired but probably not). This method allows you to define a form for each "row" and send only that row of data on submit.
The problem with wrapping a FORM tag around a TR tag (or TR around a FORM) is that it's invalid HTML. The FORM will still allow submit as usual but at this point the DOM is broken. Note: Try getting the child elements of your FORM or TR with JavaScript, it can lead to unexpected results.
Note that IE7 doesn't support these CSS table styles and IE8 will need a doctype declaration to get it into "standards" mode: (try this one or something equivalent)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Any other browser that supports display:table, display:table-row and display:table-cell should display your css data table the same as it would if you were using the TABLE, TR and TD tags. Most of them do.
Note that you can also mimic THEAD, TBODY, TFOOT by wrapping your row groups in another DIV with display: table-header-group, table-row-group and table-footer-group respectively.
NOTE: The only thing you cannot do with this method is colspan.
Check out this illustration: http://jsfiddle.net/ZRQPP/
If all of these rows are related and you need to alter the tabular data ... why not just wrap the entire table in a form, and change GET to POST (unless you know that you're not going to be sending more than the max amount of data a GET request can send).
(That's assuming, of course, that all of the data is going to the same place.)
<form method="POST" action="your_action">
<table>
<tr>
<td><input type="text" name="r1c1" value="" /></td>
<!-- ... snip ... -->
</tr>
<!-- ... repeat as needed ... -->
</table>
</form>
You may have issues with column width, but you can set those explicitly.
<table>
<tr>
<td>
<form>
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td>
<form>
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
You may want to also consider making it a single form, and then using jQuery to select the form elements from the row you want, serialize them, and submit them as the form.
See: http://api.jquery.com/serialize/
Also, there are a number of very nice grid plugins:
http://www.google.com/search?q=jquery+grid&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
If using JavaScript is an option and you want to avoid nesting tables, include jQuery and try the following method.
First, you'll have to give each row a unique id like so:
<table>
<tr id="idrow1"><td> ADD AS MANY COLUMNS AS YOU LIKE </td><td><button onclick="submitRowAsForm('idrow1')">SUBMIT ROW1</button></td></tr>
<tr id="idrow2"><td> PUT INPUT FIELDS IN THE COLUMNS </td><td><button onclick="submitRowAsForm('idrow2')">SUBMIT ROW2</button></td></tr>
<tr id="idrow3"><td>ADD MORE THAN ONE INPUT PER COLUMN</td><td><button onclick="submitRowAsForm('idrow3')">SUBMIT ROW3</button></td></tr>
</table>
Then, include the following function in your JavaScript for your page.
<script>
function submitRowAsForm(idRow) {
form = document.createElement("form"); // CREATE A NEW FORM TO DUMP ELEMENTS INTO FOR SUBMISSION
form.method = "post"; // CHOOSE FORM SUBMISSION METHOD, "GET" OR "POST"
form.action = ""; // TELL THE FORM WHAT PAGE TO SUBMIT TO
$("#"+idRow+" td").children().each(function() { // GRAB ALL CHILD ELEMENTS OF <TD>'S IN THE ROW IDENTIFIED BY idRow, CLONE THEM, AND DUMP THEM IN OUR FORM
if(this.type.substring(0,6) == "select") { // JQUERY DOESN'T CLONE <SELECT> ELEMENTS PROPERLY, SO HANDLE THAT
input = document.createElement("input"); // CREATE AN ELEMENT TO COPY VALUES TO
input.type = "hidden";
input.name = this.name; // GIVE ELEMENT SAME NAME AS THE <SELECT>
input.value = this.value; // ASSIGN THE VALUE FROM THE <SELECT>
form.appendChild(input);
} else { // IF IT'S NOT A SELECT ELEMENT, JUST CLONE IT.
$(this).clone().appendTo(form);
}
});
form.submit(); // NOW SUBMIT THE FORM THAT WE'VE JUST CREATED AND POPULATED
}
</script>
So what have we done here? We've given each row a unique id and included an element in the row that can trigger the submission of that row's unique identifier. When the submission element is activated, a new form is dynamically created and set up. Then using jQuery, we clone all of the elements contained in <td>'s of the row that we were passed and append the clones to our dynamically created form. Finally, we submit said form.
You can use the form attribute id to span a form over multiple elements each using the form attribute with the name of the form as follows:
<table>
<tr>
<td>
<form method="POST" id="form-1" action="/submit/form-1"></form>
<input name="a" form="form-1">
</td>
<td><input name="b" form="form-1"></td>
<td><input name="c" form="form-1"></td>
<td><input type="submit" form="form-1"></td>
</tr>
</table>
If you need form inside tr and inputs in every td, you can add form in td tag, and add attribute 'form' that contains id of form tag to outside inputs.
Something like this:
<tr>
<td>
<form id='f1'>
<input type="text">
</form>
</td>
<td>
<input form='f1' type="text">
</td>
</tr>
If all of these rows are related and you need to alter the tabular data ... why not just wrap the entire table in a form, and change GET to POST (unless you know that you're not going to be sending more than the max amount of data a GET request can send).
I cannot wrap the entire table in a form, because some input fields of each row are input type="file" and files may be large. When the user submits the form, I want to POST only fields of current row, not all fields of the all rows which may have unneeded huge files, causing form to submit very slowly.
So, I tried incorrect nesting: tr/form and form/tr. However, it works only when one does not try to add new inputs dynamically into the form. Dynamically added inputs will not belong to incorrectly nested form, thus won't get submitted. (valid form/table dynamically inputs are submitted just fine).
Nesting div[display:table]/form/div[display:table-row]/div[display:table-cell] produced non-uniform widths of grid columns. I managed to get uniform layout when I replaced div[display:table-row] to form[display:table-row] :
div.grid {
display: table;
}
div.grid > form {
display: table-row;
div.grid > form > div {
display: table-cell;
}
div.grid > form > div.head {
text-align: center;
font-weight: 800;
}
For the layout to be displayed correctly in IE8:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...
<meta http-equiv="X-UA-Compatible" content="IE=8, IE=9, IE=10" />
Sample of output:
<div class="grid" id="htmlrow_grid_item">
<form>
<div class="head">Title</div>
<div class="head">Price</div>
<div class="head">Description</div>
<div class="head">Images</div>
<div class="head">Stock left</div>
<div class="head">Action</div>
</form>
<form action="/index.php" enctype="multipart/form-data" method="post">
<div title="Title"><input required="required" class="input_varchar" name="add_title" type="text" value="" /></div>
It would be much harder to make this code work in IE6/7, however.
If you can use javascript and strictly require it on your web, you can put textboxes, checkboxes and whatever on each row of your table and at the end of each row place button (or link of class rowSubmit) "save". Without any FORM tag. Form than will be simulated by JS and Ajax like this:
<script type="text/javascript">
$(document).ready(function(){
$(".rowSubmit").click(function()
{
var form = '<form><table><tr>' + $(this).closest('tr').html() + '</tr></table></form>';
var serialized = $(form).serialize();
$.get('url2action', serialized, function(data){
// ... can be empty
});
});
});
</script>
What do you think?
PS: If you write in jQuery this:
$("valid HTML string")
$(variableWithValidHtmlString)
It will be turned into jQuery object and you can work with it as you are used to in jQuery.
I second Harmen's div suggestion. Alternatively, you can wrap the table in a form, and use javascript to capture the row focus and adjust the form action via javascript before submit.
I had a problem similar to the one posed in the original question. I was intrigued by the divs styled as table elements (didn't know you could do that!) and gave it a run. However, my solution was to keep my tables wrapped in tags, but rename each input and select option to become the keys of array, which I'm now parsing to get each element in the selected row.
Here's a single row from the table. Note that key [4] is the rendered ID of the row in the database from which this table row was retrieved:
<table>
<tr>
<td>DisabilityCategory</td>
<td><input type="text" name="FormElem[4][ElemLabel]" value="Disabilities"></td>
<td><select name="FormElem[4][Category]">
<option value="1">General</option>
<option value="3">Disability</option>
<option value="4">Injury</option>
<option value="2"selected>School</option>
<option value="5">Veteran</option>
<option value="10">Medical</option>
<option value="9">Supports</option>
<option value="7">Residential</option>
<option value="8">Guardian</option>
<option value="6">Criminal</option>
<option value="11">Contacts</option>
</select></td>
<td>4</td>
<td style="text-align:center;"><input type="text" name="FormElem[4][ElemSeq]" value="0" style="width:2.5em; text-align:center;"></td>
<td>'ccpPartic'</td>
<td><input type="text" name="FormElem[4][ElemType]" value="checkbox"></td>
<td><input type="checkbox" name="FormElem[4][ElemRequired]"></td>
<td><input type="text" name="FormElem[4][ElemLabelPrefix]" value=""></td>
<td><input type="text" name="FormElem[4][ElemLabelPostfix]" value=""></td>
<td><input type="text" name="FormElem[4][ElemLabelPosition]" value="before"></td>
<td><input type="submit" name="submit[4]" value="Commit Changes"></td>
</tr>
</table>
Then, in PHP, I'm using the following method to store in an array ($SelectedElem) each of the elements in the row corresponding to the submit button. I'm using print_r() just to illustrate:
$SelectedElem = implode(",", array_keys($_POST['submit']));
print_r ($_POST['FormElem'][$SelectedElem]);
Perhaps this sounds convoluted, but it turned out to be quite simple, and it preserved the organizational structure of the table.
Tables are not meant for this, why don't you use <div>'s and CSS?
it's as simple as not using a table for markup, as stated by Harmen. You're not displaying data after all, you're collecting data.
I'll take for example the question 23 here: http://accessible.netscouts-ggmbh.eu/en/developer.html#fb1_22_5
On paper, it's good as it is. If you had to display the results, it'd probably be OK.
But you can replace it with ... 4 paragraphs with a label and a select (option's would be the headers of the first line). One paragraph per line, this is far more simple.

<input> element in tinymce

I want to have input element in my editor so user can type something in it. I've tried just write html in my textarea but the input element goes unclickable (ex1)
Value: <input type='text' value='12.3'> kg
I've also tried to make contenteditable="true" div and input worked in it without problems, so it is tinymce problem (ex2)
I can't use input in editor even if I initialize tinymce with div and set html manually
ed.getBody().innerHTML = "Value: <input type='text' value='12.3'> kg"
You can't have an input element into textarea element.
Try
<form method="post" action="dump.php">
<label for="kgElement">Value:</label>
<input type='text' id="kgElement" placeholder='12.3'> kg
</form>
That is because you're having the Input field inside the textarea element. Which itself is fully editable.
Take it out of the textarea, and then it would work just the way you want it to.

jQuery - Populate closest hidden input with radio's value

I have the following HTML and jQuery to populate hidden fields within each <td> element:
<td>
<div id="sex_div" style="text-align: left; width:120;">
<input type="hidden" id="sex" value=""/>
<input type="radio" id="sex_male" name="sex_div" value="M"/><label for="sex_male">Male</label>
<input type="radio" id="sex_female" name="sex_div" value="F"/><label for="sex_female">Female</label>
</div>
</td>
The jQuery I have is as follows:
$("input :radio").click(function() {
$(this).siblings("input [type='hidden']").val($(this).val());
});
and obviously the buttonset,
$("#sex_div").buttonset();
This is just a small part of the whole form. The rest all looks similar.
Now the issue is that the hidden field is not being set when clicking/selecting a radio button. I have been struggling with this seemingly easy problem for two days now!
Thanks for any help!
$(this).siblings("input[type='hidden']").val($(this).val());
There should be no space between input and [type='hidden']. Spaces in selectors mean to search for descendant elements that match the next token.
you need to remove space in your selector
$("input:radio").click(function() {
$(this).siblings("input[type='hidden']").val($(this).val());
});

Tab through a table inside content editable div?

I am trying to figure out how to tab through HTML tables cells inside a contenteditable div. I know most people would say why are you doing that in the first place. I am working on a text editor that allows the user to insert a pre-formatted table where ever they want. I have tackled inserting the table dynamically at the users cursor but I cannot figure out how to let the user tab from the content to the html table and through each cell. I have tried input boxes which allows them to tab through but it leaves selector bars on each corner and still requires the user to double click on the cell to add content. I have also tried just table cells and it will not tab to the cells it just jumps over them. Any help would be much appreciated... after conquering the tracking of the cursor for inserting the table I thought I was home free... :(
<div id="divbilltext" runat="server" contenteditable="true" style="height:auto;">
<table>
<tr>
<td>
<input type="text" id="a" tabindex="1"/>
</td>
<td>
<input type="text" id="b" tabindex="2"/>
</td>
<td>
<input type="text" id="c" tabindex="3"/>
</td>
</tr>
</table>
</div>
tabindex would be the most consistent way of achieving this:
http://www.w3.org/TR/html4/interact/forms.html#adef-tabindex
however tabindex is supported by specific elements only: A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA.

Categories

Resources