I have a page at this link http://183.78.169.53/lp/addRoute6.php. You can press first the add button to have at least 5 rows. So then you can select the location for the first row. Then if you select the same location for second row if it is same with the previous it will automatically be selected to "Select Location". I have cater to this validation well with the codes below. The problem comes say for first row I selected Loc 1 and second row is Loc 2 and third row is Loc 1. This is allowed as long the row before and next is not the same. The problem in this scenario I remove the second row then what happens is row 1 is Loc 1 and row 2 is also Loc 2. How to revalidate when I remove a row?

$('table.dynatable tbody tr select[name^="locationFrom"]').live('change', handleLocationFromUpdate);
function handleLocationFromUpdate(index) {
    if ($(this).closest('tr').next().length == 0 && $(this).closest('tr').prev().length == 0) {
        var nextValue = $('select[name^="locationFrom"]', $(this).closest('tr').next('tr'));
        if (nextValue.val() == $(this).val()) $(this).val('');
    }
    else if ($(this).closest('tr').next().length == 0 && $(this).closest('tr').prev().length > 0) {
        //alert("Last Field");
        var prevValue = $('select[name^="locationFrom"]', $(this).closest('tr').prev('tr'));
        if (prevValue.val() == $(this).val()) $(this).val('');
    }
    else {
        var prevValue = $('select[name^="locationFrom"]', $(this).closest('tr').prev('tr'));
        var nextValue = $('select[name^="locationFrom"]', $(this).closest('tr').next('tr'));
        if (prevValue.val() == $(this).val() || nextValue.val() == $(this).val()) $(this).val('');
    }
}

Recommended Answers

All 5 Replies

You have some options, two of them are:

Option 1. Before removing one row you need to check if it will cause any rout problem. If there is a problem, you don't remove the row.

Option 2. After you remove one row, you check if that lead to any rout problem. If so, you need to alert the error or set the location to 'Select Location' on the problematic row.

Hope it helps.

Dear Ale,
I tried some thing with option 2 before you posted to me. So what I did was I run through each dropdownlist in the remove function and if there is something wrong I alert and I nullied the values. Is that right to do?

I think that works, but you can improve that, if you want.

You could perhaps, in case of conflict when removing, alert the user and offers him the choices he would have, for example:
- Delete the row and conflicted row
- Keep change (must change conflicted row)
- Cancel change

Right and wrong depends too much on the flexibility you want your use to have.

Dear Ale,
I will look into and see what is best on my side. Thank you for the confirmation.

You are welcome.

If you don't have any other doubts, please mark as solved, and if you think, as helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.