ahmed salah

ahmed salah

  • NA
  • 530
  • 148.7k

How to map index using jquery to repair indexes when remove

Sep 17 2016 8:57 PM
Problem
when remove any course from table list html his position in center or first it make problem

in index in database

Details

IF I have list of courses as following :

Delphi

Flash

Photoshop

IF I remove flash by JQUERY remove button then click save button

it delete flash and Photoshop

because there are wrong in delete courses by using remove function in jquery

if i remove php it is working without any problem because it is last item

suggestion

using map function when remove but how to do that

if there are any solution for that without using map no problem

i use model as following
  1. public class Cusomemp2  
  2.   
  3.     {  
  4.   
  5.         public List<EmployeeCourse> empcourses { get; set; }  
  6.   
  7.     
  8.   
  9.     }  
  10.   
  11. }  
i use for edit httppost
  1. var result = db.Employees  
  2.   
  3.              .Where(p => p.Id == custom.Id)  
  4.   
  5.              .Include(c => c.EmployeeCourses)  
  6.   
  7.              .FirstOrDefault();  
  8.   
  9.         if (custom.empcourses.Any())  
  10.   
  11.         {  
  12.   
  13.             foreach (var ec in result.EmployeeCourses.ToList())//to remove existing EmployeeCourses  
  14.   
  15.             {  
  16.   
  17.                 db.EmployeeCourses.Remove(ec);  
  18.   
  19.                 db.SaveChanges();  
  20.   
  21.     
  22.   
  23.             }  
  24.   
  25.         }  
  26.   
  27.     
  28.   
  29.         result.EmployeeCourses = custom.empcourses;  
  30.   
  31.      db.SaveChanges();  

in model view my code as following by jquery

actually i need to modify code below using map function if there are any function do that never mind
  1. $(function () {  
  2.   
  3.            //add courses using drop down  
  4.   
  5.             var index = 0;  
  6.   
  7.             $("#CourseId").change(function () {  
  8.   
  9.     
  10.   
  11.                 var id = $(this).val();  
  12.   
  13.                 var txt = $("#CourseId option:selected").text();  
  14.   
  15.     
  16.   
  17.                $("#tb").append("<tr><td><input type = 'hidden' name='empcourses[" + index + "].CourseId' value='" + id + "'/></td><td>" + txt + "</td><td><input type='button' value='remove' class='r'</td></tr>")  
  18.   
  19.     
  20.   
  21.                 index++;  
  22.   
  23.     
  24.   
  25.             });  
  26.   
  27.             $("#tb").on("click"".r"function () {  
  28.   
  29.               // remove function  
  30.   
  31.                 $(this).parent().parent().remove();  
  32.   
  33.     
  34.   
  35.                 $(this).parent().prev().prev().remove();  
  36.   
  37.              });  
  38.   
  39.     
  40.   
  41. // retrieve data in edit view using ajax  
  42.   
  43.             $.ajax({  
  44.   
  45.                 url: "/Employee/getcoursesbyempid",  
  46.   
  47.                 data:{x:$("#hid").val()},  
  48.   
  49.                 success: function (res) {  
  50.   
  51.                     $.each(res, function (i, e) {  
  52.   
  53.     
  54.   
  55.                           $("#tb").append("<tr><td><input type = 'hidden' name='empcourses[" + index + "].CourseId' value='" + e.Id + "'/></td><td>" + e.CourseName + "</td><td><input type='button' value='remove' class='r'</td></tr>")  
  56.   
  57.     
  58.   
  59.                         index++;  
  60.   
  61.                     });  
  62.   
  63.                 }  
  64.   
  65.     
  66.   
  67.             })  
  68.   
  69.         });  
see image below to understand what i need


Answers (3)