Reduce width of number field?

Reduce width of number field?

by Fernando Oliveira -
Number of replies: 46
Picture of Plugin developers

Hello,

Anyone know how I can go about reducing the width of the number field? Currently, the default width is 46 characters wide, which is throwing off my template. I only need users to enter a 2 digit number.

Any suggestions would be appreciated.

Fernando

Attachment number-width.png
Average of ratings: -
In reply to Fernando Oliveira

Re: Reduce width of number field?

by Itamar Tzadok -

Try enclosing these fields in span tags with a css class and then adding a rule in the css template. This may look something like the following.

Add entry template:

<span class="2digit_number_field">[[Day1-1] ]</span>

...

<span class="2digit_number_field">[[Day1-2] ]</span>

...

CSS template:

.2digit_number_field input {width:5px;}

 

smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Fernando Oliveira -
Picture of Plugin developers
Thanks Itamar!
In reply to Fernando Oliveira

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Hello Itamar, what I am doing wrong. In my add entry template I have:

<table border="0">
<tbody>
<tr>
<td><span class="Name_field">[[Name] ]</span></td>
</tr>
</tbody>
</table>

and at the end of the CSS template I have:

/*****  Single View CSS *****/

.caption {
  font-style: italic;
}

#singleimage {
  width: 700px;
}

.Name_field input {width:5px;}

(The square parenthesis disappear so I put in the space)

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Anyone? Thanks

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Tieku Bortei-Doku -

I am trying to specify the width of the entry field following Itamar's instructions but it's not working. Can anyone look at what I have done and tell me why it's not working.

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Tieku, it seems to work on the demo site so I'm not sure why it doesn't work for you (I trust that you clear the browser cache). Do you have any other css definitions their that may clash with this one? smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Hello Itamar,

I went to the demo site, but it still does not work. Here is my Add Template:

<div class="defaulttemplate">
<table cellpadding="5">
<tbody>
<tr>
<td align="right" valign="top">Name:</td>
<td align="left"><span class="Name_field">[[Name] ]</span></td>
</tr>
</tbody>
</table>
</div>


And my CSS Template:

.Name_field input {width:5px;}

but the width does not change. And I cleared my browser cache.
Thanks

 

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Ah, yes. That's because the width of the Text field type is inline styled and thus overrides any stylesheet definition. There is a recent tracker issue on this, in which I propose a hack for 1.9, but it should work also for 2.0 (I have to check though). In the meantime you can use javascript to work around, so simply add at the bottom of the Add template the following:

<script type="text/javascript">
document.getElementById('[[Name#id] ]').style.width='5px';
</script>

hth smile

Average of ratings: Useful (1)
In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Frank Ralf -
Hi Tieku,

Try using !important with your CSS.

hth
Frank
Average of ratings: Useful (1)
In reply to Frank Ralf

Re: Reduce width of number field?

by Itamar Tzadok -

... of course ... I'm so into just changing the code now that I forget some important workarounds. smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

How exactly do I use !important with my CSS?

Thanks (from a novice)

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Try

.Name_field {width: 5px !important;}

 

With the internet no one is a novice anymore (apropo online learning ...). Just google css important and you'll find many explanations with examples.

smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Tried both !important and Javascript code but neither work. This is on a new installation of 2.0.2. Field width does not change? Also tried and demo.moodle.org sad

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Select the page source of that part and post it here so that we could take a look at it. smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Getting error trying to paste text of Page Source. "Error writing to database". Also cannot paste in text fiel and send. File size of 112KB larger than 100KB

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Take a screenshot, paste an image. smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Screen shot of Page Source? Entire code requires multiple screenshots

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Where does "that part" of page source begin and end?

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

I don't know what happens in other browsers by in FF you can select any section of the page , right click and View selection source.

So for example I extracted this bit of html from the Add entry page of the weblink database activity in Moodle Demo:

<h2 class="main">New entry</h2>
<div class="defaulttemplate">
<table cellpadding="5"><tbody>
<tr><td valign="top" align="right">URL: </td><td><div title="Website address"><input name="field_4_0" id="field_url_4daf51062d747" value="" size="60" type="text"><button id="filepicker-button-4daf51062d747" style="">Choose a link...</button></div></td></tr>
<tr><td valign="top" align="right">Description: </td><td><div title="Website description"><div><textarea style="display: none;" id="field_5" name="field_5" rows="10" cols="30"></textarea>

:-)
In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Frank Ralf -
Just follow the link I provided to SitePoint's CSS reference page. wink

CSS FAQ and Themes FAQ are good starting points for learning more about CSS and theming. And for peeking under the hood of any website Firebug is an invaluable tool.

hth
Frank
In reply to Frank Ralf

Re: Reduce width of number field?

by Tieku Bortei-Doku -

This is what I got:

<form enctype="multipart/form-data" action="edit.php" method="post"><div><input name="d" value="1" type="hidden" /><input name="rid" value="0" type="hidden" /><input name="sesskey" value="OUaiAN2K77" type="hidden" /><div class="box generalbox boxaligncenter boxwidthwide"><h2 class="main">New entry</h2><div class="defaulttemplate"><table cellpadding="5"><tr><td valign="top" align="right">Name: </td><td  align="left"><span class="Name_field"><div title="Name Desc"><input style="width:300px;" type="text" name="field_1" id="field_1" value="" /></div></span></td></tr></table></div><div class="mdl-align"><input type="submit" name="saveandview" value="Save and view" />&nbsp;<input type="submit" value="Save and add another" /></div></div></div></form>                        </div>
                    </div>
                </div>
 
In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

I've just tried it on Moodle demo and it seems to work.

Here is the Add template code which you can simply paste into the template in html mode:

<div class="defaulttemplate">
<table cellpadding="5">
<tbody>
<tr>
<td align="right" valign="top">Name:</td>
<td align="left"><span class="Name_field">
<div title="Name Desc">[[Name] ]</div>
</span></td>
</tr>
</tbody>
</table>
</div>

 

And in the css template:

.Name_field input {width:5px !important;}

 

Refresh browser cache. smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

I just went to the demo site to try, If you go there in the next 50 minutes I have created a course C1 with a database called DB1. But the displayed field width is still the same for the Name text field (Does not look like 5px)?

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Alas missed your 50 minutes mark, but look:

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

What am I missing?

Please pick out the flaw in my steps:

  1. Go to demo.moodle.org and login as admin
  2. Create a course and database activity – give it name and introduction
  3. Create a new field – Text
  4. Field name -- Name – Add
  5. Templates – Add template – Edit HTML Source – paste your code (remove space between second square bracket [[Name] ]) – Save template
  6. CSS template – paste your code – Save template
  7. Go to -- Add Entry -- width the same???

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Create a course ???

Try first in the existing Features demo course. If it works there then it may be something about the new course and if not, maybe something about your browser. thoughtful

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Seems to be working now. Thanks for your patience. Maybe was not saving the template or not refreshing browser cache enough? Not sure.

This just gets me past my first step in building my database activity. A few quick questions:

  1. Can I perform calculations on fields i.e. field1 + field2 = field3
  2. Is there data validation for text or number field input?
  3. Is there a pop-up date-picker date field
  4. Can I auto-populate fields with values from users profile e.g. institution, department, and User profile fields

Thanks

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

You may be able to do 1-3 with javascript.

I'm working on implementing all this functionality in my own version of the module but I don't expect a version for Moodle 2 before the end of the summer. smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Can you point me to your version of the module for 1.9?

Thanks

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

A preview should be available by the end of the weekend and will be announced when committed. smile

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Paul Young -

Go to http://www.javascriptkit.com/ and you will find lots of javascript kits you can tweak and use. Just make sure you accknowledge the author.

In reply to Paul Young

Re: Reduce width of number field?

by Tieku Bortei-Doku -

I am making steady progress building my database activity. I found this code for a popup calendar I would like to use. How do I tie this to the Date field [[Date] ] i created? Can the database Date field use the calendar popup that comes with Moodle 2.0?

<SCRIPT Xlanguage="JavaScript" ID="jscal1x">
var cal1x = new CalendarPopup("testdiv1");
</SCRIPT>
<!-- The next line prints out the source in this example page. It should not be included when you actually use the calendar popup code -->
<SCRIPT Xlanguage="JavaScript">writeSource("jscal1x");</SCRIPT>
<INPUT TYPE="text" NAME="date1x" VALUE="" SIZE=25>
<A HREF="#" XonClick="cal1x.select(document.forms[0].date1x,'anchor1x','MM/dd/yyyy'); return false;" TITLE="cal1x.select(document.forms[0].date1x,'anchor1x','MM/dd/yyyy'); return false;" NAME="anchor1x" ID="anchor1x">select</A>

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

You don't. This calendar seems to assume a simple text field for input an output. The Moodle date field is not a simple text field. But then again, once you recognize that the Moodle date field is in effect a calendar interface (albeit a very simple one (albeit not as simple as a simple text field)) you may realize that if you use the javascript calendar you don't really need the Moodle date field and can do with a simple text field as the example implementation of the script suggests.

One more considertation. Get the designated text field ('date1x' in the example) by getElementById and not by document.forms[0]. The Add template may have more than one form and since the order of forms in document.forms may differ from browser to browser, using document.forms will put the 'date1x' field in superposition, to the effect that its actual place-holder form may be known only from a browser's perspective. smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

I know I need some step by step help here.

I create a text field called Date ([[Date] ]) instead of using the date field type. What do I substitute in the example code above and where does each piece of code go? Where do I save the js files?

Thanks.

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Without looking at the cal script I suppose you should add the following next to [[Date] ] in the template:

<a href="#" onClick="cal1x.select(document.getElementById("[[Date#id] ],'anchor1x','MM/dd/yyyy'); return false;" title="cal1x.select(document.getElementById("[[Date#id] ],'anchor1x','MM/dd/yyyy'); return false;" name="anchor1x" id="anchor1x">select</a>

The js files should go anywhere that is accessible from the course site and then you should add at the end of the template, for each js file:

<script type="text/javascript" src="XXXXXXX"></script>

where XXXXXXX is the url of the js file.

htw smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Where is the matching " for ("[[Date#id] ],

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

should be:

... document.getElementById("[[Date#id] ]") ...

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Hello Itamar I have got it to work. But after several hours I found i had to change the " to '. Here is my add entry template. Why is the select link showing up under the [[Date] ] input field/box and not to the right of it?

 

[[Date] ]<A HREF="#" XonClick="cal.select(document.getElementById('[[Date#id] ]'),'anchor1','MM/dd/yyyy'); return false;" NAME="anchor1" ID="anchor1">select</A>

<SCRIPT LANGUAGE="JavaScript" SRC="http://localhost/m202/javascript/CalendarPopup.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript"> var cal = new CalendarPopup();</SCRIPT>

What is the JavaScript template for? I thought I should be able to paste the JavaScript code there but it only works when I paste it in the Add entry template?


Is there a difference between going into HTML mode and disabling the editor? HTML mode adds and re-arranges the code, and you cannot keep the editor disabled?
Thanks

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Why is the select link showing up under the [[Date] ] input field/box and not to the right of it?

because the field is hardcodedly enclosed in a div tag. If you want the select to the right of the field either put these items in a 2-column 1-row table or work out the required css (which may be quite tricky).

What is the JavaScript template for?

It is for javascript code, BUT, in your case you use [[Date#id] ] tag which is not javascript code and as is can be known and properly converted to code only in the Add template. You can pass this tag as string into a function which is defined in the js template but that requires some more coding. You also include a js file which you can do in the js template if you use the proper dom methods.

Is there a difference between going into HTML mode and disabling the editor?

If the editor in database 2.0 works like in 1.9, it doesn't appreciate white spaces in the code so unless you remove all white spaces from the code you need to disable it if you want to edit the template. As a workaround, keep and modify this bit of code in an external file and copy paste into the template as needed.

hth smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

either put these items in a 2-column 1-row table or work out the required css (which may be quite tricky).

Or I should get the Javascript code to simply click in the entry field to trigger the popup calendar, instead of using the select link?

And all your responses were very helpful. Thanks

 

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

Try:

<script type="text/javascript">

var cal = new CalendarPopup();

document.getElementById('[[Date#id] ]').XonClick="cal.select(this,'anchor1','MM/dd/yyyy'); return false;";

</script>

and next to the date field simply leave

<a href="#" name="anchor1" id="anchor1"></a>

You need this anchor so that the cal popup will open there.

smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

I wonder what I am doing wrong. This is what i have in the Add template, but not working:

[[Date] ]<a href="#" name="anchor1" id="anchor1"></a>

<SCRIPT language="JavaScript" SRC="http://localhost/m3/java/CalendarPopup.js"></SCRIPT>

<script type="text/javascript">

var cal = new CalendarPopup();

document.getElementById('[[Date#id] ]').XonClick="cal.select(this,'anchor1','MM/dd/yyyy'); return false;";

</script>

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

If you use FF check in Tools ->Error console, if there are any javascript errors. smile

In reply to Itamar Tzadok

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Not to sound lazy but I get no errors messages in FF, but alot of warnings. And warnings with the other code that works?

This other one I found seems to work? I just need to change the code and link it to [[Date#id] ]

<link href="/ECN/Ztools/CalendarControl/CalendarControl.css"
      rel="stylesheet" type="text/css">
<script src="/ECN/Ztools/CalendarControl/CalendarControl.js"
        language="javascript"></script>

<input name="todays_date"
       onfocus="showCalendarControl(this);"
       type="text">

 

Thanks

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Tieku Bortei-Doku -

Should it be:

<link href="http://localhost/m202/javascript/CalendarControl.css"
rel="stylesheet" type="text/css">
<script src="http://localhost/m202/javascript/CalendarControl.js"
language="javascript"></script>


[[Date] ]<script type="text/javascript">

document.getElementById('[[Date#id] ]').onfocus="showCalendarControl(this);"

</script>

 

??? Not very sure. This does nowt work though.

In reply to Tieku Bortei-Doku

Re: Reduce width of number field?

by Itamar Tzadok -

I don't know why. The event handler may be overriden. See if it works with a 'select' link. smile