0

I have multiple controller actions like adminbedroomtypesort, adminbookingstatussort, communicationtypesort, countrysort, currencysort , durationsort, featuressort, floorsort, furnishedsort, includedbillssort and one js file

My js file:

$(document).ready(function() {  
 var data = {}; 
$( "#sortable" ).sortable({
    update: function(event, ui) { 
        var update = ui.item.index();
        update = update+1;
        data['update'] = update;          
        console.log('update sortorder: '+update);
    },
    start: function(event, ui) { 
        var start = ui.item.index();
        start = start+1;
        data['start'] = start;
        console.log('start: ' +start);
    },
    stop: function (event, ui) {  
        $('#loader').show();

        $.ajax({
        type: 'POST', 
        url: Routing.generate('adminbedroomtypesort'),
        contentType: 'application/x-www-form-urlencoded',
        data: {data: data},
       success: function(result,status,xhr){
            var res = JSON.parse(result);                
             if(res){
               $('#loader').hide();
             } 
        },
        error: function(xhr, status, error) {     
                console.log(status);
            } 
    });
    }
});
$( "#sortable" ).disableSelection();
});

Controller action:

/**
 * @Route("/bookingstatus/sort", name="adminbookingstatussort", options = { "expose" = true })
 */
public function sortbookingstatusAction(Request $request) {

    if ($request->isXmlHttpRequest()) {
        $em = $this->getDoctrine()->getManager();
        //retrieve sort order from bedroomtypelistpage     
        $data = $request->request->get('data');
        //Retrieve the start position and end position(updateorder) for the sorted field
        $start = $data['start'];
        $updateorder = empty($data['update']) ? NULL : $data['update'];
        if(empty($updateorder)){
          return new Response(json_encode('success')); 
        }               
        $bedroomtype = $em->getRepository('EpitaHousingBundle:Bookingstatus')->findOneBy(array('sortorder' => $start));
        // flag -1 drag order is top to bottom
        // flag 1 drag order is bottom to top
        $flag = ($start < $updateorder) ? -1 : 1;
        $btwelements = $this->getDoctrine()
                ->getRepository(Bookingstatus::class)
                ->findBedroom($start, $updateorder, $flag);
        foreach ($btwelements as $betweenvalues) {
            $order = $betweenvalues->getSortorder();
            $betweenvalues->setSortorder($order + $flag);
        }
        $bedroomtype->setSortorder($updateorder);
        $em->flush();
        return new Response(json_encode('success'));
    }
    return new Response('Success');
}

like multiple controller actions have, here i did one controller action to one separate js file it's working fine but here i am using same js file doing multiple times change url's only. Is there any way to pass url: Routing.generate('adminbedroomtypesort'), multiple url's to one js file corresponding controller actions help me any one. Thanks for advance...

somesh
  • 528
  • 2
  • 10
  • 26
  • Have you tried https://stackoverflow.com/a/42341050/789213 or https://stackoverflow.com/a/22853154/789213? – Garfield Oct 06 '17 at 11:52
  • Here i want to pass multiple controller actions to one js file url: Routing.generate('adminbedroomtypesort') inside ajax depends up on selection of controller actions – somesh Oct 06 '17 at 11:55
  • js file same for every controller action but only one change ajax routing url i have multiple action names are adminbedroomtypesort, adminbookingstatussort, communicationtypesort, countrysort, currencysort , durationsort, featuressort, floorsort, furnishedsort, includedbillssort – somesh Oct 06 '17 at 11:57
  • just wrap the ajax bit inside a function with accepts the url as a parameter, and pass that parameter to the ajax call instead of hard-coding it. Just like any program function - that's the whole point of functions, to create re-usable code. Possibly you'll find to need to allow variation of other parameters too, such as the data to send, the callback to run when ajax returns, etc etc. But the basic concept is not hard - if you want to re-use the ajax code, then create a re-usable function which accepts the URL as an input variable and then runs the ajax code, incorporating that URL variable – ADyson Oct 06 '17 at 12:44
  • Thanks to every one i got the solution – somesh Oct 06 '17 at 13:01
  • var urls = document.getElementById("sort").innerHTML; var url = ''; if(urls === 'Bedroom Types'){ url = 'adminbedroomtypesort'; } else if(urls === 'BookingStatus Types'){ url = 'adminbookingstatussort'; } – somesh Oct 06 '17 at 13:01
  • I target div id to get every url based on if statements – somesh Oct 06 '17 at 13:02
  • Now i able to reuse js file to every controller action Thanks for your valuable suggestions – somesh Oct 06 '17 at 13:04

0 Answers0