views:

76

answers:

3

i have a html table where one of the columns is a set of checkboxes

There are three checkboxes in each row. The original names of the checkboxes are

Row 1: person[0].Choices (value=1 name= person[0].Choices value=2 person[0].Choices, etc. .)
Row 2: person[1].Choices(value=1 name= person[1].Choices value=2 person[1].Choices, etc . .)
Row 3: person[2].Choices(value=1 name= person[2].Choices value=2 person[2].Choices, etc . .)

i want to:

  1. Delete the first row of the html table.
  2. Rename all of the checkbox indexers so at the end of it, there are two left

Row 1: person[0].Choices (value=1 name= person[0].Choices value=2 person[0].Choices, etc. .)
Row 2: person[1].Choices(value=1 name= person[1].Choices value=2 person[1].Choices, etc . .)

but note that since the first row has been deleted what was checked in Row 2 before is now in Row 1 and what used to be in Row 3 is now in Row 2, etc.

Can this be done through jquery or javascript as i need them to be in consecutive order for the default asp.net mvc binding to work.

EDIT

i found an image that describes what my table looks like to hopefully clarify the point. http://weblogs.asp.net/blogs/psperanza/CheckboxGrid_6F9D4218.png

+2  A: 

I believe you can just name them person[] and they will be indexed automatically when the form is submit.

As per your updated question, it seems like the simplest way would be to use the jQuery attribute manipulator mentioned in another answer. Still... It feels like there is a better way to do this.

As for the jQuery, you should add a class (like 'person') to each of the form elements, and then use this: (untested)

$('#person:first').remove()
Carson Myers
im confused. what do you mean call them person. i somehow need to differentiate between the checkboxes on rows 1,2,3, right ?
ooo
no, you don't. If you name them `person[]`, then their values will be indexed in the order that the inputs appear in the source when the form is submitted.
Carson Myers
but even for checkboxes where there are multiple entries per index. to clarify, every checkbox in the first row should be part of person[0], every checkbox in the second row should be part of person[1]
ooo
i just tested this with checkboxes and if you dont explicitally put the index on person[index].Choices then it shows up null in the binding object in the controller action
ooo
i added a picture in the question to clarify
ooo
I see what you're doing there. It was hard to tell from the sort-of layout you posted at first. The remove-a-row part of my answer still stands, but I hope you have each row in a <div class='whatever'> and didn't just use a table for it...
Carson Myers
i have the remove a row working now but im stuck on the binding to the checkboxes . ..
ooo
look at Erik's answer, you'll have to replace `selector` with and ID for each checkbox you want to change, then you set the `name` attribute of each one accordingly. see http://jquery.com
Carson Myers
A: 

I'm not sure I 100% understand your question, but you can change the name of elements pretty easily, or any other attribute like this:

$(selector).attr('name','new_input_name');
Erik
i understand that but my issue is purely with checkboxes as i need all the checkboxes in each row to keep the same indexer
ooo
A: 

i got it working through some other SO feedback and put the response below. If anyone thinks this can be optimized, please let me know . .

 <script type="text/javascript">

    $(document).ready(function() {

        $(".removeButtonQuestion").live("click", function(event) {

            debugger;

            var row = $(this).closest("tr").get(0).rowIndex;
            var col = $(this).closest("td").get(0).cellIndex;

            $(this).closest("tr").remove();

            var input = $("#questionsTable :checkbox");

            for (i = 0; i <= input.length; i++) {

                var checkbox = input[i];
                var row1 = $(checkbox).closest('tr').get(0);
                var rowIndex = row1.rowIndex;
                if (rowIndex >= row) {
                    var newRowIndex = rowIndex - 1;
                    $(checkbox).attr('name', 'updater.person[' + newRowIndex + '].Choices');
                }
            }
        });
    });
</script>
ooo