views:

85

answers:

1

Site: http://tinyurl.com/2cjlsxk

This code is not optimized nor is it the best method. If you have any ideas to improve anything, let me know.

Please visit the site to get an idea of the data. I have used Dan G. Switzer, II's calculation plugin [adding .sum() .max() .min() .avg()]

The validation requirement i'd like to have is to make sure nothing conflicts with that user's already determined range. Also, that there are no gaps in the range.

For example give

Brian 40 50 1200
Brian 50 70 1200

I dont want to the user to be able to set the first 50 to 39 because 39 would be smaller than 40. I dont want them to let 50 be set to anything higher than 50 because it would overlap the next range.

Any good ideas? perhaps actually running through all the values and then making a real range and then on BLur have it check to make sure no range is actually overlapped or gapped.

Each unique input is defined as id=NAME, so if I wanted to reference all the inputs of Brian, I could use $("input[id='Brian']").each() or if I wanted to reference all the START inputs of Brian, I can use $("input[id='Brian'][name='start[]'").each()

Edit:

One thing to note is that the page is PHP, and PHP is ran to populate the inputs via a CSV file. It will always start with correct data, and PHP can be used to help create ranges.

because of this I was thinking of just disabling the START field, because it will always populate the next range. However, I will be adding the ability to delete rules, so that can get messy if they are limited in what they can do.

+2  A: 

One of the problems I see is that the Name fields is editable. If the field is changed, all the overlapping has to be recalculated. Not only is that is a performance issue, it is also a usability problem: what if one wants to change a name and the script, seeing discrepancies, forbids it?

A solution would be to change the table to one resembling the diagram below:

[ Add Employee ]

|    Name    |  Start  |  End  |  Wages  |
==========================================
|            |    0    |   50  |   100   |
|    Brian   |   50    |   70  |   150   |
|            |  100    |  150  |   200   |
|                 [ Add Rule ]           |
------------------------------------------
|   Another  |    0    |  ...  |   etc   |

The [ Add Employee ] button would ask for a name and add a cell in the first column, and a line in the next three: with a default Start value of 0. The user can then enter data on the next fields, and can [ Add Rules ] as wanted. A good restriction could be to lock the values of the Start column and set them to the End value of the previous line.

$('#Brian .LineN input.start').val(
    $('#Brian .LineN').prev().find('input.end').val()
);

Then adding a comparison function to check if the End value is lower than the Start value is trivial to implement.

Deleting a rule could simply follow the same procedure as for adding a line: the line below the one deleted (if any) would set its Start value as the now previous line's End.

The real difficulty would be to insert lines at arbitrary points. I'm not going to think about that one, though.

EDIT: If a rewrite is out of the question, then just disabling the start should be enough. However, IMHO I would rather (re)write something with no caveats than spend time later on numerous bugs and feature requests.

passcod
This is definitely a better solution, how would you suggest I process the input fields? right now I have to use to different input namesRight now I use `<td><input type='text'name='Default_wage[]'></td>` for Default and `<td><input type='text'name='wage[]'></td> ` for everyone else.The $_POST is parsed by PHP on submitting. I guess one way would be allowing looping through all the post vars.Is it possible to use Associative Arrays in the name? for example`<td><input type='text'name='wage["Default"]'></td>`
BHare
You can use associative arrays in POST. It would also be relatively easy to use jQuery to generate names for the input fields depending on where they are. But... if you use jQuery, why not use XHR? You could use JSON to pass on whatever structure you decide on.
passcod
OK. Thats good to know but totally re-writing what I already have is a last thing I want right now.
BHare
IMHO I would rather rewrite something (near) perfect than spend time later fixing bugs ~ debugging is alright as long as it doesn't take more time than coding well would have.
passcod