SitePoint Sponsor

User Tag List

Page 11 of 13 FirstFirst ... 78910111213 LastLast
Results 251 to 275 of 310
  1. #251
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Ok now I have this function for add. So I call the setRowIndex(row, index) from this add function is it? The row will represent which row?


    $("table.dynatable button.add").click(function() {
    var form = this.form;
    id++;

    // Get a new row based on the prototype row
    //var prot = master.find(".prototype").clone();
    var prot = $(form).data('prototype').clone();
    prot.find('[name^="id"]').attr("value", id);
    prot.find('[name^="locationFrom"]').attr('name', 'locationFrom[' + id + ']');
    prot.find('[name^="locationFrom"]').attr('id', 'locationFrom' + id + '');
    prot.find('[name^="locationFrom"]').attr('value', '');
    prot.find('[name^="eventFrom"]').attr('name', 'eventFrom[' + id + ']');
    prot.find('[name^="eventFrom"]').attr('value', '');
    prot.find('[name^="locationTo"]').attr('name', 'locationTo[' + id + ']');
    prot.find('[name^="locationTo"]').attr('id', 'locationTo' + id + '');
    prot.find('[name^="locationTo"]').attr('value', '');
    prot.find('[name^="eventTo"]').attr('name', 'eventTo[' + id + ']');
    prot.find('[name^="eventTo"]').attr('value', '');

    $("#dynamicRow").find('tbody').append(prot);

    return false;
    });

  2. #252
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Paul,
    Ok now I have this function for add. So I call the setRowIndex(row, index) from this add function is it? The row will represent which row?
    That's benefit of passing the actual row to the function. It can represent whichever row needs to be updated.

    So you might call the function to set the last row to have an index that's one more than the total number of rows, after which the function can use used to set the appropriate index number on the new row that's about to be added.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #253
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Never mind let me try passing the row by myself. I will get back to once I am stuck. What is the command to update the rows index?

  4. #254
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Paul,
    Never mind let me try passing the row by myself. I will get back to once I am stuck. What is the command to update the rows index?
    There currently is none, which is why I suggested that one be written to do so.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #255
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    I was thinking since only the last row is going to be update then we just send the index will do right. Is it the code have to be something below. Where I get the the last row first select and try to update the id and many more id to be updated? So once I set the new id for the last row will the new row automatically be inserted just before the last row is it?

    $("table.dynatable tbody tr:last td:nth-child(1) select").id

  6. #256
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    I was thinking since only the last row is going to be update then we just send the index will do right. Is it the code have to be something below. Where I get the the last row first select and try to update the id and many more id to be updated?
    Yes, that would work too, but it does have many dependencies within it. Typically, the less dependencies the better, because that then results in there being less ways that the code can fail.

    Quote Originally Posted by newtomysql View Post
    So once I set the new id for the last row will the new row automatically be inserted just before the last row is it?
    Nope, that sort of thing doesn't happen automatically. More code needs to be written to achieve that. That's also why writing functions with an eye for reuse, can be very useful too.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #257
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    To be frank with looks like the effort require is going to be a lot right. Do you think is worth doing it this way where we have to keep sorting the rows? I am thinking why not do the final check upon submit what is your opinion?

  8. #258
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    I am thinking why not do the final check upon submit what is your opinion?
    It would be useful if the validator allowed you to make such changes just before performing its validation routine, but the validator doesn't provide any such hooks.

    An event such as onbeforesubmit would allow us to set up those rules before the validator processes the submit button, but since such an event doesn't exist, we can't.

    It's a good idea though for a future version of the validator though.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #259
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    What is your opinion do you think should I do it via javascript which I feel a bit more comfortable with? Just to get your frank opinion as you are the guru right.

  10. #260
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    What is your opinion do you think should I do it via javascript which I feel a bit more comfortable with?
    I think that if someone changes a select box field, that all other fields that also need to be changed should be changed automatically by the script.

    That way you won't need to create additional validation routines.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #261
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    How is your idea? I dont quite get it clearly when you say change one then change another automatically? Any code snippet?

  12. #262
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    How is your idea? I dont quite get it clearly when you say change one then change another automatically?
    Isn't it true that when either the locationFrom or locationTo changes, that other fields should change in relation to that?

    Quote Originally Posted by newtomysql View Post
    Any code snippet?
    I have no code snippets that have been previously prepared for your situation, but I'm happy to help you write your own code for this situation.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  13. #263
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    So if that is the case so where should now I start from? What should be my first step to do the codes?

  14. #264
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    So if that is the case so where should now I start from? What should be my first step to do the codes?
    I would add code for the first row locationTo field so that any changes to it cause the locationFrom on the same row to change as well.
    Then add code to the last row for locationFrom and locationTo to do the same job.
    Then you can figure out commonalities between those functions so that you can refactor the work to a single function instead.

    After that, you can then focus on the added rows.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #265
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    For the first part your said capture the changes for the first locationTo. So can I do something below? What to add in the details of the function? I dont know how to enforce the changes to the locationFrom when locationTo changes?
    $("table.dynatable tbody tr:first td:nth-child(4)").change( function()
    {

    }
    );
    For the last row should I do something like this?

    $("table.dynatable tbody tr:last td:nth-child(2)").change( function()
    {

    }
    );

  16. #266
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    For the first part your said capture the changes for the first locationTo. So can I do something below? What to add in the details of the function? I dont know how to enforce the changes to the locationFrom when locationTo changes?
    Please stop using td:nth-child(4) as it's fragile and more difficult to interpret than select[name^="locationTo"]

    The code to enforce changes to locationFrom is very similar to the code you already have that enforces changes to locationTo
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  17. #267
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Below is how it looks like is this correct? So where to call this code from then?

    var locationTo = $('[name^="locationTo"]', row);
    var targetId = $('[name^="locationFrom"]', row).attr('id');
    rule = {
    required: true,
    noEequalTo: '#' + targetId
    };
    $(locationTo).rules('add', rule);

  18. #268
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Below is how it looks like is this correct? So where to call this code from then?
    A validation is not what is called for here. What is more appropriate here is to set the other select field to match the one that has just been selected.

    Look at lines 133 to 138 of your existing code for an example.

    It could be rewritten though so that the nth-child parts don't cause any confusion, which would make it something like this:

    Code javascript:
    $('table.dynatable tbody tr:first select[name^="locationFrom"]').change( function() {
        var otherField = $('select[name^="locationTo"]', $(this).parents('tr'));
        otherField.val(this.value);
    });
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  19. #269
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Based on your suggestion I have done both for first and last row as below and it works well. So how about the middle rows now to make sure they are not same in terms of locationFrom and locationTo and locationFrom of a new row to be same from the previous locationTo?

    $('table.dynatable tbody tr:first select[name^="locationFrom"]').change( function() {
    var otherField = $('select[name^="locationTo"]', $(this).parents('tr'));
    otherField.val(this.value);
    });

    $('table.dynatable tbody tr:first select[name^="locationTo"]').change( function() {
    var otherField = $('select[name^="locationFrom"]', $(this).parents('tr'));
    otherField.val(this.value);
    });

    $('table.dynatable tbody tr:last select[name^="locationFrom"]').change( function() {
    var otherField = $('select[name^="locationTo"]', $(this).parents('tr'));
    otherField.val(this.value);
    });

    $('table.dynatable tbody tr:last select[name^="locationTo"]').change( function() {
    var otherField = $('select[name^="locationFrom"]', $(this).parents('tr'));
    otherField.val(this.value);
    });

  20. #270
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    So how about the middle rows now to make sure they are not same in terms of locationFrom and locationTo and locationFrom of a new row to be same from the previous locationTo?
    Start off with the second part.

    The first row locationTo needs to set the next row locationFrom.
    The last row locationFrom needs to set the previous row locationTo.

    When you add a new row, that new row's locationFrom needs to set the previous row locationTo, and the new row's locationTo needs to set the next row's locationFrom
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  21. #271
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    For the second case is the code will look something below. But where exactly to call this code from?

    var otherField = $('select[name^="locationTo"]', closest('tr').prev('tr'));
    otherField.val(this.value);

  22. #272
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    But where exactly to call this code from?
    You would call it from the change event of the new locationFrom field.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  23. #273
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    I dont know how to do such that $('table.dynatable tbody tr:last select[name^="locationTo"]').change( function() { the onchange is for not the last or first row? How to do that ya?

  24. #274
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    I dont know how to do such that $('table.dynatable tbody tr:last select[name^="locationTo"]').change( function() { the onchange is for not the last or first row? How to do that ya?
    When the about-to-be-added-row is stored in a variable and before it's added to the DOM, is a good time to do it.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  25. #275
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    873
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Below is how I plan to do it? Can you comment?

    $("table.dynatable button.add").click(function() {
    var form = this.form;
    id++;

    // Get a new row based on the prototype row
    //var prot = master.find(".prototype").clone();
    var prot = $(form).data('prototype').clone();
    prot.find('[name^="id"]').attr("value", id);
    prot.find('[name^="locationFrom"]').attr('name', 'locationFrom[' + id + ']');
    prot.find('[name^="locationFrom"]').attr('id', 'locationFrom' + id + '');
    prot.find('[name^="locationFrom"]').attr('value', '');
    prot.find('[name^="eventFrom"]').attr('name', 'eventFrom[' + id + ']');
    prot.find('[name^="eventFrom"]').attr('value', '');
    prot.find('[name^="locationTo"]').attr('name', 'locationTo[' + id + ']');
    prot.find('[name^="locationTo"]').attr('id', 'locationTo' + id + '');
    prot.find('[name^="locationTo"]').attr('value', '');
    prot.find('[name^="eventTo"]').attr('name', 'eventTo[' + id + ']');
    prot.find('[name^="eventTo"]').attr('value', '');

    $("#dynamicRow").find('tbody').append(prot);

    var otherField = $('select[name^="locationTo"]', closest('tr').prev('tr'));
    otherField.val(this.value);



    return false;
    });


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •