c# - JQuery MVC collection name attributes -


i have table, , i've dynamically added / removed rows it. table contains fields posted collection mvc. in scenario updating table jquery means collections contain incomplete collections example ...

function add() {      $.ajax({             url: "getrow",             type: 'post',             datatype: 'html',             timeout: 30000,             data: {                 rowindex: otherrows.length,                 "item.property1": $("option:selected", dropdown).text(),                 "item.property2": $("option:selected", dropdown).val()             },             success: function (result) {                 var newrow = $(result);                 tablebody.append(newrow);             }         }); } 

this function makes ajax call go mvc action result returns result of row @ given index , additionally sets default values drop down on page.

now lets assume have similar function called "delete" in using jquery remove row , @ point table has 3 rows (ignoring header row of course), , row i'm removing middle row.

this means fields in rows end looking ...

//row 1: <input type="text" name="somecollection[0].property1" />   //row 2:    nothing, deleted of course  //row 3:  <input type="text" name="somecollection[2].property1" />  

so if postback, because of inconsistent id range, mvc model binder bind item 1 me , item 2 (actually item 3 prior client side delete) not mapped.

the idea want server logic simple "if id item in collection not in post data, delete database", way collection manipulation can entirely client side in save on constant postbacks on heavy page.

so started putting function in jquery fix problem ...

function updatenames(table) {     var rows = $("tr", table);     var index = 0;      rows.each(function () {         var inputfields = $("input");          inputfields.each(function (){           //replace name="bla[<any number>].anything"            //        name="bla[" + index + "].anything"                 });          index++;     }); } 

so question ... how jquery "replace [] in name attribute [index]"?

i know wont solve problem of nested collections / other such complex scenarios once have function solved can extend later , requirements not involved yet.

edit:

some additional detail on current thought pattern ... or bad?

if grab name attribute value , "walk chars" until find first "[" , next "]" replace in between, should solve problem, type of thing on ie slow hell.

anyone got neat "just call clever function" type answer? (if there one).

edit 2:

wow gotta harder ... dumbass feel right 2 reasons (not looking , regex obvious solution here)

jquery: how replace between characters?

if can figure out right regex i've got solution (maybe that's question should have asked i'm annoyed crazyness of regex).

but power of regex cannot under estimated :)

this should work you:

function updatenames(table) {      var rows = $("tr", table);     var index = 0;      rows.each(function () {         var inputfields = $(this).find("input");         inputfields.each(function (){             var currentname = $(this).attr("name");             $(this).attr("name", currentname.replace(/\[(.*?)\]/, '['+index+']'));           });         index++;      }); } 

if there multiple inputs inside each row , want update 1 consider using 'starts with' jquery selector: http://api.jquery.com/attribute-starts-with-selector/.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -