$(function() {
$("body").on("click",".set-topic",function(){
/*获取所有题目接口*/
var subjectType =$(this).attr("data-type");
var tid = $(this).attr("data-id");
var url = '/questions?subjectType='+subjectType;
var params = {
"tid":tid
};
$.ajax({
url: url,
type: 'GET',
data:params,
success: function(data) {
$("body").find(".topicList").html(data);
var allIds = $("body").find("input[name=Tids]").attr("value");
var arrryId=allIds.split(",");/*split() 方法用于把一个字符串分割成字符串数组。*/
$("body").find(".topicList").find(".layui-unselect").click(function(){
var $this = $(this);
var pId = $this.parents("tr").attr("data-id");
getInputIds($this,arrryId,pId);
})
bindEvent();
}
});
function getInputIds($this,arrryId,pId){
if($this.hasClass("layui-form-checked")){
$this.removeClass("layui-form-checked");
arrryId.removeByValue(pId);
$("body").find("input[name=Tids]").attr("value",arrryId);
}else{
$this.addClass("layui-form-checked");
arrryId.push(pId);
var stringIds = arrryId.join(",")
$("body").find("input[name=Tids]").attr("value",stringIds);
}
}
/**根据值删除数组元素**/
Array.prototype.removeByValue = function(val) {
for(var i=0; i<this.length; i++) {
if(this[i] == val) {
this.splice(i, 1);
break;
}
}
}
function bindEvent(){
/**弹出框中的翻页**/
$('.pagination').find('a').click(function(){
var allIds = $("body").find("input[name=Tids]").attr("value");
var arrryId=allIds.split(",")
$.get(this.href, function(data){
$("body").find(".topicList").html(data);
$("body").find("input[name=Tids]").attr("value",arrryId);
/*回显*/
$("body").find(".topicList").find(".layui-unselect").each(function(){
var $this = $(this);
var pId = $this.parents("tr").attr("data-id");
if(allIds.indexOf(pId)>=0){
$this.addClass("layui-form-checked");
}else{
$this.removeClass("layui-form-checked");
}
})
$("body").find(".topicList").find(".layui-unselect").click(function(){
var $this = $(this);
var pId = $this.parents("tr").attr("data-id");
getInputIds($this,arrryId,pId);
})
bindEvent();
})
return false;
});
}
/* 提交选中题目*/
$("#Modal").find(".btn-primary").unbind("click").click(function(){
var url = '/topic/'+tid+'/set_questions';
var qids=$("body").find("input[name=Tids]").attr("value");
if (qids.substr(0,1)==','){
s=qids.substr(1);
}
if(qids==""){
}
var params = {
tid:tid,
qids:qids
}
$.ajax({
url:url,
method:'post',
data:params,
success:function(data){
location.reload();
}
})
})
})
$("#topics-ip").click(function(){
/*获取所有专题接口*/
var theadTr = '<tr><td>序号</td><td>专题</td><td>费用</td></tr>';
$("#a-thead").html(theadTr);
var url = '/topics/json';
getTopics(url);
});
$("#topics-ip").click(function(){
/*获取所有专题接口*/
var theadTr = '<tr><td>序号</td><td>专题</td><td>费用</td></tr>';
$("#a-thead").html(theadTr);
var url = '/topics/json';
getTopics(url);
/*检索专题*/
$("body").find(".seach-p-btn").unbind("click").click(function(){
var key = $(this).siblings(".layui-input").val();
var url='/topics?key='+key;
getTopics(url);
});
/*输入框实时搜索*/
$("body").on("input",".layui-input",function(){
var key = $(this).val();
var url='/topics?key='+key;
getTopics(url);
});
});
$("#practicer-ip").click(function(){
/*获取所有练习者接口*/
var theadTr = '<tr><td>序号</td><td>姓名</td><td>邮箱</td></tr>';
$("#a-thead").html(theadTr);
var url = '/users';
getUser(url);
/*检索练习者*/
$("body").find(".seach-p-btn").unbind("click").click(function(){
var key = $(this).siblings(".layui-input").val();
var url='/users?key='+key;
getUser(url);
});
/*输入框实时搜索*/
$("body").on("input",".layui-input",function(){
var key = $(this).val();
var url='/users?key='+key;
getUser(url);
});
});
function getUser(url){
$.ajax({
url: url,
type: 'GET',
success: function(data) {
if(data.code==200){
console.log(data)
var n = data.users.length;
if(n>0){
$("#a-list").html("");
for(var i=0;i<n;i++){
var index = i+1
var tr = '<tr data-id="'
+data.users[i].id
+'" data-name="'
+data.users[i].name
+'"><td>'
+index
+'</td><td>'
+data.users[i].name
+'</td><td>'
+data.users[i].email
+'</td></tr>';
$("#a-list").append(tr);
}
}else{
var tr = '<tr><td colspan="3">暂无信息</td></tr>';
$("#a-list").html(tr);
}
/* 选中专题*/
$("#a-list").find("tr").click(function(){
$(this).toggleClass("tr-checked")
$(this).siblings().removeClass("tr-checked");
})
}else{
layer.msg("获取专题失败,请刷新后重试!")
}
}
})
/* 提交选中练习者*/
$("#Modal").find(".btn-primary").unbind("click").click(function(){
var title = $("#a-list").find(".tr-checked").attr("data-name");
var id = $("#a-list").find(".tr-checked").attr("data-id");
$("#practicer-ip").val(title);
$("input[name=toAuthorizerId]").val(id)
$("#Modal").modal("hide");
})
}
function getTopics(url){
$.ajax({
url: url,
type: 'GET',
success: function(data) {
if(data.code==200){
var m = data.topics;
console.log(data)
console.log(m)
console.log(333)
var n = data.topics.length;
var m = data.topics;
console.log(m)
if(n>0){
$("#a-list").html("");
for(var i=0;i<n;i++){
var index = i+1
var tr = '<tr data-id="'
+data.topics[i].id
+'" data-title="'
+data.topics[i].title
+'"><td>'
+index
+'</td><td>'
+data.topics[i].title
+'</td><td>'
+data.topics[i].fee
+'</td></tr>';
$("#a-list").append(tr);
}
}else{
var tr = '<tr><td colspan="3">暂无信息</td></tr>';
$("#a-list").html(tr);
}
/* 选中专题*/
$("#a-list").find("tr").click(function(){
$(this).toggleClass("tr-checked")
$(this).siblings().removeClass("tr-checked");
})
}else{
layer.msg("获取专题失败,请刷新后重试!")
}
}
})
/* 提交选中专题*/
$("#Modal").find(".btn-primary").unbind("click").click(function(){
var title = $("#a-list").find(".tr-checked").attr("data-title");
var id = $("#a-list").find(".tr-checked").attr("data-id");
$("#topics-ip").val(title);
$("input[name=topicId]").val(id)
$("#Modal").modal("hide");
})
}
})