Welcome Guest, Not a member yet? Register   Sign In
Passing selected row values to hidden inputs?
#1

[eluser]Punisher[/eluser]
I'm still learning about CI and about web development so this will not be a problem for you experienced devs.
I have a view which fetches data from controlers function in a foreach loop. So my data is populated in a table. I also have a hidden form which is shown on jquery function and I would like to use it as a edit form.

example: When user clicks on selected row form pops out and values within inputs are that of selected row. When there is only one selected row it is not a problem since there is only one id but hot to do this wih multiple data. My guess is that this is done by Javascript/jQuery.

Can someone write an example how to pass a selected row from in to another input. Thanks.

Example:
Code:
<?php foreach($user as $row){ ?>
<tr>
<td> &lt;?php echo $row->something ; ? </td>
<td><a id="row_&lt;?php echo $row-&gt;something_id?&gt;" href="#">Edit</a></td>
</tr>
&lt;?php } ?&gt;


script
var a = ???
$(document).ready(function(){
$('row_a').click(function(){
//

});
});
end_script







#2

[eluser]ivantcholakov[/eluser]
Code:
&lt;?php foreach($user as $row){ ?&gt;
<tr>
<td> &lt;?php echo $row->something ; ? </td>
<td><a class="edit_action" id="row_&lt;?php echo $row-&gt;something_id?&gt;" href="#">Edit</a></td>
</tr>
&lt;?php } ?&gt;


&lt;script&gt;
$(document).ready(function(){
$('.edit_action').click(function(){

    var edit_action_id = $(this).attr('id').toString();

    alert('This link was clicked: ' + edit_action_id);

});
});
&lt;/script&gt;
#3

[eluser]Punisher[/eluser]
Thx for fast reply. :-)
So if I understand you I need to put same class on all edits and the one that was clicked will be $(this).attr('id'),

But what about other values from same row? how can I retrieve them?
#4

[eluser]jairoh_[/eluser]
in a row, you should put an id attribute.

the moment you click the row, fetch some result in the database using ajax. and then pop it out sir Smile

hope this helps.
#5

[eluser]Punisher[/eluser]
Thx. You directed me the right way. I was wondering is this the recommended way to go or is there some other "standard" devs are using for this?
#6

[eluser]ivantcholakov[/eluser]
Or, by using jQuery, you may find the parent TR element, corresponds to your link. After that, you may scan its TD children and see what is inside them. Something like this (correct my mistakes, if any):

Code:
// This piece of code is inside the event handler, it may be simplified.

var row = $(this).parent('tr');
var cells = row.children('td');
var cell_0_value = $(cells[0]).html();

alert(cell_0_value);
#7

[eluser]jairoh_[/eluser]
[quote author="Punisher" date="1376261290"]Thx. You directed me the right way. I was wondering is this the recommended way to go or is there some other "standard" devs are using for this?[/quote]
i use it. it's better doing that, than fetching already the values and hiding it and show it when you click the row. Smile
#8

[eluser]Punisher[/eluser]
I've done some thinking and may be the good solution to place unique id on all row fields by passing row id on all of them and then just fetching them by that id since the data was allready fetched from db. For example id of some td will be:

Code:
id="title_&lt;?php row-&gt;row_id?&gt;"
id="name_ &lt;?php row-&gt;row_id?&gt;"


and then each time loop passes them they will have unique id because of row_id.

And then in jQuery i can say for example:

Code:
$('.edit_string').click(function(){
                var edit_id = $(this).attr('id').toString();
                var astring = "title_"+edit_id;
                var a = $('#'+astring).text();
}




Theme © iAndrew 2016 - Forum software by © MyBB