Welcome Guest, Not a member yet? Register   Sign In
Javascript - array of objects, exchage values
#1

[eluser]SPeed_FANat1c[/eluser]
I have generated these input fields:

Code:
<div class="places">
  &lt;input type="hidden" name="link_1" value="0"&gt;
        
  &lt;input type="hidden" name="link_2" value="1"&gt;
        
  &lt;input type="hidden" name="link_3" value="2"&gt;
</div>

I then make an array of these

Code:
places = $(":hidden", ".places").serializeArray();

Now I have array of objects. It represesnts links on the sidebar with places. 0 is the highest place, 2 is lowest place.

I have a table with these links and buttons up, down next to each link in admin panel. So when I press down on the link_1, I want its value to become 1, because now it is 0, and value of link_2 become 0.

So I have to find in array of objects which link has value 1. Then assign to it value 0, and assign value 1 to link_1.

How can I do it? Or I am doing it too complicated? Maybe there is more simple solution (without page reload)?

Here is how it looks.

[Image: nuorodosjpg.th.jpg]
#2

[eluser]teampoop[/eluser]
Probably your best approach would be to set all of the hidden fields to zero, then assign the selected one the correct value. I am assuming you have the following
Code:
<ul class="the_links">
  <li><a href="link" id="link_l">link 1</a></li>
  <li><a href="link" id="link_2">link 2</a></li>
  <li><a href="link" id="link_3">link 3</a></li>
</ul>

<div class="places">
  &lt;input type="hidden" name="link_1" value="0"&gt;
  &lt;input type="hidden" name="link_2" value="1"&gt;
  &lt;input type="hidden" name="link_3" value="2"&gt;
</div>

Your JS would be something like so:
Code:
$("a", ".the_links").click(function(){
  $a = $(":hidden", ".places");  // gets your nodes
  $a.each(function(){ $(this).val(0);});  // sets all to zero
  $a.find("[name="+$(this).attr('id')+"}").val(1);  // finds proper name and sets value
});

Now this is just a quick thought and I haven't tested it, but I think it's on the right track.

HTH
#3

[eluser]SPeed_FANat1c[/eluser]
you mean by setting all fields to 0 and only assigning 1 to which had moved down?

What if admin moves the link down several times (e.g. from place 0 to place 2) Ok, we could increase that value by one on each move down.

Then admin wants to move down another link, then suddenly he decides to move up the link he moved donn earlier. This gets so confusing. And by sending that data to server, we will have to have a php function which figures out what link is in what place by those numbers. And then save to database.

What I want is to have some array with current places after all movements up and down so I could send it to a controller which simply saves it to database.

The table look like this:
Code:
<tbody><tr>
<th>Pavadinimas</th><th>Nuoroda</th><th>Apibūdinimas</th><th>Redagavimas</th><th>Trynimas</th><th>Kilnojimas</th></tr>
<tr>
<td>test</td><td>www.google.lt</td><td>paieska</td><td><a href="http://localhost/darzelis/admin_/nuorodos/redaguoti/1"><img src="http://localhost/darzelis/images/admin/application_form_edit.png"></a></td><td><a class="trinti" id="trinti_1" href="http://localhost/darzelis/admin_/nuorodos/trinti/1"><img src="http://localhost/darzelis/images/admin/delete.png"></a></td><td><a class="down" id="down_1" href="#"><img src="http://localhost/darzelis/images/admin/arrow_down.png"></a></td></tr>
<tr>
<td>asd</td><td>http://asd.lt</td><td>apib</td><td><a href="http://localhost/darzelis/admin_/nuorodos/redaguoti/2"><img src="http://localhost/darzelis/images/admin/application_form_edit.png"></a></td><td><a class="trinti" id="trinti_2" href="http://localhost/darzelis/admin_/nuorodos/trinti/2"><img src="http://localhost/darzelis/images/admin/delete.png"></a></td><td><a class="up" id="up_2" href="#"><img src="http://localhost/darzelis/images/admin/arrow_up.png"></a><a class="down" id="down_2" href="#"><img src="http://localhost/darzelis/images/admin/arrow_down.png"></a></td></tr>
<tr>
<td>asd</td><td>http://asd.lt</td><td>apib</td><td><a href="http://localhost/darzelis/admin_/nuorodos/redaguoti/3"><img src="http://localhost/darzelis/images/admin/application_form_edit.png"></a></td><td><a class="trinti" id="trinti_3" href="http://localhost/darzelis/admin_/nuorodos/trinti/3"><img src="http://localhost/darzelis/images/admin/delete.png"></a></td><td><a class="up" id="up_3" href="#"><img src="http://localhost/darzelis/images/admin/arrow_up.png"></a></td></tr>
</tbody>


I am trying now do this:

Code:
$('a.down').live('click',function(){
         id = this.id.replace('down_', "");
         place = -1;
         //find a place of link
        
         $.each(places, function(i, field){
            if(field.name == 'link_'+id)
            {
                place = field.value;
                console.log('vieta ' + place);
            }
         });
                  
         //find an id of the lower link
         lower_link_id = -1;
         $.each(places, function(i, field){
            console.log('tikrinam ' + field.value + (place+1));
            place_of_lower_link = place + 1;
            
            if(field.value == place_of_lower_link)
            {    
                lower_link_id = (field.name).replace('link_',"");
                
            }
         });
        
         console.log(lower_link_id);
        
        
         return false;
     });

It is not finished. But this code is also so confusing. How the hell can be so much code to change only two values? Big Grin In codeigniter php + mysql with page refresh on each click there would be few lines Smile


Edit: I am thinking now that there is no need to only update database when all movements are finished. I'll update database on each move up or down. It uses more server resourses but still that is so small usage that nobody ever notice Smile so there is no need to do it too perfect. Will do it tomorow, today I am sick of this javascript - so much time wasted and no result.
Thanks for the replay btw Smile
#4

[eluser]SPeed_FANat1c[/eluser]
Just made change with codeigniter and sql. It is so much faster to code it.

Code:
function move_down($link_id)
    {
        //find a place of link with current id
          $query = $this->db->select('vieta')->where('link_id',$link_id)->get('nuorodos');
          $row = $query->row();
          $place = $row->vieta;

          //find an id of lower link
          
          $query = $this->db->select('link_id')->where('vieta',$place+1)->get('nuorodos');
          $row = $query->row();
          $lower_link_id  = $row->link_id;
          
          //assign a new place for the current link
                    
          $data = array(
              'vieta' => $place + 1
          );
          $query = $this->db->where('link_id',$link_id)->update('nuorodos', $data);
          
          //assign a new place for lower link
          if($query)
          {
              $data = array(
                  'vieta' => $place
              );
              $query = $this->db->where('link_id',$lower_link_id)->update('nuorodos', $data);
            
              if($query)
                  $return_value = 'ok';
              else $return_value = 'change_not_made';
          }
          else $return_value = 'change_not_made';
          
          return $return_value;
    }

I would like to ask is it necessary to check if update was succesfull? Those additional checking lines look not nice, and it is so litte chance that sql server will shut down or something. How do you think?




Theme © iAndrew 2016 - Forum software by © MyBB