【发布时间】:2019-07-30 10:40:11
【问题描述】:
我想为 jquery 数据表的每一行添加编辑和删除按钮,例如,我想在每次更新和删除时更新数据库。我已使用 JSON 填充数据库中的数据表并添加了这些按钮,但问题是我不知道如何设置按钮(链接)以重定向到包含完整信息的 jsp 编辑页面。
JS代码
$(document).ready(function(){
var $table = $('#articleListTable');
if($table.length){
$table.DataTable({
lengthMenu : [[5,10,20,-1],['5', '10' , '20' , 'All']],
pageLength : 10,
ajax: {
type:'GET',
url: 'loadArticle',
dataSrc: ''
},
columns: [
{ data :'codeArt'},
{ data :'rubrique.designation'},
{ data :'fournisseur.nomFournisseur'},
{ data :'refArt'},
{ data :'designationC'},
{ data :'quantite'},
{
defaultContent: '<a class="btn btn-xs btn-default" href="#" role="button"><span class="glyphicon glyphicon-file " aria-hidden="true"></span></a> <a href="/article/modifier/1" class="btn btn-xs btn-success" role="button"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a> <a href="javascript:void(0);" class="btn btn-xs btn-danger" role="button" data-toggle="modal" data-target="#modalArticle${article.getIdArticle()}"><span class="glyphicon glyphicon-trash " aria-hidden="true"></span>'
}
]
});
}
控制器代码
package com.gestion.mvc.controllers;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gestion.mvc.entities.Article;
import com.gestion.mvc.entities.Fournisseur;
import com.gestion.mvc.entities.Rubrique;
import com.gestion.mvc.export.FileExporter;
import com.gestion.mvc.services.IArticleService;
import com.gestion.mvc.services.IFournisseurService;
import com.gestion.mvc.services.IRubriqueService;
import com.google.gson.Gson;
@Controller
@RequestMapping(value = "/article")
public class ArticleController {
@Autowired
private IArticleService articleService;
@Autowired
private IRubriqueService rubriqueService;
@Autowired
private IFournisseurService fournisseurService;
@Autowired
@Qualifier("articleExporter")
private FileExporter exporter;
@RequestMapping(value="/loadArticle")
@ResponseBody
public List<Article> JSONArticle () {
return articleService.selectAll();
}
@RequestMapping(value = "/modifier/{idArticle}")
public String editArticle(Model model, @PathVariable Long idArticle) {
if (idArticle != null) {
Article article = articleService.getById(idArticle);
List<Rubrique> listRubrique = rubriqueService.selectAll();
List<Fournisseur> listFournisseur = fournisseurService.selectAll();
if (listRubrique == null) {
listRubrique = new ArrayList<Rubrique>();
}
if(listFournisseur == null) {
listFournisseur = new ArrayList<Fournisseur();
}
model.addAttribute("listRub", listRubrique);
model.addAttribute("listFrn", listFournisseur);
if (article != null) {
model.addAttribute("article", article);
}
}
return "article/ajouterArticle";
}
@RequestMapping(value = "/supprimer/{idArticle}")
public String deleteArticle(Model model, @PathVariable Long idArticle) {
if (idArticle != null) {
Article article = articleService.getById(idArticle);
if (article != null) {
//TODO verification avant suppression
articleService.remove(idArticle);
}
}
return "redirect:/article/";
}
}
这是显示我所做的图片。
【问题讨论】:
标签: javascript jquery spring-mvc datatable datatables