Commit 3098019a by aa.gusti

perbaikan detable + checkbox

1 parent 7472c8b0
...@@ -136,7 +136,7 @@ class BaseView(object): ...@@ -136,7 +136,7 @@ class BaseView(object):
self.list_buttons = (btn_add, btn_close) self.list_buttons = (btn_add, btn_close)
self.columns = None self.columns = None
self.form_params = dict(scripts="") self.form_params = dict(scripts="")
self.list_url = [] self.list_url = ""
self.list_route = '' self.list_route = ''
self.list_schema = colander.Schema self.list_schema = colander.Schema
self.allow_view = True self.allow_view = True
...@@ -145,8 +145,12 @@ class BaseView(object): ...@@ -145,8 +145,12 @@ class BaseView(object):
self.allow_post = False self.allow_post = False
self.allow_unpost = False self.allow_unpost = False
self.allow_check = False self.allow_check = False
self.check_field = ""
self.state_save = False self.state_save = False
self.server_side = True self.server_side = True
self.scroll_y = False
self.scroll_x = False
self.list_form = None self.list_form = None
self.form_list = None self.form_list = None
self.filter_columns = False self.filter_columns = False
...@@ -238,7 +242,8 @@ class BaseView(object): ...@@ -238,7 +242,8 @@ class BaseView(object):
return r return r
def view_list(self, **kwargs): def view_list(self, **kwargs):
if self.list_schema: """
custom:
allow_view = kwargs.get("allow_view", self.allow_view) allow_view = kwargs.get("allow_view", self.allow_view)
allow_edit = kwargs.get("allow_edit", self.allow_edit) allow_edit = kwargs.get("allow_edit", self.allow_edit)
allow_delete = kwargs.get("allow_delete", self.allow_delete) allow_delete = kwargs.get("allow_delete", self.allow_delete)
...@@ -248,20 +253,45 @@ class BaseView(object): ...@@ -248,20 +253,45 @@ class BaseView(object):
state_save = kwargs.get("state_save", self.state_save) state_save = kwargs.get("state_save", self.state_save)
filter_columns = kwargs.get("filter_columns", self.filter_columns) filter_columns = kwargs.get("filter_columns", self.filter_columns)
server_side = kwargs.get("server_side", self.server_side) server_side = kwargs.get("server_side", self.server_side)
new_buttons
list_url
action_suffix
"""
allow_view = kwargs.get("allow_view", self.allow_view)
allow_edit = kwargs.get("allow_edit", self.allow_edit)
allow_delete = kwargs.get("allow_delete", self.allow_delete)
allow_post = kwargs.get("allow_post", self.allow_post)
allow_unpost = kwargs.get("allow_unpost", self.allow_unpost)
allow_check = kwargs.get("allow_check", self.allow_check)
check_field = kwargs.get("check_field", self.check_field)
state_save = kwargs.get("state_save", self.state_save)
filter_columns = kwargs.get("filter_columns", self.filter_columns)
server_side = kwargs.get("server_side", self.server_side)
new_buttons = kwargs.get("new_buttons")
is_object = kwargs.get("is_object")
list_url = kwargs.get("list_url", self.list_url)
action_suffix = kwargs.get("action_suffix", self.action_suffix)
list_schema = kwargs.get("list_schema", self.list_schema)
scroll_y = kwargs.get("scroll_y", self.scroll_y)
scroll_x = kwargs.get("scroll_x", self.scroll_x)
parent = kwargs.get("parent")
if list_schema:
if parent:
action_suffix += f'?parent_id={parent.id}'
schema = self.list_schema() schema = self.list_schema()
schema = schema.bind(request=self.req) schema = schema.bind(request=self.req)
new_buttons = kwargs.get("new_buttons")
if not new_buttons: if not new_buttons:
new_buttons = self.new_buttons new_buttons = self.new_buttons
list_url = kwargs.get("list_url", self.list_url)
if not list_url and self.list_route: if not list_url and self.list_route:
list_url = self.req.route_url(self.list_route) list_url = self.req.route_url(self.list_route)
else: else:
list_url = list_url and list_url[0:1] != "/" and "/" + list_url or list_url list_url = list_url and list_url[0:1] != "/" and "/" + list_url or list_url
list_url = self.home + list_url list_url = self.home + list_url
action_suffix = list_url and kwargs.get("action_suffix", "/grid/act") or None
table = DeTable(schema, table = DeTable(schema,
action=list_url, action=list_url,
action_suffix=action_suffix, action_suffix=action_suffix,
...@@ -273,14 +303,18 @@ class BaseView(object): ...@@ -273,14 +303,18 @@ class BaseView(object):
allow_post=allow_post, allow_post=allow_post,
allow_unpost=allow_unpost, allow_unpost=allow_unpost,
allow_check=allow_check, allow_check=allow_check,
check_field=check_field,
state_save=state_save, state_save=state_save,
new_buttons=new_buttons, new_buttons=new_buttons,
filter_columns=filter_columns, filter_columns=filter_columns,
server_side=server_side server_side=server_side,
scroll_y=scroll_y,
scroll_x=scroll_x
) )
resources = table.get_widget_resources() resources = table.get_widget_resources()
# resources=dict(css="", js="") # resources=dict(css="", js="")
if kwargs.get("is_object"): if is_object:
return dict(form=table, scripts="", css=resources["css"], return dict(form=table, scripts="", css=resources["css"],
js=resources["js"]) js=resources["js"])
...@@ -307,6 +341,12 @@ class BaseView(object): ...@@ -307,6 +341,12 @@ class BaseView(object):
return self.next_act(**kwargs) return self.next_act(**kwargs)
def get_list(self, **kwargs): def get_list(self, **kwargs):
"""
parameter
list_schema optional
list_join callback
list_filter callback
"""
url = [] url = []
select_list = {} select_list = {}
list_schema = kwargs.get("list_schema") list_schema = kwargs.get("list_schema")
...@@ -354,18 +394,17 @@ class BaseView(object): ...@@ -354,18 +394,17 @@ class BaseView(object):
columns = self.columns columns = self.columns
query = self.db_session.query().select_from(self.table) query = self.db_session.query().select_from(self.table)
table_join = kwargs.get('table_join') list_join = kwargs.get('list_join')
if table_join is not None: if list_join is not None:
query = table_join(query, **kwargs) query = list_join(query, **kwargs)
else: else:
query = self.list_join(query, **kwargs) query = self.list_join(query, **kwargs)
if self.req.user and self.req.user.company_id and hasattr(self.table, "company_id"): if self.req.user and self.req.user.company_id and hasattr(self.table, "company_id"):
query = query.filter( query = query.filter(
self.table.company_id == self.req.user.company_id) self.table.company_id == self.req.user.company_id)
list_filter = kwargs.get('list_filter')
table_filter = kwargs.get('table_filter') if list_filter is not None:
if table_filter is not None: query = list_filter(query, **kwargs)
query = table_filter(query, **kwargs)
else: else:
query = self.list_filter(query, **kwargs) query = self.list_filter(query, **kwargs)
...@@ -462,6 +501,7 @@ class BaseView(object): ...@@ -462,6 +501,7 @@ class BaseView(object):
def view_view(self, **kwargs): # row = query_id(request).first() def view_view(self, **kwargs): # row = query_id(request).first()
request = self.req request = self.req
row = self.query_id().first() row = self.query_id().first()
self.ses["readonly"]=True
if not row: if not row:
return self.id_not_found() return self.id_not_found()
is_object = kwargs.get("is_object", self.is_object) is_object = kwargs.get("is_object", self.is_object)
...@@ -595,6 +635,7 @@ class BaseView(object): ...@@ -595,6 +635,7 @@ class BaseView(object):
is_object = kwargs.get("is_object", self.is_object) is_object = kwargs.get("is_object", self.is_object)
kwargs["is_object"] = is_object kwargs["is_object"] = is_object
table = self.get_item_table(**kwargs) table = self.get_item_table(**kwargs)
self.ses["readonly"]=False
if self.req.POST: if self.req.POST:
if 'save' in self.req.POST: if 'save' in self.req.POST:
controls = self.req.POST.items() controls = self.req.POST.items()
...@@ -681,11 +722,12 @@ class BaseView(object): ...@@ -681,11 +722,12 @@ class BaseView(object):
d[f] = d[f].strip() d[f] = d[f].strip()
return d return d
def get_item_table(self, row=None, **kwargs): def get_item_table(self, parent=None, **kwargs):
if not self.form_list: if not self.form_list:
return None return None
self.list_schema = self.form_list self.list_schema = self.form_list
kwargs["is_object"] = True kwargs["is_object"] = True
kwargs["parent"] = parent
return self.view_list(**kwargs) return self.view_list(**kwargs)
def before_edit(self, form): def before_edit(self, form):
...@@ -701,6 +743,7 @@ class BaseView(object): ...@@ -701,6 +743,7 @@ class BaseView(object):
def view_edit(self, **kwargs): def view_edit(self, **kwargs):
request = self.req request = self.req
self.ses["readonly"]=False
row = self.query_id().first() row = self.query_id().first()
is_object = kwargs.get("is_object", self.is_object) is_object = kwargs.get("is_object", self.is_object)
kwargs["is_object"] = is_object kwargs["is_object"] = is_object
...@@ -754,6 +797,7 @@ class BaseView(object): ...@@ -754,6 +797,7 @@ class BaseView(object):
def view_delete(self, **kwargs): def view_delete(self, **kwargs):
request = self.req request = self.req
q = self.query_id() q = self.query_id()
self.ses["readonly"]=True
row = q.first() row = q.first()
is_object = kwargs.get("is_object", self.is_object) is_object = kwargs.get("is_object", self.is_object)
kwargs["is_object"] = is_object kwargs["is_object"] = is_object
......
...@@ -116,7 +116,10 @@ class DeTable(field.Field): ...@@ -116,7 +116,10 @@ class DeTable(field.Field):
allow_post=False, allow_post=False,
allow_unpost=False, allow_unpost=False,
allow_check=False, allow_check=False,
check_field=False,
filter_columns=False, filter_columns=False,
scroll_x=False,
scroll_y=False,
**kw **kw
): ):
super().__init__(schema, **kw) super().__init__(schema, **kw)
...@@ -131,7 +134,10 @@ class DeTable(field.Field): ...@@ -131,7 +134,10 @@ class DeTable(field.Field):
self.allow_post = json.dumps(allow_post) self.allow_post = json.dumps(allow_post)
self.allow_unpost = json.dumps(allow_unpost) self.allow_unpost = json.dumps(allow_unpost)
self.allow_check = json.dumps(allow_check) self.allow_check = json.dumps(allow_check)
self.filter_columns = json.dumps(filter_columns) self.check_field = json.dumps(check_field)
self.filter_columns = filter_columns
self.scroll_x = json.dumps(scroll_x)
self.scroll_y = json.dumps(scroll_y)
# Button yang dikirim sebagai tambahan # Button yang dikirim sebagai tambahan
new_buttons = kw.get("new_buttons") or () new_buttons = kw.get("new_buttons") or ()
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
allow_post allow_post|field.allow_post; allow_post allow_post|field.allow_post;
allow_unpost allow_unpost|field.allow_unpost; allow_unpost allow_unpost|field.allow_unpost;
allow_check allow_check|field.allow_check; allow_check allow_check|field.allow_check;
check_field check_field|field.check_field;
state_save state_save|field.state_save; state_save state_save|field.state_save;
filter_columns filter_columns|field.filter_columns; filter_columns filter_columns|field.filter_columns;
filter_scripts filter_scripts|field.filter_scripts; filter_scripts filter_scripts|field.filter_scripts;
...@@ -47,34 +48,34 @@ ...@@ -47,34 +48,34 @@
</div> </div>
</div> </div>
</div> </div>
<div class="row" tal:condition="allow_check=='true'"> <div tal:condition="allow_check=='true'">
<div class="form-group col-md-3"> <div class="row">
<div class="input-group"> <div class="input-group col-md-2">
<input type="checkbox" class="${tableid}checkAll"/> <span class="input-group-addon">
<span <input type="checkbox" class="${tableid}checkAll"/>
class="input-group-addon">Pilih Semua
</span> </span>
<label for="${tableid}checkAll">Pilih Semua</label>
</div> </div>
<script>
$(document).ready(function () {
$('.${tableid}checkAll').click(function () {
if (this.checked) {
console.log("AAAAAAAAAAAA");
$(".${tableid}_check").prop("checked", true);
} else {
console.log("BBBBBBBBBBBB");
$(".${tableid}_check").prop("checked", false);
}
});
});
</script>
</div> </div>
<script>
$(document).ready(function () {
$('.${tableid}checkAll').click(function () {
if (this.checked) {
$(".${tableid}_check").prop("checked", true);
} else {
$(".${tableid}_check").prop("checked", false);
}
});
});
</script>
</div> </div>
<table id="${tableid}" <table id="${tableid}"
class="table table-bordered table-hover table-condensed dataTable no-footer"> class="table table-bordered table-hover table-condensed dataTable no-footer">
<thead> <thead>
<tr> <tr>
<th tal:repeat="child field">${child.title}</th> <tal:block tal:repeat="child field">
<th tal:condition="python:hasattr(child, 'action') and not getattr(child.condition)==False and False or True">${child.title}</th>
</tal:block>
</tr> </tr>
</thead> </thead>
...@@ -100,6 +101,7 @@ ...@@ -100,6 +101,7 @@
var o${tableid}Url = o${tableid}Uri + "${url_suffix}"; var o${tableid}Url = o${tableid}Uri + "${url_suffix}";
var m${tableid}ID; var m${tableid}ID;
var m${tableid}CheckList = []; var m${tableid}CheckList = [];
var check_field =${check_field};
deform.addCallback('${tableid}', function (oid) { deform.addCallback('${tableid}', function (oid) {
// $(document).ready(function () { // $(document).ready(function () {
...@@ -184,11 +186,20 @@ ...@@ -184,11 +186,20 @@
} }
return result; return result;
} }
} else if (${tableid}Columns[co].data === "id" && ${tableid}Columns[co].action === true) { } else if (${tableid}Columns[co].data === "id") {
${tableid}Columns[co].render = function (id) { ${tableid}Columns[co].render = function (id, typ, data, setting) {
if (${tableid}Columns[co].action === false) return ""
let result = ""; let result = "";
if (${allow_check}) { if (${allow_check}) {
result = '<input type="checkbox" class="${tableid}_check" value="' + id + '"/>&nbsp;'; var checked = "";
if (check_field !== "") {
checked = data[check_field] !== null ? "checked" : "";
m${tableid}CheckList.push(id);
}
var mtableId = "${tableid}";
result = '<input type="checkbox" class="' + mtableId + '_check" value="' + id + '" ' + checked + ' name="' + mtableId + '_check"/>&nbsp;';
} }
if (${allow_view}) if (${allow_view})
result += '<a href="${url}/' + id + '/view"><i class="fas fa-eye" aria-hidden="true" title="View"></i></a>&nbsp;'; result += '<a href="${url}/' + id + '/view"><i class="fas fa-eye" aria-hidden="true" title="View"></i></a>&nbsp;';
...@@ -212,45 +223,52 @@ ...@@ -212,45 +223,52 @@
} }
let ${tableid}Params = { let ${tableid}Params = {}
dom: '<"row"<"col-md-8"<"toolbar">Bl><"col-md-4"fr>>tip',
processing: true,
serverSide: ${server_side},
stateSave: ${state_save},
scrollCollapse: true,
sort: ${sorts},
info: true,
filter: ${filters},
autoWidth: false,
paginate: ${paginates},
paginationType: "full_numbers",
order: [],
lengthMenu: [
[10, 25, 50, 100],
[10, 25, 50, 100]
],
pageLength: 25,
columns: ${tableid}Columns,
language: ${tableid}Language,
initComplete: function () {
$('.dataTables_filter input').unbind()
.bind('keyup', function (e) {
var code = e.keyCode || e.which;
if (code === 13) {
o${tableid}.search(this.value).draw();
}
});
}
}
var param_ajax = "";
var param_data = "";
if (!${server_side}) { if (!${server_side}) {
${tableid}Params.data = ${data}; param_data = ${data};
} else { } else {
${tableid}Params.ajax = o${tableid}Url; param_ajax = o${tableid}Url;
} }
o${tableid} = $('#${tableid}').DataTable(${tableid}Params); o${tableid} = $('#${tableid}').DataTable({
scrollX: ${field.scroll_x},
scrollY: ${field.scroll_y},
dom: '<"row"<"col-md-8"<"toolbar">Bl><"col-md-4"fr>>tip',
processing: true,
data: param_data,
ajax: param_ajax,
serverSide: ${server_side},
stateSave: ${state_save},
scrollCollapse: true,
sort: ${sorts},
info: true,
filter: ${filters},
autoWidth: false,
paginate: ${paginates},
paginationType: "full_numbers",
order: [],
lengthMenu: [
[10, 25, 50, 100],
[10, 25, 50, 100]
],
pageLength: 25,
columns: ${tableid}Columns,
language: ${tableid}Language,
initComplete: function () {
$('.dataTables_filter input').unbind()
.bind('keyup', function (e) {
var code = e.keyCode || e.which;
if (code === 13) {
o${tableid}.search(this.value).draw();
}
});
}
}
);
let tb = tb_array.join(' '); let tb = tb_array.join(' ');
$("div.toolbar").html(tb); $("div.toolbar").html(tb);
$("div.toolbar").attr('style', 'display:block; float: left; margin-bottom:6px; line-height:16px;'); $("div.toolbar").attr('style', 'display:block; float: left; margin-bottom:6px; line-height:16px;');
...@@ -273,16 +291,15 @@ ...@@ -273,16 +291,15 @@
$(this).addClass('row_selected'); $(this).addClass('row_selected');
} }
}); });
<tal:block tal:condition="filter_columns">
$(".${tableid}-control-filter").on('keyup', function (e) {
$(".${tableid}-control-filter").on('keyup', function (e) {
var code = e.keyCode || e.which; var code = e.keyCode || e.which;
if (code === 13) { if (code === 13) {
filter_table() filter_table()
} }
}); });
function filter_table() { function filter_table() {
$(".${tableid}-control-filter").each(function (e) { $(".${tableid}-control-filter").each(function (e) {
var col_id = $(this).attr("id"); var col_id = $(this).attr("id");
var value; var value;
...@@ -339,47 +356,47 @@ ...@@ -339,47 +356,47 @@
o${tableid}.draw(); o${tableid}.draw();
} }
$(".${tableid}-control-filter").on('click', function (e) { $(".${tableid}-control-filter").on('click', function (e) {
console.log("Write Data Click"); console.log("Write Data Click");
var typ = $(this).attr("type"); var typ = $(this).attr("type");
if (typ === 'radio') { if (typ === 'radio') {
filter_table() filter_table()
} }
}); });
$(".${tableid}-control-filter").on('change', function (e) { $(".${tableid}-control-filter").on('change', function (e) {
var typ = $(this).prop("nodeName").toLowerCase(); var typ = $(this).prop("nodeName").toLowerCase();
if (typ === "select") { if (typ === "select") {
filter_table() filter_table()
} }
var typ = $(this).attr("type").toLowerCase(); var typ = $(this).attr("type").toLowerCase();
if (typ === "date") { if (typ === "date") {
filter_table() filter_table()
} }
}); });
console.log("Read Data"); console.log("Read Data");
$(".${tableid}-control-filter").each(function (e) { $(".${tableid}-control-filter").each(function (e) {
var col_id = $(this).attr("id") var col_id = $(this).attr("id")
var value; var value;
if ($(this).attr("type") === 'radio') { if ($(this).attr("type") === 'radio') {
col_id = $(this).attr('id').split("-"); col_id = $(this).attr('id').split("-");
col_id.length = 2; col_id.length = 2;
col_id = col_id.join("-"); col_id = col_id.join("-");
value = localStorage.getItem(col_id); value = localStorage.getItem(col_id);
if (value !== undefined && value != null) if (value !== undefined && value != null)
if ($(this).val() === value) $(this).attr("checked", true); if ($(this).val() === value) $(this).attr("checked", true);
else $(this).attr("checked", false); else $(this).attr("checked", false);
} else { } else {
value = localStorage.getItem(col_id); value = localStorage.getItem(col_id);
if (value !== undefined && value !== null) if (value !== undefined && value !== null)
$(this).val(value); $(this).val(value);
} }
console.log(col_id, $(this).attr('id'), value); console.log(col_id, $(this).attr('id'), value);
}); });
${structure: btnscripts} ${structure: btnscripts}
filter_table(); filter_table();
// }); </tal:block>
${structure: filter_scripts} ${structure: filter_scripts}
}); });
</script> </script>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!